docs: add correct ctx.state example to init and docs (#3147)

- Correct outdated, non-functional and confusing "Removal of <Head>
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`
This commit is contained in:
Roscoe Rubin-Rottenberg
2025-08-06 00:17:24 +02:00
committed by GitHub
parent b7f1abb0d0
commit a5205b2a8e
3 changed files with 38 additions and 25 deletions
+26 -15
View File
@@ -128,33 +128,44 @@ The `<Head>` 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<State>();
```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 (
<div>
<h1>About Me</h1>
</div>
);
});
// Render that in _app.tsx
export default define.page(function App({ Component, state }) {
return (
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
{ctx.state.title ? <title>{ctx.state.title}</title> : null}
{state.title ? <title>{state.title}</title> : null}
</head>
<body>
<ctx.Component />
<Component />
</body>
</html>
);
}
});
```
## Update deployment settings