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:
@@ -7,6 +7,7 @@ import {
|
||||
isValidElement,
|
||||
type VNode,
|
||||
} from "preact";
|
||||
import { jsxTemplate } from "preact/jsx-runtime";
|
||||
import { SpanStatusCode } from "@opentelemetry/api";
|
||||
import type { ResolvedFreshConfig } from "./config.ts";
|
||||
import type { BuildCache } from "./build_cache.ts";
|
||||
@@ -215,19 +216,23 @@ export class Context<State> {
|
||||
vnode = result;
|
||||
}
|
||||
|
||||
let appChild = vnode;
|
||||
// deno-lint-ignore no-explicit-any
|
||||
let appVNode: VNode<any>;
|
||||
|
||||
let hasApp = true;
|
||||
|
||||
if (isAsyncAnyComponent(appDef)) {
|
||||
const child = vnode;
|
||||
props.Component = () => child;
|
||||
props.Component = () => appChild;
|
||||
const result = await renderAsyncAnyComponent(appDef, props);
|
||||
if (result instanceof Response) {
|
||||
return result;
|
||||
}
|
||||
|
||||
vnode = result;
|
||||
appVNode = result;
|
||||
} else if (appDef !== null) {
|
||||
const child = vnode;
|
||||
vnode = h(appDef, {
|
||||
Component: () => child,
|
||||
appVNode = h(appDef, {
|
||||
Component: () => appChild,
|
||||
config: this.config,
|
||||
data: null,
|
||||
error: this.error,
|
||||
@@ -238,6 +243,9 @@ export class Context<State> {
|
||||
state: this.state,
|
||||
url: this.url,
|
||||
});
|
||||
} else {
|
||||
hasApp = false;
|
||||
appVNode = appChild ?? h(Fragment, null);
|
||||
}
|
||||
|
||||
const headers = init.headers !== undefined
|
||||
@@ -270,12 +278,22 @@ export class Context<State> {
|
||||
try {
|
||||
setRenderState(state);
|
||||
|
||||
return preactRender(
|
||||
const inner = preactRender(
|
||||
vnode ?? h(Fragment, null),
|
||||
this,
|
||||
state,
|
||||
headers,
|
||||
!hasApp,
|
||||
);
|
||||
|
||||
if (!hasApp) {
|
||||
return inner;
|
||||
}
|
||||
|
||||
appChild = jsxTemplate([inner]);
|
||||
|
||||
const outer = preactRender(appVNode, this, state, true);
|
||||
|
||||
return outer;
|
||||
} catch (err) {
|
||||
if (err instanceof Error) {
|
||||
span.recordException(err);
|
||||
@@ -287,6 +305,27 @@ export class Context<State> {
|
||||
}
|
||||
throw err;
|
||||
} finally {
|
||||
// Add preload headers
|
||||
const basePath = this.config.basePath;
|
||||
const runtimeUrl = state.buildCache.clientEntry.startsWith(".")
|
||||
? state.buildCache.clientEntry.slice(1)
|
||||
: state.buildCache.clientEntry;
|
||||
let link = `<${
|
||||
encodeURI(`${basePath}${runtimeUrl}`)
|
||||
}>; rel="modulepreload"; as="script"`;
|
||||
state.islands.forEach((island) => {
|
||||
const specifier = `${basePath}${
|
||||
island.file.startsWith(".") ? island.file.slice(1) : island.file
|
||||
}`;
|
||||
link += `, <${
|
||||
encodeURI(specifier)
|
||||
}>; rel="modulepreload"; as="script"`;
|
||||
});
|
||||
|
||||
if (link !== "") {
|
||||
headers.append("Link", link);
|
||||
}
|
||||
|
||||
state.clear();
|
||||
setRenderState(null);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user