Skip to content

Commit 2ca696d

Browse files
authored
Merge branch 'main' into rename-FreshContext-req-to-request
2 parents 0e3cf3b + f1fe325 commit 2ca696d

35 files changed

Lines changed: 993 additions & 355 deletions

deno.json

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,22 @@
8787
"lib": ["dom", "dom.asynciterable", "deno.ns", "deno.unstable"],
8888
"jsx": "precompile",
8989
"jsxImportSource": "preact",
90-
"jsxPrecompileSkipElements": ["a", "img", "source", "body", "html", "head"]
90+
"jsxPrecompileSkipElements": [
91+
"a",
92+
"img",
93+
"source",
94+
"body",
95+
"html",
96+
"head",
97+
"title",
98+
"meta",
99+
"script",
100+
"link",
101+
"style",
102+
"base",
103+
"noscript",
104+
"template"
105+
]
91106
},
92107
"lint": {
93108
"rules": {

deno.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docs/canary/advanced/head.md

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
---
2+
description: Modify the document head in Fresh
3+
---
4+
5+
The
6+
[`<head>`](https://developer.mozilla.org/en-US/docs/Web/HTML/Reference/Elements/head)-element
7+
is a crucial element in HTML to set metadata for a page. It allows you to:
8+
9+
- Set the document title with `<title>`
10+
- Specify page metadata with `<meta>`
11+
- Link to resources like stylesheets with `<link>`
12+
- Include JavaScript code with `<script>`
13+
14+
> [info]: The outer HTML structure including `<head>` is typically created
15+
> inside `_app.tsx`.
16+
17+
## Passing metadata from `ctx.state`
18+
19+
For simple scenarios passing metadata along from a handler or a middleware by
20+
writing to `ctx.state` is often sufficient.
21+
22+
```tsx routes/_app.tsx
23+
import { define } from "../util.ts";
24+
25+
export default define.page((ctx) => {
26+
return (
27+
<html lang="en">
28+
<head>
29+
<meta charset="utf-8" />
30+
<title>{ctx.state.title ?? "Welcome!"}</title>
31+
</head>
32+
<body>
33+
<ctx.Component />
34+
</body>
35+
</html>
36+
);
37+
});
38+
```
39+
40+
## Using the `<Head>`-component
41+
42+
For more complex scenarios, or to set page metadata from islands, Fresh ships
43+
with the `<Head>`-component.
44+
45+
```tsx routes/about.tsx
46+
import { Head } from "fresh/runtime";
47+
48+
export default define.page((ctx) => {
49+
return (
50+
<div>
51+
<Head>
52+
<title>About me</title>
53+
</Head>
54+
<h1>About me</h1>
55+
<p>I like Fresh!</p>
56+
</div>
57+
);
58+
});
59+
```
60+
61+
### Avoiding duplicate tags
62+
63+
You might end up with duplicate tags, when multiple `<Head />` components are
64+
rendered on the same page. Fresh will employ the following strategies to find
65+
the matching element:
66+
67+
1. For `<title>` elements Fresh will set `document.title` directly
68+
2. Check if an element with the same `key` exists
69+
3. Check if an element with the same `id` attribute
70+
4. Only for `<meta>` elements: Check if there is a `<meta>` element with the
71+
same `name` attribute
72+
5. No matching element was found, Fresh will create a new one and append it to
73+
`<head>`
74+
75+
> [info]: The `<title>`-tag is automatically deduplicated, even without a `key`
76+
> prop.

docs/canary/examples/migration-guide.md

Lines changed: 0 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -147,53 +147,6 @@ accordingly.
147147
}
148148
```
149149

150-
## Removal of `<Head>` component
151-
152-
The `<Head>` component was used in Fresh 1.x to add additional tags to the
153-
`<head>` portion of an HTML document from anywhere on the page. This feature was
154-
removed in preparation and due to performance concerns as it required a complex
155-
machinery in the background to work.
156-
157-
Instead, passing head-related data is best done via `ctx.state`, which can be
158-
easily set through the [define helper](/docs/canary/advanced/define).
159-
160-
```tsx
161-
// utils.ts
162-
export interface State {
163-
title?: string;
164-
}
165-
export const define = createDefine<State>();
166-
167-
// routes/about.tsx
168-
import { define } from "../utils.ts";
169-
170-
export default define.page(function AboutPage(ctx) {
171-
// Set a route specific data in a handler
172-
ctx.state.title = "About Me";
173-
return (
174-
<div>
175-
<h1>About Me</h1>
176-
</div>
177-
);
178-
});
179-
180-
// Render that in _app.tsx
181-
export default define.page(function App({ Component, state }) {
182-
return (
183-
<html lang="en">
184-
<head>
185-
<meta charset="utf-8" />
186-
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
187-
{state.title ? <title>{state.title}</title> : null}
188-
</head>
189-
<body>
190-
<Component />
191-
</body>
192-
</html>
193-
);
194-
});
195-
```
196-
197150
## Update deployment settings
198151

199152
Fresh 2 requires assets to be build during deployment instead of building them

docs/canary/examples/modifying-the-head.md

Lines changed: 0 additions & 195 deletions
This file was deleted.

docs/toc.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ const toc: RawTableOfContents = {
5454
["forms", "Forms", "link:canary"],
5555
["define", "Define Helpers", "link:canary"],
5656
["environment-variables", "Environment Variables", "link:canary"],
57+
["head", "Modifying <head>", "link:canary"],
5758
],
5859
},
5960
deployment: {
@@ -85,7 +86,6 @@ const toc: RawTableOfContents = {
8586
pages: [
8687
["migration-guide", "Migration Guide", "link:canary"],
8788
["daisyui", "daisyUI", "link:canary"],
88-
["modifying-the-head", "Modifying the <head>", "link:canary"],
8989
["creating-a-crud-api", "Creating a CRUD API", "link:latest"],
9090
["markdown", "Rendering Markdown", "link:canary"],
9191
["rendering-raw-html", "Rendering raw HTML", "link:canary"],

packages/fresh/deno.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@fresh/core",
3-
"version": "2.0.0-alpha.60",
3+
"version": "2.0.0-alpha.62",
44
"license": "MIT",
55
"exports": {
66
".": "./src/mod.ts",

0 commit comments

Comments
 (0)