From 79d8cfc3ed83814bfbaa93bb8564b9311436ac7b Mon Sep 17 00:00:00 2001 From: Marvin Hagemeister Date: Tue, 9 Sep 2025 14:36:51 +0200 Subject: [PATCH] docs: make 2.x the default version (#3369) --- deno.json | 3 +- deno.lock | 37 ++- .../concepts/ahead-of-time-builds.md | 2 +- docs/{latest => 1.x}/concepts/app-wrapper.md | 0 docs/{latest => 1.x}/concepts/architecture.md | 0 .../{latest => 1.x}/concepts/data-fetching.md | 0 docs/{latest => 1.x}/concepts/deployment.md | 3 +- docs/{latest => 1.x}/concepts/error-pages.md | 0 docs/{latest => 1.x}/concepts/forms.md | 0 docs/1.x/concepts/index.md | 8 + docs/1.x/concepts/islands.md | 228 ++++++++++++++ docs/{canary => 1.x}/concepts/layouts.md | 40 ++- docs/1.x/concepts/middleware.md | 222 +++++++++++++ docs/{latest => 1.x}/concepts/partials.md | 2 +- docs/{latest => 1.x}/concepts/plugins.md | 12 +- docs/{latest => 1.x}/concepts/routes.md | 0 docs/1.x/concepts/routing.md | 151 +++++++++ docs/1.x/concepts/server-components.md | 17 + .../concepts/server-configuration.md | 4 +- docs/1.x/concepts/static-files.md | 55 ++++ docs/{latest => 1.x}/concepts/updating.md | 0 docs/1.x/examples/active-links.md | 79 +++++ .../examples/authentication-with-supabase.md | 4 +- .../examples/changing-the-src-dir.md | 0 .../client-side-components-and-libraries.md | 0 .../examples/creating-a-crud-api.md | 2 +- .../examples/dealing-with-cors.md | 2 +- .../examples/handling-complex-routes.md | 4 +- docs/{latest => 1.x}/examples/index.md | 0 .../examples/init-the-server.md | 0 .../examples/migrating-to-tailwind.md | 2 +- .../examples/modifying-the-head.md | 0 .../examples/rendering-markdown.md | 0 docs/1.x/examples/rendering-raw-html.md | 83 +++++ .../examples/setting-the-language.md | 0 .../examples/sharing-state-between-islands.md | 0 docs/{latest => 1.x}/examples/using-csp.md | 0 .../examples/using-fresh-canary-version.md | 0 .../examples/using-twind-v1.md | 0 .../{latest => 1.x}/examples/writing-tests.md | 2 +- .../getting-started/adding-interactivity.md | 0 .../getting-started/create-a-project.md | 0 .../getting-started/create-a-route.md | 4 +- .../getting-started/custom-handlers.md | 0 .../getting-started/deploy-to-production.md | 0 .../getting-started/dynamic-routes.md | 2 +- .../getting-started/form-submissions.md | 4 +- docs/1.x/getting-started/index.md | 16 + .../getting-started/running-locally.md | 0 docs/{latest => 1.x}/integrations/index.md | 0 docs/1.x/introduction/index.md | 35 +++ docs/canary/concepts/index.md | 36 --- docs/canary/concepts/islands.md | 135 -------- docs/canary/concepts/middleware.md | 89 ------ docs/canary/concepts/routing.md | 32 -- docs/canary/concepts/static-files.md | 52 ---- docs/canary/examples/active-links.md | 48 --- docs/canary/examples/rendering-raw-html.md | 32 -- docs/canary/getting-started/index.md | 128 -------- docs/canary/introduction/index.md | 75 ----- .../advanced/app-wrapper.md | 6 +- docs/{canary => latest}/advanced/builder.md | 0 docs/{canary => latest}/advanced/define.md | 0 .../advanced/environment-variables.md | 0 .../advanced/error-handling.md | 2 +- docs/{canary => latest}/advanced/forms.md | 0 docs/{canary => latest}/advanced/head.md | 0 docs/{canary => latest}/advanced/index.md | 0 docs/{canary => latest}/advanced/layouts.md | 0 docs/{canary => latest}/advanced/partials.md | 0 .../advanced/troubleshooting.md | 0 docs/{canary => latest}/advanced/vite.md | 0 docs/{canary => latest}/concepts/app.md | 11 +- docs/{canary => latest}/concepts/context.md | 0 .../concepts/file-routing.md | 14 +- docs/latest/concepts/index.md | 34 +- docs/latest/concepts/islands.md | 223 ++++--------- docs/latest/concepts/layouts.md | 40 +-- docs/latest/concepts/middleware.md | 292 +++++------------- docs/latest/concepts/routing.md | 167 ++-------- docs/latest/concepts/server-components.md | 17 - docs/latest/concepts/static-files.md | 79 +++-- .../deployment/cloudflare-workers.md | 0 .../deployment/deno-compile.md | 0 .../deployment/deno-deploy.md | 0 docs/{canary => latest}/deployment/docker.md | 0 docs/{canary => latest}/deployment/index.md | 0 docs/latest/examples/active-links.md | 47 +-- docs/{canary => latest}/examples/daisyui.md | 0 docs/{canary => latest}/examples/markdown.md | 0 .../examples/migration-guide.md | 10 +- docs/latest/examples/rendering-raw-html.md | 83 +---- docs/latest/getting-started/index.md | 124 +++++++- docs/latest/introduction/index.md | 80 +++-- docs/{canary => latest}/plugins/cors.md | 0 docs/{canary => latest}/plugins/csrf.md | 0 docs/{canary => latest}/plugins/index.md | 4 +- .../plugins/trailing-slashes.md | 0 docs/{canary => latest}/testing/index.md | 12 +- docs/toc.ts | 87 +++--- tools/check_links.ts | 61 ++++ www/components/homepage/FormsSection.tsx | 2 +- www/components/homepage/Hero.tsx | 4 +- www/components/homepage/PartialsSection.tsx | 2 +- www/main_test.ts | 19 +- www/routes/_error.tsx | 3 + www/routes/docs/_middleware.ts | 4 +- www/routes/index.tsx | 6 +- www/vite.config.ts | 8 +- 109 files changed, 1589 insertions(+), 1502 deletions(-) rename docs/{latest => 1.x}/concepts/ahead-of-time-builds.md (98%) rename docs/{latest => 1.x}/concepts/app-wrapper.md (100%) rename docs/{latest => 1.x}/concepts/architecture.md (100%) rename docs/{latest => 1.x}/concepts/data-fetching.md (100%) rename docs/{latest => 1.x}/concepts/deployment.md (95%) rename docs/{latest => 1.x}/concepts/error-pages.md (100%) rename docs/{latest => 1.x}/concepts/forms.md (100%) create mode 100644 docs/1.x/concepts/index.md create mode 100644 docs/1.x/concepts/islands.md rename docs/{canary => 1.x}/concepts/layouts.md (74%) create mode 100644 docs/1.x/concepts/middleware.md rename docs/{latest => 1.x}/concepts/partials.md (99%) rename docs/{latest => 1.x}/concepts/plugins.md (95%) rename docs/{latest => 1.x}/concepts/routes.md (100%) create mode 100644 docs/1.x/concepts/routing.md create mode 100644 docs/1.x/concepts/server-components.md rename docs/{latest => 1.x}/concepts/server-configuration.md (97%) create mode 100644 docs/1.x/concepts/static-files.md rename docs/{latest => 1.x}/concepts/updating.md (100%) create mode 100644 docs/1.x/examples/active-links.md rename docs/{latest => 1.x}/examples/authentication-with-supabase.md (98%) rename docs/{latest => 1.x}/examples/changing-the-src-dir.md (100%) rename docs/{latest => 1.x}/examples/client-side-components-and-libraries.md (100%) rename docs/{latest => 1.x}/examples/creating-a-crud-api.md (99%) rename docs/{latest => 1.x}/examples/dealing-with-cors.md (97%) rename docs/{latest => 1.x}/examples/handling-complex-routes.md (92%) rename docs/{latest => 1.x}/examples/index.md (100%) rename docs/{latest => 1.x}/examples/init-the-server.md (100%) rename docs/{latest => 1.x}/examples/migrating-to-tailwind.md (98%) rename docs/{latest => 1.x}/examples/modifying-the-head.md (100%) rename docs/{latest => 1.x}/examples/rendering-markdown.md (100%) create mode 100644 docs/1.x/examples/rendering-raw-html.md rename docs/{latest => 1.x}/examples/setting-the-language.md (100%) rename docs/{latest => 1.x}/examples/sharing-state-between-islands.md (100%) rename docs/{latest => 1.x}/examples/using-csp.md (100%) rename docs/{latest => 1.x}/examples/using-fresh-canary-version.md (100%) rename docs/{latest => 1.x}/examples/using-twind-v1.md (100%) rename docs/{latest => 1.x}/examples/writing-tests.md (97%) rename docs/{latest => 1.x}/getting-started/adding-interactivity.md (100%) rename docs/{latest => 1.x}/getting-started/create-a-project.md (100%) rename docs/{latest => 1.x}/getting-started/create-a-route.md (95%) rename docs/{latest => 1.x}/getting-started/custom-handlers.md (100%) rename docs/{latest => 1.x}/getting-started/deploy-to-production.md (100%) rename docs/{latest => 1.x}/getting-started/dynamic-routes.md (97%) rename docs/{latest => 1.x}/getting-started/form-submissions.md (96%) create mode 100644 docs/1.x/getting-started/index.md rename docs/{latest => 1.x}/getting-started/running-locally.md (100%) rename docs/{latest => 1.x}/integrations/index.md (100%) create mode 100644 docs/1.x/introduction/index.md delete mode 100644 docs/canary/concepts/index.md delete mode 100644 docs/canary/concepts/islands.md delete mode 100644 docs/canary/concepts/middleware.md delete mode 100644 docs/canary/concepts/routing.md delete mode 100644 docs/canary/concepts/static-files.md delete mode 100644 docs/canary/examples/active-links.md delete mode 100644 docs/canary/examples/rendering-raw-html.md delete mode 100644 docs/canary/getting-started/index.md delete mode 100644 docs/canary/introduction/index.md rename docs/{canary => latest}/advanced/app-wrapper.md (79%) rename docs/{canary => latest}/advanced/builder.md (100%) rename docs/{canary => latest}/advanced/define.md (100%) rename docs/{canary => latest}/advanced/environment-variables.md (100%) rename docs/{canary => latest}/advanced/error-handling.md (97%) rename docs/{canary => latest}/advanced/forms.md (100%) rename docs/{canary => latest}/advanced/head.md (100%) rename docs/{canary => latest}/advanced/index.md (100%) rename docs/{canary => latest}/advanced/layouts.md (100%) rename docs/{canary => latest}/advanced/partials.md (100%) rename docs/{canary => latest}/advanced/troubleshooting.md (100%) rename docs/{canary => latest}/advanced/vite.md (100%) rename docs/{canary => latest}/concepts/app.md (94%) rename docs/{canary => latest}/concepts/context.md (100%) rename docs/{canary => latest}/concepts/file-routing.md (91%) delete mode 100644 docs/latest/concepts/server-components.md rename docs/{canary => latest}/deployment/cloudflare-workers.md (100%) rename docs/{canary => latest}/deployment/deno-compile.md (100%) rename docs/{canary => latest}/deployment/deno-deploy.md (100%) rename docs/{canary => latest}/deployment/docker.md (100%) rename docs/{canary => latest}/deployment/index.md (100%) rename docs/{canary => latest}/examples/daisyui.md (100%) rename docs/{canary => latest}/examples/markdown.md (100%) rename docs/{canary => latest}/examples/migration-guide.md (97%) rename docs/{canary => latest}/plugins/cors.md (100%) rename docs/{canary => latest}/plugins/csrf.md (100%) rename docs/{canary => latest}/plugins/index.md (79%) rename docs/{canary => latest}/plugins/trailing-slashes.md (100%) rename docs/{canary => latest}/testing/index.md (87%) create mode 100644 tools/check_links.ts diff --git a/deno.json b/deno.json index 6249b387..14e54b5a 100644 --- a/deno.json +++ b/deno.json @@ -86,7 +86,8 @@ "imagescript": "https://deno.land/x/imagescript@1.3.0/mod.ts", "marked": "npm:marked@^15.0.11", "marked-mangle": "npm:marked-mangle@^1.1.9", - "prismjs": "npm:prismjs@^1.29.0" + "prismjs": "npm:prismjs@^1.29.0", + "vite": "npm:vite@^7.1.5" }, "compilerOptions": { "lib": ["dom", "dom.asynciterable", "deno.ns", "deno.unstable"], diff --git a/deno.lock b/deno.lock index 58eb2567..88d1abd5 100644 --- a/deno.lock +++ b/deno.lock @@ -103,7 +103,9 @@ "npm:vite-plugin-inspect@^11.3.2": "11.3.3_vite@7.1.4__@types+node@24.3.0__picomatch@4.0.3_@types+node@24.3.0_@types+node@24.2.0", "npm:vite@*": "7.1.4_@types+node@24.3.0_picomatch@4.0.3_@types+node@24.2.0", "npm:vite@7.1.4": "7.1.4_@types+node@24.3.0_picomatch@4.0.3_@types+node@24.2.0", - "npm:vite@^7.1.4": "7.1.4_@types+node@24.3.0_picomatch@4.0.3_@types+node@24.2.0" + "npm:vite@7.1.5": "7.1.5_@types+node@24.2.0_picomatch@4.0.3", + "npm:vite@^7.1.4": "7.1.4_@types+node@24.3.0_picomatch@4.0.3_@types+node@24.2.0", + "npm:vite@^7.1.5": "7.1.5_@types+node@24.2.0_picomatch@4.0.3" }, "jsr": { "@astral/astral@0.5.3": { @@ -3800,6 +3802,13 @@ "picomatch@4.0.3" ] }, + "tinyglobby@0.2.15_picomatch@4.0.3": { + "integrity": "sha512-j2Zq4NyQYG5XMST4cbs02Ak8iJUdxRM0XI5QyxXuZOzKOINmWurp3smXu3y5wDcJrptwpSjgXHzIQxR0omXljQ==", + "dependencies": [ + "fdir", + "picomatch@4.0.3" + ] + }, "to-regex-range@5.0.1": { "integrity": "sha512-65P7iz6X5yEr1cwcgvQxbbIw7Uk3gOy5dIdtZ4rDveLqhrdJP+Li/Hx6tyK0NEb+2GCyneCMJiGqrADCSNk8sQ==", "dependencies": [ @@ -3935,7 +3944,7 @@ "picomatch@4.0.3", "postcss", "rollup", - "tinyglobby" + "tinyglobby@0.2.14_picomatch@4.0.3" ], "optionalDependencies": [ "fsevents" @@ -3954,7 +3963,26 @@ "picomatch@4.0.3", "postcss", "rollup", - "tinyglobby" + "tinyglobby@0.2.14_picomatch@4.0.3" + ], + "optionalDependencies": [ + "fsevents" + ], + "optionalPeers": [ + "@types/node@24.2.0" + ], + "bin": true + }, + "vite@7.1.5_@types+node@24.2.0_picomatch@4.0.3": { + "integrity": "sha512-4cKBO9wR75r0BeIWWWId9XK9Lj6La5X846Zw9dFfzMRw38IlTk2iCcUt6hsyiDRcPidc55ZParFYDXi0nXOeLQ==", + "dependencies": [ + "@types/node@24.2.0", + "esbuild", + "fdir", + "picomatch@4.0.3", + "postcss", + "rollup", + "tinyglobby@0.2.15_picomatch@4.0.3" ], "optionalDependencies": [ "fsevents" @@ -4135,7 +4163,8 @@ "npm:prismjs@^1.29.0", "npm:rollup@^4.50.0", "npm:tailwindcss@^4.1.10", - "npm:ts-morph@26" + "npm:ts-morph@26", + "npm:vite@^7.1.5" ], "members": { "packages/build-id": { diff --git a/docs/latest/concepts/ahead-of-time-builds.md b/docs/1.x/concepts/ahead-of-time-builds.md similarity index 98% rename from docs/latest/concepts/ahead-of-time-builds.md rename to docs/1.x/concepts/ahead-of-time-builds.md index 2bf3280e..bd4b6c0c 100644 --- a/docs/latest/concepts/ahead-of-time-builds.md +++ b/docs/1.x/concepts/ahead-of-time-builds.md @@ -35,7 +35,7 @@ metadata for Fresh. Any other static files generated by plugins will be stored in the `_fresh/static` subfolder. They will be served the same as other -[static files](/docs/concepts/static-files.md). +[static files](/docs/1.x/concepts/static-files.md). > [info]: The `_fresh` folder should not be committed to the repository. Add an > entry in the `.gitignore` file to ensure that it is not committed. Create that diff --git a/docs/latest/concepts/app-wrapper.md b/docs/1.x/concepts/app-wrapper.md similarity index 100% rename from docs/latest/concepts/app-wrapper.md rename to docs/1.x/concepts/app-wrapper.md diff --git a/docs/latest/concepts/architecture.md b/docs/1.x/concepts/architecture.md similarity index 100% rename from docs/latest/concepts/architecture.md rename to docs/1.x/concepts/architecture.md diff --git a/docs/latest/concepts/data-fetching.md b/docs/1.x/concepts/data-fetching.md similarity index 100% rename from docs/latest/concepts/data-fetching.md rename to docs/1.x/concepts/data-fetching.md diff --git a/docs/latest/concepts/deployment.md b/docs/1.x/concepts/deployment.md similarity index 95% rename from docs/latest/concepts/deployment.md rename to docs/1.x/concepts/deployment.md index e33bf70e..592b8747 100644 --- a/docs/latest/concepts/deployment.md +++ b/docs/1.x/concepts/deployment.md @@ -10,6 +10,7 @@ Here are instructions for specific providers / systems: - [Deno Deploy](#deno-deploy) - [Docker](#docker) +- [Self Contained Executable](#self-contained-executable) ## Deno Deploy @@ -83,4 +84,4 @@ $ deno compile --include static --include _fresh --include deno.json -A main.ts [aws-container-registry]: https://docs.aws.amazon.com/AmazonECS/latest/userguide/create-container-image.html#create-container-image-push-ecr [gcp-container-registry]: https://cloud.google.com/container-registry/docs/pushing-and-pulling [deno-deploy]: https://deno.com/deploy -[deploy-to-production]: /docs/getting-started/deploy-to-production +[deploy-to-production]: /docs/1.x/getting-started/deploy-to-production diff --git a/docs/latest/concepts/error-pages.md b/docs/1.x/concepts/error-pages.md similarity index 100% rename from docs/latest/concepts/error-pages.md rename to docs/1.x/concepts/error-pages.md diff --git a/docs/latest/concepts/forms.md b/docs/1.x/concepts/forms.md similarity index 100% rename from docs/latest/concepts/forms.md rename to docs/1.x/concepts/forms.md diff --git a/docs/1.x/concepts/index.md b/docs/1.x/concepts/index.md new file mode 100644 index 00000000..a1cb240c --- /dev/null +++ b/docs/1.x/concepts/index.md @@ -0,0 +1,8 @@ +--- +description: | + This chapter goes over some fundamental concepts of Fresh. +--- + +This chapter goes over some fundamental concepts of Fresh. It covers the +overarching architecture design of Fresh applications, as well as reference +documentation about the various features of Fresh. diff --git a/docs/1.x/concepts/islands.md b/docs/1.x/concepts/islands.md new file mode 100644 index 00000000..9d1aa2f7 --- /dev/null +++ b/docs/1.x/concepts/islands.md @@ -0,0 +1,228 @@ +--- +description: | + Islands enable client side interactivity in Fresh. They are hydrated on the client in addition to being rendered on the server. +--- + +Islands enable client side interactivity in Fresh. Islands are isolated Preact +components that are rendered on the server and then hydrated on the client. This +is different from all other components in Fresh, as they are usually rendered on +the server only. + +Islands are defined by creating a file in the `islands/` folder in a Fresh +project. The name of this file must be a PascalCase or kebab-case name of the +island. + +```tsx islands/my-island.tsx +import { useSignal } from "@preact/signals"; + +export default function MyIsland() { + const count = useSignal(0); + + return ( +
+ Counter is at {count}.{" "} + +
+ ); +} +``` + +An island can be used in a page like a regular Preact component. Fresh will take +care of automatically re-hydrating the island on the client. + +```tsx route/index.tsx +import MyIsland from "../islands/my-island.tsx"; + +export default function Home() { + return ; +} +``` + +## Passing JSX to islands + +Islands support passing JSX elements via the `children` property. + +```tsx islands/my-island.tsx +import { useSignal } from "@preact/signals"; +import { ComponentChildren } from "preact"; + +interface Props { + children: ComponentChildren; +} + +export default function MyIsland({ children }: Props) { + const count = useSignal(0); + + return ( +
+ Counter is at {count}.{" "} + + {children} +
+ ); +} +``` + +This allows you to pass static content rendered by the server to an island in +the browser. + +```tsx routes/index.tsx +import MyIsland from "../islands/my-island.tsx"; + +export default function Home() { + return ( + +

This text is rendered on the server

+
+ ); +} +``` + +You can also create shared components in your `components/` directory, which can +be used in both static content and interactive islands. When these components +are used within islands, interactivity can be added, such as `onClick` handlers +(using an `onClick` handler on a button outside of an island will not fire). + +```tsx islands/my-island.tsx +import { useSignal } from "@preact/signals"; +import { ComponentChildren } from "preact"; +import Card from "../components/Card.tsx"; +import Button from "../components/Button.tsx"; + +interface Props { + children: ComponentChildren; +} + +export default function MyIsland({ children }: Props) { + const count = useSignal(0); + + return ( + + Counter is at {count}.{" "} + + {children} + + ); +} +``` + +## Passing other props to islands + +Passing props to islands is supported, but only if the props are serializable. +Fresh can serialize the following types of values: + +- Primitive types `string`, `boolean`, `bigint`, and `null` +- Most `number`s (`Infinity`, `-Infinity`, and `NaN` are silently converted to + `null`) +- Plain objects with string keys and serializable values +- Arrays containing serializable values +- Uint8Array +- JSX Elements (restricted to `props.children`) +- Preact Signals (if the inner value is serializable) + +Circular references are supported. If an object or signal is referenced multiple +times, it is only serialized once and the references are restored upon +deserialization. Passing complex objects like `Date`, custom classes, or +functions is not supported. + +During server side rendering, Fresh annotates the HTML with special comments +that indicate where each island will go. This gives the code sent to the client +enough information to put the islands where they are supposed to go without +requiring hydration for the static children of interactive islands. No +Javascript is sent to the client when no interactivity is needed. + +```html Response body + +
+ Counter is at 0. + + +

