From 4a6b57de89a0fff03be3b03ccce8339c0e9d1dcb Mon Sep 17 00:00:00 2001 From: Luca Casonato Date: Mon, 4 Oct 2021 11:12:12 +0200 Subject: [PATCH] docs: new website (#66) --- .github/workflows/ci.yml | 2 +- README.md | 2 +- docs/getting-started.md | 2 +- example/pages/[name].tsx | 11 -- example/pages/api/joke.ts | 21 --- example/pages/index.tsx | 47 ------- example/pages/suspense.tsx | 28 ---- example/pages/x/[module].tsx | 40 ------ examples/todo-app/.vscode/settings.json | 5 + {example => examples/todo-app}/README.md | 2 +- examples/todo-app/deps.ts | 2 + examples/todo-app/main.ts | 11 ++ examples/todo-app/pages/index.tsx | 50 ++++++++ examples/todo-app/routes.gen.ts | 14 ++ examples/todo-app/utils/database.ts | 43 +++++++ makefile | 4 +- src/cli/init.ts | 2 +- src/server/render.tsx | 3 + tests/fixture/README.md | 2 +- www/README.md | 20 +++ {example => www}/components/Button.tsx | 0 {example => www}/components/Icons.tsx | 0 {example => www}/deps.ts | 0 {example => www}/main.ts | 0 {example => www}/pages/_render.ts | 0 www/pages/index.tsx | 155 +++++++++++++++++++++++ {example => www}/routes.gen.ts | 8 -- {example => www}/server_deps.ts | 0 {example => www}/static/favicon.ico | Bin 29 files changed, 311 insertions(+), 163 deletions(-) delete mode 100644 example/pages/[name].tsx delete mode 100644 example/pages/api/joke.ts delete mode 100644 example/pages/index.tsx delete mode 100644 example/pages/suspense.tsx delete mode 100644 example/pages/x/[module].tsx create mode 100644 examples/todo-app/.vscode/settings.json rename {example => examples/todo-app}/README.md (79%) create mode 100644 examples/todo-app/deps.ts create mode 100644 examples/todo-app/main.ts create mode 100644 examples/todo-app/pages/index.tsx create mode 100644 examples/todo-app/routes.gen.ts create mode 100644 examples/todo-app/utils/database.ts create mode 100644 www/README.md rename {example => www}/components/Button.tsx (100%) rename {example => www}/components/Icons.tsx (100%) rename {example => www}/deps.ts (100%) rename {example => www}/main.ts (100%) rename {example => www}/pages/_render.ts (100%) create mode 100644 www/pages/index.tsx rename {example => www}/routes.gen.ts (56%) rename {example => www}/server_deps.ts (100%) rename {example => www}/static/favicon.ico (100%) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 5c543e5f..6bbcf726 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -36,4 +36,4 @@ jobs: run: deno cache --no-check src/*/deps.ts - name: Run tests - run: deno test -A --unstable --no-check + run: deno test -A --no-check diff --git a/README.md b/README.md index 5c07e737..251a4cc0 100644 --- a/README.md +++ b/README.md @@ -46,7 +46,7 @@ fresh init myproject To now start the project, call `deno run`: ``` -deno run -A --unstable --watch main.ts +deno run -A --watch main.ts ``` To deploy the script to [Deno Deploy](https://deno.com/deploy), push your diff --git a/docs/getting-started.md b/docs/getting-started.md index 87ef3a83..d6abbdc4 100644 --- a/docs/getting-started.md +++ b/docs/getting-started.md @@ -45,7 +45,7 @@ To learn more about how projects are structured, take a look at the You can now run your new project locally using the `deno` CLI: ``` -deno run -A --unstable --watch main.ts +deno run -A --watch main.ts ``` The `--watch` option will cause your script to be reloaded on any changes to the diff --git a/example/pages/[name].tsx b/example/pages/[name].tsx deleted file mode 100644 index 505b0895..00000000 --- a/example/pages/[name].tsx +++ /dev/null @@ -1,11 +0,0 @@ -/** @jsx h */ - -import { h } from "../deps.ts"; - -interface Props { - params: Record; -} - -export default function Greet(props: Props) { - return
Hello {props.params.name}
; -} diff --git a/example/pages/api/joke.ts b/example/pages/api/joke.ts deleted file mode 100644 index 86656fa5..00000000 --- a/example/pages/api/joke.ts +++ /dev/null @@ -1,21 +0,0 @@ -import { HandlerContext } from "../../../server.ts"; - -// Jokes courtesy of https://punsandoneliners.com/randomness/programmer-jokes/ -const JOKES = [ - "Why do Java developers often wear glasses? They can’t C#.", - "A SQL query walks into a bar, goes up to two tables and says “can I join you?", - "Wasn’t hard to crack Forrest Gump’s password. 1forrest1.", - "I love pressing the F5 key. It’s refreshing.", - "Called IT support and a chap from Australia came to fix my network connection. I asked “Do you come from a LAN down under?”", - "There are 10 types of people in the world. Those who understand binary and those who don’t.", - "Why are assembly programmers often wet? They work below C level.", - "My favourite computer based band is the Black IPs.", - "What programme do you use to predict the music tastes of former US presidential candidates? An Al Gore Rhythm.", - "An SEO expert walked into a bar, pub, inn, tavern, hostelry, public house.", -]; - -export const handler = (_ctx: HandlerContext): Response => { - const randomIndex = Math.floor(Math.random() * 10); - const body = JOKES[randomIndex]; - return new Response(body); -}; diff --git a/example/pages/index.tsx b/example/pages/index.tsx deleted file mode 100644 index 8b6521e2..00000000 --- a/example/pages/index.tsx +++ /dev/null @@ -1,47 +0,0 @@ -/** @jsx h */ - -import { RoundedButton } from "../components/Button.tsx"; -import { IconMinus, IconPlus } from "../components/Icons.tsx"; -import { h, IS_BROWSER, PageConfig, tw, useState } from "../deps.ts"; - -export default function Home() { - return ( -
-

