feat: bring back <Head> component (#3249)

This PR brings back the `<Head>` component from Fresh 1.x . The
implementation is much cleaner than back then and also works client-side
now too.
This commit is contained in:
Marvin Hagemeister
2025-08-25 15:40:47 +02:00
committed by GitHub
parent bf25d10a84
commit 1daca75489
27 changed files with 916 additions and 326 deletions
-47
View File
@@ -147,53 +147,6 @@ accordingly.
}
```
## Removal of `<Head>` component
The `<Head>` component was used in Fresh 1.x to add additional tags to the
`<head>` portion of an HTML document from anywhere on the page. This feature was
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`, which can be
easily set through the [define helper](/docs/canary/advanced/define).
```tsx
// utils.ts
export interface State {
title?: string;
}
export const define = createDefine<State>();
// 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" />
{state.title ? <title>{state.title}</title> : null}
</head>
<body>
<Component />
</body>
</html>
);
});
```
## Update deployment settings
Fresh 2 requires assets to be build during deployment instead of building them