From a5205b2a8e87765a3e61b11a3fb88d2e578c84cb Mon Sep 17 00:00:00 2001 From: Roscoe Rubin-Rottenberg <118622417+knotbin@users.noreply.github.com> Date: Tue, 5 Aug 2025 18:17:24 -0400 Subject: [PATCH] docs: add correct ctx.state example to init and docs (#3147) - Correct outdated, non-functional and confusing "Removal of component" section of migration guide in the docs to be real functional code that uses Fresh 2 and define helpers correctly - Added example of ctx.state being used for a head component (title) because it's something almost every single project will need to know how to do. - Small change to define docs use of define.page to be more in line with its use commonly - Removed mention of deno-puppeteer - a non-official repository that hasn't been updated in three years - needing to be installed from `CONTRIBUTING.md` --- .github/CONTRIBUTING.md | 4 +-- docs/canary/examples/migration-guide.md | 41 ++++++++++++++++--------- init/src/init.ts | 18 ++++++----- 3 files changed, 38 insertions(+), 25 deletions(-) diff --git a/.github/CONTRIBUTING.md b/.github/CONTRIBUTING.md index 2fcb9cea..98186a54 100644 --- a/.github/CONTRIBUTING.md +++ b/.github/CONTRIBUTING.md @@ -2,6 +2,4 @@ ## Submitting a pull request -First, please be sure to -[install Puppeteer](https://github.com/lucacasonato/deno-puppeteer#installation). -Then, please ensure `deno task ok` is run and successfully passes. +First, please be sure to ensure `deno task ok` is run and successfully passes. diff --git a/docs/canary/examples/migration-guide.md b/docs/canary/examples/migration-guide.md index c2534a3b..de2a016b 100644 --- a/docs/canary/examples/migration-guide.md +++ b/docs/canary/examples/migration-guide.md @@ -128,33 +128,44 @@ The `` component was used in Fresh 1.x to add additional tags to the removed in preparation and due to performance concerns as it required a complex machinery in the background to work. -Instead, passing head-related data is best done via `ctx.state` +Instead, passing head-related data is best done via `ctx.state`, which can be +easily set through the [define helper](/docs/canary/advanced/define). -```tsx routes/about.tsx -export const handler = { - GET(ctx) { - // Set a route specific data in a handler - ctx.state.title = "About Me"; - return page(); - }, -}; -``` +```tsx +// utils.ts +export interface State { + title?: string; +} +export const define = createDefine(); -```tsx routes/_app.tsx -export default function AppWrapper(ctx: Context) { +// routes/about.tsx +import { define } from "../utils.ts"; + +export default define.page(function AboutPage(ctx) { + // Set a route specific data in a handler + ctx.state.title = "About Me"; + return ( +
+

About Me

+
+ ); +}); + +// Render that in _app.tsx +export default define.page(function App({ Component, state }) { return ( - {ctx.state.title ? {ctx.state.title} : null} + {state.title ? {state.title} : null} - + ); -} +}); ``` ## Update deployment settings diff --git a/init/src/init.ts b/init/src/init.ts index b60a2dc6..f1dac5cc 100644 --- a/init/src/init.ts +++ b/init/src/init.ts @@ -400,8 +400,9 @@ export function Button(props: ButtonProps) { const UTILS_TS = `import { createDefine } from "fresh"; -// deno-lint-ignore no-empty-interface -export interface State {} +export interface State { + title: string; +} export const define = createDefine();`; await writeFile("utils.ts", UTILS_TS); @@ -410,9 +411,12 @@ export const define = createDefine();`; import { define } from "../utils.ts"; import Counter from "../islands/Counter.tsx"; -export default define.page(function Home() { +export default define.page(function Home(ctx) { const count = useSignal(3); + ctx.state.title = count.value + " Fresh Counter" + + (Math.abs(count.value) === 1 ? "" : "s"); + return (
@@ -435,15 +439,15 @@ export default define.page(function Home() { });`; await writeFile("routes/index.tsx", ROUTES_HOME); - const APP_WRAPPER = `import type { PageProps } from "fresh"; + const APP_WRAPPER = `import { define } from "../utils.ts"; -export default function App({ Component }: PageProps) { +export default define.page(function App({ Component, state }) { return ( - ${path.basename(projectDir)} + {state.title ?? "${path.basename(projectDir)}"} @@ -451,7 +455,7 @@ export default function App({ Component }: PageProps) { ); -}`; +});`; await writeFile("routes/_app.tsx", APP_WRAPPER); const API_NAME = `import { define } from "../../utils.ts";