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:
@@ -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.
|
||||
|
||||
@@ -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
|
||||
|
||||
+11
-7
@@ -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<State>();`;
|
||||
await writeFile("utils.ts", UTILS_TS);
|
||||
@@ -410,9 +411,12 @@ export const define = createDefine<State>();`;
|
||||
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 (
|
||||
<div class="px-4 py-8 mx-auto fresh-gradient min-h-screen">
|
||||
<div class="max-w-screen-md mx-auto flex flex-col items-center justify-center">
|
||||
@@ -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 (
|
||||
<html>
|
||||
<head>
|
||||
<meta charset="utf-8" />
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
||||
<title>${path.basename(projectDir)}</title>
|
||||
<title>{state.title ?? "${path.basename(projectDir)}"}</title>
|
||||
<link rel="stylesheet" href="/styles.css" />
|
||||
</head>
|
||||
<body>
|
||||
@@ -451,7 +455,7 @@ export default function App({ Component }: PageProps) {
|
||||
</body>
|
||||
</html>
|
||||
);
|
||||
}`;
|
||||
});`;
|
||||
await writeFile("routes/_app.tsx", APP_WRAPPER);
|
||||
|
||||
const API_NAME = `import { define } from "../../utils.ts";
|
||||
|
||||
Reference in New Issue
Block a user