This text is rendered on the server

+ +
+ +``` + +### Nesting islands + +Islands can be nested within other islands as well. In that scenario they act +like a normal Preact component, but still receive the serialized props if any +were present. + +```tsx islands/other-island.tsx +import { useSignal } from "@preact/signals"; +import { ComponentChildren } from "preact"; + +interface Props { + children: ComponentChildren; + foo: string; +} + +function randomNumber() { + return Math.floor(Math.random() * 100); +} + +export default function OtherIsland({ children, foo }: Props) { + const number = useSignal(randomNumber()); + + return ( +
+

String from props: {foo}

+

+ + {" "} + number is: {number}. +

+
+ ); +} +``` + +In essence, Fresh allows you to mix static and interactive parts in your app in +a way that's most optimal for your app. We'll keep sending only the JavaScript +that is needed for the islands to the browser. + +```tsx route/index.tsx +import MyIsland from "../islands/my-island.tsx"; +import OtherIsland from "../islands/other-island.tsx"; + +export default function Home() { + return ( +
+ + + +

Some more server rendered text

+
+ ); +} +``` + +## Rendering islands on client only + +When using client-only APIs, like `EventSource` or `navigator.getUserMedia`, +this component will not run on the server as it will produce an error like: + +``` +An error occurred during route handling or page rendering. ReferenceError: EventSource is not defined + at Object.MyIsland (file:///Users/someuser/fresh-project/islandsmy-island.tsx:6:18) + at m (https://esm.sh/v129/preact-render-to-string@6.2.0/X-ZS8q/denonext/preact-render-to-string.mjs:2:2602) + at m (https://esm.sh/v129/preact-render-to-string@6.2.0/X-ZS8q/denonext/preact-render-to-string.mjs:2:2113) + .... +``` + +Use the [`IS_BROWSER`](https://deno.land/x/fresh/runtime.ts?doc=&s=IS_BROWSER) +flag as a guard to fix the issue: + +```tsx islands/my-island.tsx +import { IS_BROWSER } from "$fresh/runtime.ts"; + +export function MyIsland() { + // Return any prerenderable JSX here which makes sense for your island + if (!IS_BROWSER) return
; + + // All the code which must run in the browser comes here! + // Like: EventSource, navigator.getUserMedia, etc. + return
; +} +``` diff --git a/docs/canary/concepts/layouts.md b/docs/1.x/concepts/layouts.md similarity index 74% rename from docs/canary/concepts/layouts.md rename to docs/1.x/concepts/layouts.md index 53110a10..bf04dad6 100644 --- a/docs/canary/concepts/layouts.md +++ b/docs/1.x/concepts/layouts.md @@ -26,16 +26,16 @@ template which can be conditioned based on state and params. Note that any state set by middleware is available via `props.state`. ```tsx routes/sub/_layout.tsx -import { define } from "../../utils.ts"; +import { PageProps } from "$fresh/server.ts"; -export default define.layout({ Component, state }) => { +export default function Layout({ Component, state }: PageProps) { // do something with state here return (
); -}) +} ``` ## Async layouts @@ -44,12 +44,33 @@ In case you need to fetch data asynchronously before rendering the layout, you can use an async layout to do so. ```tsx routes/sub/_layout.tsx -import { define } from "../../utils.ts"; +import { FreshContext } from "$fresh/server.ts"; -export default define.layout(async (ctx) => { +export default async function Layout(req: Request, ctx: FreshContext) { // do something with state here const data = await loadData(); + return ( +
+

{data.greeting}

+ +
+ ); +} +``` + +### Define helper + +To make it a little quicker to write async layouts, Fresh ships with a +`defineLayout` helper which automatically infers the correct types for the +function arguments. + +```tsx routes/greet/_layout.tsx +import { defineLayout } from "$fresh/server.ts"; + +export default defineLayout(async (req, ctx) => { + const data = await loadData(); + return (

{data.greeting}

@@ -78,22 +99,21 @@ To make `routes/sub/special.tsx` opt out of rendering layouts we can set `skipInheritedLayouts: true`. ```tsx routes/sub/special.tsx -import { type RouteConfig } from "fresh"; -import { define } from "../../utils.ts"; +import { RouteConfig } from "$fresh/server.ts"; export const config: RouteConfig = { skipInheritedLayouts: true, // Skip already inherited layouts }; -export default define.layout(() => { +export default function MyPage() { return

Hello world

; -}); +} ``` You can skip already inherited layouts inside a layout file: ```tsx routes/special/_layout.tsx -import { type LayoutConfig } from "fresh"; +import { LayoutConfig } from "$fresh/server.ts"; export const config: LayoutConfig = { skipInheritedLayouts: true, // Skip already inherited layouts diff --git a/docs/1.x/concepts/middleware.md b/docs/1.x/concepts/middleware.md new file mode 100644 index 00000000..549cc8eb --- /dev/null +++ b/docs/1.x/concepts/middleware.md @@ -0,0 +1,222 @@ +--- +description: | + Add middleware routes to intercept requests or responses for analytics purposes, access control, or anything else. +--- + +A middleware is defined in a `_middleware.ts` file. It will intercept the +request in order for you to perform custom logic before or after the route +handler. This allows modifying or checking requests and responses. Common +use-cases for this are logging, authentication, and performance monitoring. + +Each middleware gets passed a `next` function in the context argument that is +used to trigger child handlers. The `ctx` also has a `state` property that can +be used to pass arbitrary data to downstream (or upstream) handlers. This +`state` is included in `PageProps` by default, which is available to both the +special [\_app](/docs/1.x/concepts/app-wrapper.md) wrapper and normal +[routes](/docs/1.x/concepts/routes.md). `ctx.state` is normally set by modifying +its properties, e.g. `ctx.state.loggedIn = true`, but you can also replace the +entire object like `ctx.state = { loggedIn: true }`. + +```ts routes/_middleware.ts +import { FreshContext } from "$fresh/server.ts"; + +interface State { + data: string; +} + +export async function handler( + req: Request, + ctx: FreshContext, +) { + ctx.state.data = "myData"; + const resp = await ctx.next(); + resp.headers.set("server", "fresh server"); + return resp; +} +``` + +```ts routes/myHandler.ts +export const handler: Handlers = { + GET(_req, ctx) { + return new Response(`middleware data is ${ctx.state.data}`); + }, +}; +``` + +Middlewares are scoped and can be layered. This means a project can have +multiple middlewares, each covering a different set of routes. If multiple +middlewares cover a route, they will all be run, in order of specificity (least +specific first). + +For example, take a project with the following routes: + +```txt-files Project Structure +└── /routes +    ├── _middleware.ts +    ├── index.ts +    └── admin +       ├── _middleware.ts +       └── index.ts +       └── signin.ts +``` + +For a request to `/` the request will flow like this: + +1. The `routes/_middleware.ts` middleware is invoked. +2. Calling `ctx.next()` will invoke the `routes/index.ts` handler. + +For a request to `/admin` the request flows like this: + +1. The `routes/_middleware.ts` middleware is invoked. +2. Calling `ctx.next()` will invoke the `routes/admin/_middleware.ts` + middleware. +3. Calling `ctx.next()` will invoke the `routes/admin/index.ts` handler. + +For a request to `/admin/signin` the request flows like this: + +1. The `routes/_middleware.ts` middleware is invoked. +2. Calling `ctx.next()` will invoke the `routes/admin/_middleware.ts` + middleware. +3. Calling `ctx.next()` will invoke the `routes/admin/signin.ts` handler. + +A single middleware file can also define multiple middlewares (all for the same +route) by exporting an array of handlers instead of a single handler. For +example: + +```ts routes/_middleware.ts +export const handler = [ + async function middleware1(req, ctx) { + // do something + return ctx.next(); + }, + async function middleware2(req, ctx) { + // do something + return ctx.next(); + }, +]; +``` + +It should be noted that `middleware` has access to route parameters. If you're +running a fictitious `routes/[tenant]/admin/_middleware.ts` like this: + +```ts routes/[tenant]/admin/_middleware.ts +import { FreshContext } from "$fresh/server.ts"; + +export async function handler(_req: Request, ctx: FreshContext) { + const currentTenant = ctx.params.tenant; + // do something with the tenant + const resp = await ctx.next(); + return resp; +} +``` + +and the request is to `mysaas.com/acme/admin/`, then `currentTenant` will have +the value of `acme` in your middleware. + +## Middleware Destination + +To set the stage for this section, let's focus on the part of `FreshContext` +that looks like this: + +```ts fresh 🍋 +export interface FreshContext> { + ... + next: () => Promise; + state: State; + destination: router.DestinationKind; + remoteAddr: { + transport: "tcp" | "udp"; + hostname: string; + port: number; + }; + ... +} +``` + +and `router.DestinationKind` is defined like this: + +```ts fresh 🍋 +export type DestinationKind = "internal" | "static" | "route" | "notFound"; +``` + +This is useful if you want your middleware to only run when a request is headed +for a `route`, as opposed to something like `http://localhost:8001/favicon.ico`. + +### Example + +Initiate a new Fresh project (`deno run -A -r https://fresh.deno.dev/`) and then +create a `_middleware.ts` file in the `routes` folder like this: + +```ts routes/_middleware.ts +import { FreshContext } from "$fresh/server.ts"; + +export async function handler(req: Request, ctx: FreshContext) { + console.log(ctx.destination); + console.log(req.url); + const resp = await ctx.next(); + return resp; +} +``` + +If you start up your server (`deno task start`) you'll see the following: + +```sh Terminal +Task start deno run -A --watch=static/,routes/ dev.ts +Watcher Process started. +The manifest has been generated for 4 routes and 1 islands. + + 🍋 Fresh ready + Local: http://localhost:8000/ + +route +http://localhost:8000/ +internal +http://localhost:8000/_frsh/js/3c7400558fc00915df88cb181036c0dbf73ab7f5/deserializer.js +internal +http://localhost:8000/_frsh/js/3c7400558fc00915df88cb181036c0dbf73ab7f5/signals.js +internal +http://localhost:8000/_frsh/js/3c7400558fc00915df88cb181036c0dbf73ab7f5/plugin-twind-main.js +internal +http://localhost:8000/_frsh/js/3c7400558fc00915df88cb181036c0dbf73ab7f5/main.js +internal +http://localhost:8000/_frsh/js/3c7400558fc00915df88cb181036c0dbf73ab7f5/island-counter.js +internal +http://localhost:8000/_frsh/refresh.js +static +http://localhost:8000/logo.svg?__frsh_c=3c7400558fc00915df88cb181036c0dbf73ab7f5 +internal +http://localhost:8000/_frsh/alive +internal +http://localhost:8000/_frsh/js/3c7400558fc00915df88cb181036c0dbf73ab7f5/chunk-PDMKJVJ5.js +internal +http://localhost:8000/_frsh/js/3c7400558fc00915df88cb181036c0dbf73ab7f5/chunk-UGFDDSOV.js +internal +http://localhost:8000/_frsh/js/3c7400558fc00915df88cb181036c0dbf73ab7f5/chunk-RCK7U3UF.js +``` + +That first `route` request is for when `Fresh` responds with the root level +`index.tsx` route. The rest, as you can see, are either `internal` or `static` +requests. You can use `ctx.destination` to filter these out if your middleware +is only supposed to deal with routes. + +## Middleware Redirects + +If you want to redirect a request from a middleware, you can do so by returning: + +```ts routes/_middleware.ts +export function handler(req: Request): Response { + return Response.redirect("https://example.com", 307); +} +``` + +`307` stands for temporary redirect. You can also use `301` for permanent +redirect. You can also redirect to a relative path by doing: + +```ts routes/_middleware.ts +export function handler(req: Request): Response { + return new Response("", { + status: 307, + headers: { Location: "/my/new/relative/path" }, + }); +} +``` diff --git a/docs/latest/concepts/partials.md b/docs/1.x/concepts/partials.md similarity index 99% rename from docs/latest/concepts/partials.md rename to docs/1.x/concepts/partials.md index 1b697dcf..aa8af04a 100644 --- a/docs/latest/concepts/partials.md +++ b/docs/1.x/concepts/partials.md @@ -76,7 +76,7 @@ Let's use a typical documentation page layout as an example. It often features a main content area and a sidebar of links to switch between pages of the documentation (marked green here). -![A sketched layout of a typical documentation page with the sidebar on the left composed of green links and a main content area on the right. The main content area is labeled as Partial docs-content](/docs/fresh-partial-docs.png) +![A sketched layout of a typical documentation page with the sidebar on the left composed of green links and a main content area on the right. The main content area is labeled as Partial docs-content](/docs/1.x/fresh-partial-docs.png) The code for such a page (excluding styling) might look like this: diff --git a/docs/latest/concepts/plugins.md b/docs/1.x/concepts/plugins.md similarity index 95% rename from docs/latest/concepts/plugins.md rename to docs/1.x/concepts/plugins.md index fdc39278..15202631 100644 --- a/docs/latest/concepts/plugins.md +++ b/docs/1.x/concepts/plugins.md @@ -130,7 +130,7 @@ may be provided to Fresh's `start` entrypoint in the `main.ts` file. ### Hook: `buildStart` This hook is run at the start of the Fresh -[ahead-of-time build task](/docs/concepts/ahead-of-time-builds). It may be +[ahead-of-time build task](/docs/1.x/concepts/ahead-of-time-builds). It may be synchronous or asynchronous. The build start hook is called with the @@ -139,25 +139,25 @@ object, which contains the full Fresh configuration. This hook may be used to generate precompiled static assets. Any files saved to the `static` subfolder of `config.build.outDir` (typically `_fresh`) will be -served the same as other [static files](/docs/concepts/static-files). +served the same as other [static files](/docs/1.x/concepts/static-files). ### Hook: `buildEnd` This hook is run at the end of the Fresh -[ahead-of-time build task](/docs/concepts/ahead-of-time-builds). It may be +[ahead-of-time build task](/docs/1.x/concepts/ahead-of-time-builds). It may be synchronous or asynchronous. ### Routes and Middlewares You can create routes and middlewares that get loaded and rendered like the -normal [routes](/docs/concepts/routes) and -[middlewares](/docs/concepts/middleware). +normal [routes](/docs/1.x/concepts/routes) and +[middlewares](/docs/1.x/concepts/middleware). The plugin routes and middlewares need a defined path in the format of a file name without a filetype inside the routes directory(E.g. `blog/index`, `blog/[slug]`). -For more examples see the [Concepts: Routing](/docs/concepts/routing) page. +For more examples see the [Concepts: Routing](/docs/1.x/concepts/routing) page. To create a middleware you need to create a `MiddlewareHandler` function. diff --git a/docs/latest/concepts/routes.md b/docs/1.x/concepts/routes.md similarity index 100% rename from docs/latest/concepts/routes.md rename to docs/1.x/concepts/routes.md diff --git a/docs/1.x/concepts/routing.md b/docs/1.x/concepts/routing.md new file mode 100644 index 00000000..f758833c --- /dev/null +++ b/docs/1.x/concepts/routing.md @@ -0,0 +1,151 @@ +--- +description: | + File based routing is the simplest way to do routing in Fresh apps. Additionally custom patterns can be configured per route. +--- + +Routing is the mechanism that determines what route a given incoming request is +handled by. Fresh routes requests based on their URL path. By default routes +specify which paths they are invoked for using the name of the file. Routes can +also define a custom [URL pattern][urlpattern] to match against for more +advanced use cases. + +The file based routing in Fresh is very similar to the file based routing seen +in other frameworks, namely Next.js. File names are used to determine which +route a given request should be handled by. The pattern is determined based on +the path of the file on disk, relative to the `routes/` directory. + +File names are mapped to route patterns as follows: + +- File extensions are ignored. +- Literals in the file path are treated as string literals to match. +- Files named `/index.` behave identically to a file named + `.`. +- Path segments can be made dynamic by surrounding an identifier with `[` and + `]`. +- Paths where the last path segment follows the structure `[...]` are + treated as having a wildcard suffix. + +Here is a table of file names, which route patterns they map to, and which paths +they might match: + +| File name | Route pattern | Matching paths | +| --------------------------- | ---------------------- | --------------------------------------- | +| `index.ts` | `/` | `/` | +| `about.ts` | `/about` | `/about` | +| `blog/index.ts` | `/blog` | `/blog` | +| `blog/[slug].ts` | `/blog/:slug` | `/blog/foo`, `/blog/bar` | +| `blog/[slug]/comments.ts` | `/blog/:slug/comments` | `/blog/foo/comments` | +| `old/[...path].ts` | `/old/:path*` | `/old/foo`, `/old/bar/baz` | +| `docs/[[version]]/index.ts` | `/docs{/:version}?` | `/docs`, `/docs/latest`, `/docs/canary` | + +Advanced use-cases can require that a more complex pattern be used for matching. +A custom [URL pattern][urlpattern] can be specified in the route configuration. +This pattern will be used instead of the file path based pattern: + +```ts routes/x.ts +import { RouteConfig } from "$fresh/server.ts"; + +export const config: RouteConfig = { + routeOverride: "/x/:module@:version/:path*", +}; + +// ... +``` + +## Route Groups + +When working with [layouts](/docs/1.x/concepts/layouts) or +[middlewares](/docs/1.x/concepts/middleware), you'll sometimes come across a +situation where you want your routes to inherit from a layout other than what's +suggested by the URL segment. + +Let's illustrate that with an example: + +```txt Example page layout +/about -> layout A +/career -> layout A +/archive -> layout B +/contact -> layout B +``` + +Without any way to group routes this is a problem because every route segment +can only have one `_layout` file. + +```txt-files Project structure + +└── routes +    ├── _layout.tsx # applies to all routes here :( +    ├── about.tsx +    ├── career.tsx +    ├── archive.tsx +    └── contact.tsx +``` + +We can solve this problem with route groups. A route group is a folder which has +a name that is wrapped in parentheses. For example `(info)` would be considered +a route group and so would `(marketing)`. This enables us to group related +routes in a folder and use a different `_layout` file for each group. + +```txt-files Project structure +└── /routes +    ├── (marketing) +    │ ├── _layout.tsx # only applies to about.tsx and career.tsx +    │ ├── about.tsx +    │ └── career.tsx +    └── (info) +    ├── _layout.tsx # only applies to archive.tsx and contact.tsx +    ├── archive.tsx +    └── contact.tsx +``` + +> [warn]: Be careful about routes in different groups which match to the same +> URL. Such scenarios will lead to ambiguity as to which route file should be +> picked. +> +> ```txt-files Project structure +> └── /routes +>    ├── (group-1) +>    │ └── about.tsx # Bad: Maps to same `/about` url +>    └── (group-2) +>    └── about.tsx # Bad: Maps to same `/about` url +> ``` + +[urlpattern]: https://developer.mozilla.org/en-US/docs/Web/API/URL_Pattern_API + +## Co-location + +If you want to store components and islands closer to their routes, you may want +to use co-location. + +When the name of a route group folder starts with an underscore, like +`(_components)`, Fresh will ignore that folder and it’s effectively treated as +private. This means you can use these private route folders to store components +related to a particular route. + +Following the above example, say you have some components you only want to use +in your marketing pages, you could create a route group folder `(_components)` +to house these. + +The one special name is `(_islands)` which tells Fresh to treat all files in +that folder as an island. + +```txt-files Project structure +└── /routes +    ├── (marketing) +    │ ├── _layout.tsx +    │ ├── about.tsx +    │ ├── career.tsx + │ ├── (_components) +    │ │ └── newsletter-cta.tsx + │ └── (_islands) +    │ └── interactive-stats.tsx # Fresh treats this as an island +    └── shop + ├── (_components) +    │ └── product-card.tsx + └── (_islands) +    └── cart.tsx # Fresh treats this as an island +``` + +Combined together, this gives you the ability to organise your code on a feature +basis and put all related components, islands or anything else into a shared +folder. diff --git a/docs/1.x/concepts/server-components.md b/docs/1.x/concepts/server-components.md new file mode 100644 index 00000000..64815375 --- /dev/null +++ b/docs/1.x/concepts/server-components.md @@ -0,0 +1,17 @@ +--- +description: | + Fresh's architecture is designed to leverage server components by default. +--- + +If you've read about Fresh's [architecture](/docs/1.x/concepts/architecture) +then you know that it's based on the islands architecture pattern. The flip side +of this is that everything else is, by default, a server component. When you +[create a route](/docs/1.x/getting-started/create-a-route), all of the +components used are rendered on the server. No JavaScript is sent to the client, +unless you specifically include something from the `/islands/` folder. + +Internally, Fresh's rendering heavily leverages +[preact-render-to-string](https://github.com/preactjs/preact-render-to-string). +This is the exact library mentioned on Preact's +[Server-Side Rendering](https://preactjs.com/guide/v10/server-side-rendering/) +article. diff --git a/docs/latest/concepts/server-configuration.md b/docs/1.x/concepts/server-configuration.md similarity index 97% rename from docs/latest/concepts/server-configuration.md rename to docs/1.x/concepts/server-configuration.md index bd8af9e6..6afe752b 100644 --- a/docs/latest/concepts/server-configuration.md +++ b/docs/1.x/concepts/server-configuration.md @@ -103,8 +103,8 @@ await dev(import.meta.url, "./main.ts", { ## Plugins -See the [docs](/docs/concepts/plugins) on this topic for more detail. But as a -quick example, you can do something like this to load plugins: +See the [docs](/docs/1.x/concepts/plugins) on this topic for more detail. But as +a quick example, you can do something like this to load plugins: ```ts main.ts await start(manifest, { plugins: [twindPlugin(twindConfig)] }); diff --git a/docs/1.x/concepts/static-files.md b/docs/1.x/concepts/static-files.md new file mode 100644 index 00000000..5af5d2d1 --- /dev/null +++ b/docs/1.x/concepts/static-files.md @@ -0,0 +1,55 @@ +--- +description: | + Fresh has built-in support for serving static files. This is useful for serving images, CSS, and other static assets. +--- + +Fresh automatically serves static assets placed in a `static/` directory in the +project root. These assets are served at the root of the webserver, with a +higher priority than routes. This means that if a given request matches a file +in the `static/` folder, it is always served, even if there is a route that +would also match the request. + +Static asset responses automatically get a `content-type` header assigned based +on the file extension of the file on disk. Assets are also automatically +streamed from disk to the client to improve performance and efficiency for both +user and server. + +Fresh also adds an `etag` header to assets automatically and handles the +`If-None-Match` header for incoming requests. + +### Caching + +By default, no caching headers are added to assets. This can be disadvantageous +in many scenarios, so Fresh makes it easy to serve assets with long cache +lifetimes too. + +The first approach to do this is manual. The client runtime exports an `asset` +function that takes an absolute path to the static asset and returns a "locked" +version of this path that contains a build ID for cache busting. When the asset +is requested at this "locked" path, it will be served with a cache lifetime of +one year. + +```tsx routes/page.tsx +import { asset } from "$fresh/runtime.ts"; + +export default function Page() { + return ( +

+ View brochure +

+ ); +} +``` + +Fresh also does this automatically for `src` and `srcset` attributes in `` +and `` HTML tags. These will automatically use "locked" paths if Fresh +deems it safe to do so. You can always opt out of this behaviour per tag, by +adding the `data-fresh-disable-lock` attribute. + +```tsx routes/user.tsx +{/* Locked URL source */} +; + +{/* Preserve URL source and disable lock */} +; +``` diff --git a/docs/latest/concepts/updating.md b/docs/1.x/concepts/updating.md similarity index 100% rename from docs/latest/concepts/updating.md rename to docs/1.x/concepts/updating.md diff --git a/docs/1.x/examples/active-links.md b/docs/1.x/examples/active-links.md new file mode 100644 index 00000000..df3727d2 --- /dev/null +++ b/docs/1.x/examples/active-links.md @@ -0,0 +1,79 @@ +--- +description: | + Style active links with ease in Fresh +--- + +Fresh automatically enhances the accessibility of `` elements by adding the +aria-current attribute when rendering links that match the current URL. This +attribute is recognized by assistive technologies and clearly indicates the +current page within a set of pages. + +- `aria-current="page"` - Added to links with an exact path match, enhancing + accessibility by indicating the current page to assistive technologies. + +As we aim to improve accessibility, we encourage the use of `aria-current` for +styling current links where applicable. + +## Styling with CSS + +The `aria-current` attribute is easily styled with CSS using attribute +selectors, providing a native way to visually differentiate the active link. + +```css static/styles.css +/* Give links pointing to the current page a green color */ +a[aria-current="page"] { + color: green; +} + +/* Color all ancestor links of the current page */ +a[aria-current="true"] { + color: peachpuff; +} +``` + +## Tailwind / Twind + +In Tailwind or similar CSS frameworks like Twind, you can apply styles to +elements with the aria-current attribute using bracket notation in your class +definitions. However, the specific syntax varies slightly between Tailwind and +Twind. For Tailwind, use the syntax: + +```tsx component/Menu.tsx +function Menu() { + return ( + + Link to some page + + ); +} +``` + +For Twind, the syntax is: + +```tsx component/Menu.tsx +function Menu() { + return ( + + Link to some page + + ); +} +``` + +### Twind Plugin + +The original twind plugin (`import twindPlugin from "$fresh/plugins/twind.ts";`) +supports the above style: + +```tsx routes/page.tsx +class="[aria-current='page']:text-green-600" +``` + +### TwindV1 Plugin + +The new twind plugin (`import twindPlugin from "$fresh/plugins/twindv1.ts";`) +requires a slightly different syntax (note the position of the left bracket): + +```tsx routes/page.tsx +class="aria-[current='page']:text-green-600" +``` diff --git a/docs/latest/examples/authentication-with-supabase.md b/docs/1.x/examples/authentication-with-supabase.md similarity index 98% rename from docs/latest/examples/authentication-with-supabase.md rename to docs/1.x/examples/authentication-with-supabase.md index a80b1021..43b93a23 100644 --- a/docs/latest/examples/authentication-with-supabase.md +++ b/docs/1.x/examples/authentication-with-supabase.md @@ -24,7 +24,7 @@ The purpose of the example app we're building here is to showcase the basic building blocks of an implementation. As such, it is limited in functionality and purposefully leaves out things like [password resets](https://supabase.com/docs/guides/auth/server-side/email-based-auth-with-pkce-flow-for-ssr), -[proper error handling](https://fresh.deno.dev/docs/concepts/error-pages) as +[proper error handling](https://fresh.deno.dev/docs/1.x/concepts/error-pages) as well as validating input form data. You can find the [full code here](https://github.com/morlinbrot/supa-fresh-pkce), where the missing functionality is implemented. @@ -226,7 +226,7 @@ we are writing next. We can now write a middleware that will check the auth status of any request, guarding any protected routes. You can read up on middlewares and where to put -them [in the docs](https://fresh.deno.dev/docs/concepts/middleware). +them [in the docs](https://fresh.deno.dev/docs/1.x/concepts/middleware). ```ts routes/_middleware.ts import { FreshContext } from "$fresh/server.ts"; diff --git a/docs/latest/examples/changing-the-src-dir.md b/docs/1.x/examples/changing-the-src-dir.md similarity index 100% rename from docs/latest/examples/changing-the-src-dir.md rename to docs/1.x/examples/changing-the-src-dir.md diff --git a/docs/latest/examples/client-side-components-and-libraries.md b/docs/1.x/examples/client-side-components-and-libraries.md similarity index 100% rename from docs/latest/examples/client-side-components-and-libraries.md rename to docs/1.x/examples/client-side-components-and-libraries.md diff --git a/docs/latest/examples/creating-a-crud-api.md b/docs/1.x/examples/creating-a-crud-api.md similarity index 99% rename from docs/latest/examples/creating-a-crud-api.md rename to docs/1.x/examples/creating-a-crud-api.md index 9d6a22e2..932d9352 100644 --- a/docs/latest/examples/creating-a-crud-api.md +++ b/docs/1.x/examples/creating-a-crud-api.md @@ -170,7 +170,7 @@ user 2 deleted Options can be used for some advanced cases, including implementing preflight request checks for complex CORS use cases. See more on the -[CORS documentation](/docs/examples/dealing-with-cors). +[CORS documentation](/docs/1.x/examples/dealing-with-cors). ## Full File Reference diff --git a/docs/latest/examples/dealing-with-cors.md b/docs/1.x/examples/dealing-with-cors.md similarity index 97% rename from docs/latest/examples/dealing-with-cors.md rename to docs/1.x/examples/dealing-with-cors.md index 17b24119..4219f8c8 100644 --- a/docs/latest/examples/dealing-with-cors.md +++ b/docs/1.x/examples/dealing-with-cors.md @@ -89,4 +89,4 @@ requests. Of course there's no reason why you need to use middleware in order to solve this. The headers can be set directly in the -[handler](/docs/getting-started/custom-handlers) as well. +[handler](/docs/1.x/getting-started/custom-handlers) as well. diff --git a/docs/latest/examples/handling-complex-routes.md b/docs/1.x/examples/handling-complex-routes.md similarity index 92% rename from docs/latest/examples/handling-complex-routes.md rename to docs/1.x/examples/handling-complex-routes.md index 1e7d0f4d..98ffc1e0 100644 --- a/docs/latest/examples/handling-complex-routes.md +++ b/docs/1.x/examples/handling-complex-routes.md @@ -3,8 +3,8 @@ description: | Sometimes URL based routing isn't enough. --- -The page on [routing](/docs/concepts/routing) hints at complex routing based on -URL patterns using a `RouteConfig` object. Let's dive into this in a bit more +The page on [routing](/docs/1.x/concepts/routing) hints at complex routing based +on URL patterns using a `RouteConfig` object. Let's dive into this in a bit more detail. A `RouteConfig` has a `routeOverride` string property, which makes use of the diff --git a/docs/latest/examples/index.md b/docs/1.x/examples/index.md similarity index 100% rename from docs/latest/examples/index.md rename to docs/1.x/examples/index.md diff --git a/docs/latest/examples/init-the-server.md b/docs/1.x/examples/init-the-server.md similarity index 100% rename from docs/latest/examples/init-the-server.md rename to docs/1.x/examples/init-the-server.md diff --git a/docs/latest/examples/migrating-to-tailwind.md b/docs/1.x/examples/migrating-to-tailwind.md similarity index 98% rename from docs/latest/examples/migrating-to-tailwind.md rename to docs/1.x/examples/migrating-to-tailwind.md index b3cbbfcf..47edb39b 100644 --- a/docs/latest/examples/migrating-to-tailwind.md +++ b/docs/1.x/examples/migrating-to-tailwind.md @@ -10,7 +10,7 @@ will now install the Tailwind CSS plugin instead of twind like it did before. ## Requirements before migrating The tailwind plugin requires Fresh's -[ahead of time builds](/docs/concepts/ahead-of-time-builds) to be set up, +[ahead of time builds](/docs/1.x/concepts/ahead-of-time-builds) to be set up, otherwise it won't work. Make sure to switch your projects to ahead of time builds in your project before continuing this guide. If your project is already configured to use ahead of time builds, then you're good to go. diff --git a/docs/latest/examples/modifying-the-head.md b/docs/1.x/examples/modifying-the-head.md similarity index 100% rename from docs/latest/examples/modifying-the-head.md rename to docs/1.x/examples/modifying-the-head.md diff --git a/docs/latest/examples/rendering-markdown.md b/docs/1.x/examples/rendering-markdown.md similarity index 100% rename from docs/latest/examples/rendering-markdown.md rename to docs/1.x/examples/rendering-markdown.md diff --git a/docs/1.x/examples/rendering-raw-html.md b/docs/1.x/examples/rendering-raw-html.md new file mode 100644 index 00000000..b7f8bd06 --- /dev/null +++ b/docs/1.x/examples/rendering-raw-html.md @@ -0,0 +1,83 @@ +--- +description: | + How to render raw HTML in Fresh. +--- + +Text content in Fresh is always escaped, whether serverside rendered or rendered +in islands. While this generally desired, it can create issues in certain +situations. + +## Warning + +The TL;DR is to use Preact's `dangerouslySetInnerHTML`. As the name implies, it +should not be used lightly. + +Setting arbitrary HTML can be dangerous. Make sure you trust the source. +Rendering user-supplied HTML to the DOM makes your site vulnerable to cross- +site scripting. The markup must first be sanitizied, or better yet, something +you trust. + +## Example: Rendering JSON-LD + +Suppose we need to add some microdata markup to a page. The following will +result in **escaped characters, and will not work**: + +```tsx components/json-ld.tsx +const json = ` +{ + "@context": "http://schema.org", + "@type": "PostalAddress", + "streetAddress": "8888 University Drive", + "addressLocality": "Burnaby", + "addressRegion": "British Columbia" +} +`; + +export default function JsonLd() { + return ; +} +``` + +Instead, we can use `dangerouslySetInnerHTML`: + +```tsx components/json-ld.tsx +export default function JsonLd() { + return ( + ; -} +```tsx routes/dynamic-html.tsx +
This is raw HTML" }} />; ``` -Instead, we can use `dangerouslySetInnerHTML`: +This will output: -```tsx components/json-ld.tsx -export default function JsonLd() { - return ( -