- Welcome to `fresh`. Try update this message in the ./pages/index.tsx - file, and refresh. -

- -

- {IS_BROWSER ? "Viewing browser render." : "Viewing JIT render."} -

-
- ); -} - -function Counter() { - const [count, setCount] = useState(0); - return ( -
-

Current count: {count}

-
- setCount(count - 1)} - disabled={!IS_BROWSER} - > - - - setCount(count + 1)} - disabled={!IS_BROWSER} - > - - -
-
- ); -} - -export const config: PageConfig = { runtimeJS: true }; diff --git a/example/pages/suspense.tsx b/example/pages/suspense.tsx deleted file mode 100644 index fd3c592d..00000000 --- a/example/pages/suspense.tsx +++ /dev/null @@ -1,28 +0,0 @@ -/** @jsx h */ - -import { h, Suspense, tw, useData } from "../deps.ts"; - -export default function SuspensePage() { - return ( - - Loading... -

- )} - > - -
- ); -} - -export function SuspendedComponent() { - const data = useData("Loaded", delayer); - return

{data}!

; -} - -// A function that returns a promise that resolves after a delay with the given -// value. -function delayer(key: string) { - return new Promise((resolve) => setTimeout(() => resolve(key), 2000)); -} diff --git a/example/pages/x/[module].tsx b/example/pages/x/[module].tsx deleted file mode 100644 index 69940296..00000000 --- a/example/pages/x/[module].tsx +++ /dev/null @@ -1,40 +0,0 @@ -/** @jsx h */ - -import { h, PageConfig, PageProps, useData } from "../../deps.ts"; - -export default function ModuleInfoPage({ params }: PageProps) { - const module = params.module; - - if (module.includes("/")) { - return
Invalid module name.
; - } - - const url = `https://cdn.deno.land/${params.module}/meta/versions.json`; - const info = useData(url, fetcher); - - if (info === null) { - return
No module found with name `{params.module}`.
; - } - - const { latest, versions } = info; - return ( -

- The module `{params.module}` has {versions.length} versions, with {latest} - {" "} - being the most recent. -

- ); -} - -interface ModuleInfo { - latest: string; - versions: string[]; -} - -async function fetcher(url: string): Promise { - const resp = await fetch(url); - if (resp.status === 200) return resp.json(); - return null; -} - -export const config: PageConfig = { runtimeJS: false }; diff --git a/examples/todo-app/.vscode/settings.json b/examples/todo-app/.vscode/settings.json new file mode 100644 index 00000000..e40716fd --- /dev/null +++ b/examples/todo-app/.vscode/settings.json @@ -0,0 +1,5 @@ +{ + "deno.enable": true, + "deno.lint": true, + "deno.unstable": true +} diff --git a/example/README.md b/examples/todo-app/README.md similarity index 79% rename from example/README.md rename to examples/todo-app/README.md index 850ee95d..664ae48a 100644 --- a/example/README.md +++ b/examples/todo-app/README.md @@ -5,7 +5,7 @@ Start the project: ``` -deno run -A --unstable --watch main.ts +deno run -A --watch main.ts ``` After adding, removing, or moving a page in the `pages` directory, run: diff --git a/examples/todo-app/deps.ts b/examples/todo-app/deps.ts new file mode 100644 index 00000000..4c772c75 --- /dev/null +++ b/examples/todo-app/deps.ts @@ -0,0 +1,2 @@ +export * from "../../runtime.ts"; +export * from "../../server.ts"; diff --git a/examples/todo-app/main.ts b/examples/todo-app/main.ts new file mode 100644 index 00000000..d65a6340 --- /dev/null +++ b/examples/todo-app/main.ts @@ -0,0 +1,11 @@ +/// +/// +/// +/// +/// +/// + +import { start } from "../../server.ts"; +import routes from "./routes.gen.ts"; + +await start(routes); diff --git a/examples/todo-app/pages/index.tsx b/examples/todo-app/pages/index.tsx new file mode 100644 index 00000000..7bc5bb2b --- /dev/null +++ b/examples/todo-app/pages/index.tsx @@ -0,0 +1,50 @@ +/** @jsx h */ +import { h, HandlerContext, Suspense, useData } from "../deps.ts"; +import { getDatabase, Todo } from "../utils/database.ts"; + +export default function TodoListPage() { + return ( +
+

Your TODOs

+ + Loading...

}> + +
+
+ ); +} + +function AddTodoForm() { + return ( +
+ + +
+ ); +} + +function TodoList() { + const todos = useData("todos", async () => { + const db = await getDatabase(); + const todos = await db.listTodos(); + return todos; + }); + return ( +
    + {todos.map((todo) =>
  • {todo.text}
  • )} +
+ ); +} + +export const handler = { + async POST(ctx: HandlerContext): Promise { + const form = await ctx.req.formData(); + const text = form.get("text"); + if (typeof text !== "string") { + return new Response("misformed form", { status: 400 }); + } + const db = await getDatabase(); + await db.addTodo(text); + return Response.redirect(ctx.req.url, 303); + }, +}; diff --git a/examples/todo-app/routes.gen.ts b/examples/todo-app/routes.gen.ts new file mode 100644 index 00000000..5b4125c2 --- /dev/null +++ b/examples/todo-app/routes.gen.ts @@ -0,0 +1,14 @@ +// DO NOT EDIT. This file is generated by `fresh`. +// This file SHOULD be checked into source version control. +// To update this file, run `fresh routes`. + +import * as $0 from "./pages/index.tsx"; + +const routes = { + pages: { + "./pages/index.tsx": $0, + }, + baseUrl: import.meta.url, +}; + +export default routes; diff --git a/examples/todo-app/utils/database.ts b/examples/todo-app/utils/database.ts new file mode 100644 index 00000000..8208c206 --- /dev/null +++ b/examples/todo-app/utils/database.ts @@ -0,0 +1,43 @@ +import { Client } from "https://raw.githubusercontent.com/denodrivers/postgres/72f523cb9d8f863204a3fa02f52d534e7ce30ccf/mod.ts"; + +export interface Todo { + id: number; + text: string; +} + +class Database { + #client: Client; + + constructor(client: Client) { + this.#client = client; + } + + static async connect() { + const client = new Client(Deno.env.get("DATABASE_URL")); + await client.connect(); + await client.queryObject`CREATE TABLE IF NOT EXISTS todos ( + id SERIAL PRIMARY KEY, + text TEXT NOT NULL + )`; + return new Database(client); + } + + async listTodos(): Promise { + const { rows } = await this.#client.queryObject`SELECT * FROM todos`; + return rows; + } + + async addTodo(text: string): Promise { + const { rows } = await this.#client.queryObject` + INSERT INTO todos (text) VALUES (${text}) + RETURNING * + `; + return rows[0]; + } +} + +const db: Database | Promise = Database.connect(); + +export async function getDatabase(): Promise { + return await db; +} diff --git a/makefile b/makefile index b5a9f9f4..36b2d61b 100644 --- a/makefile +++ b/makefile @@ -7,5 +7,5 @@ lint: test: deno test -A -watch_example: - deno run -A --unstable --watch --no-check example/main.ts \ No newline at end of file +dev: + deno run -A --watch --no-check www/main.ts \ No newline at end of file diff --git a/src/cli/init.ts b/src/cli/init.ts index 791fc845..0f9f5e8f 100644 --- a/src/cli/init.ts +++ b/src/cli/init.ts @@ -165,7 +165,7 @@ await start(routes); Start the project: \`\`\` -deno run -A --unstable --watch main.ts +deno run -A --watch main.ts \`\`\` After adding, removing, or moving a page in the \`pages\` directory, run: diff --git a/src/server/render.tsx b/src/server/render.tsx index 07973c04..8df01b48 100644 --- a/src/server/render.tsx +++ b/src/server/render.tsx @@ -272,6 +272,9 @@ export function template(opts: TemplateOptions): string { const page = ( + + + {opts.preloads.map((src) => )} {opts.imports.map((src) => )}