fix: bring back island module preloading (#2503)
This commit is contained in:
@@ -14,6 +14,7 @@ import {
|
||||
setRenderState,
|
||||
} from "./runtime/server/preact_hooks.tsx";
|
||||
import { DEV_ERROR_OVERLAY_URL } from "./constants.ts";
|
||||
import { asset } from "./runtime/shared.ts";
|
||||
|
||||
export interface Island {
|
||||
file: string | URL;
|
||||
@@ -188,6 +189,7 @@ export class FreshReqContext<State>
|
||||
this.#islandRegistry,
|
||||
this.#buildCache,
|
||||
partialId,
|
||||
headers,
|
||||
);
|
||||
return new Response(html, responseInit);
|
||||
}
|
||||
@@ -211,6 +213,7 @@ function preactRender<State, Data>(
|
||||
islandRegistry: ServerIslandRegistry,
|
||||
buildCache: BuildCache,
|
||||
partialId: string,
|
||||
headers: Headers,
|
||||
) {
|
||||
const state = new RenderState(ctx, islandRegistry, buildCache, partialId);
|
||||
setRenderState(state);
|
||||
@@ -234,6 +237,23 @@ function preactRender<State, Data>(
|
||||
|
||||
return `<!DOCTYPE html>${res}`;
|
||||
} finally {
|
||||
// Add preload headers
|
||||
const basePath = ctx.config.basePath;
|
||||
const runtimeUrl = asset(`${basePath}/fresh-runtime.js`);
|
||||
let link = `<${encodeURI(runtimeUrl)}>; rel="modulepreload"; as="script"`;
|
||||
state.islands.forEach((island) => {
|
||||
const chunk = buildCache.getIslandChunkName(island.name);
|
||||
if (chunk !== null) {
|
||||
link += `, <${
|
||||
encodeURI(asset(`${basePath}${chunk}`))
|
||||
}>; rel="modulepreload"; as="script"`;
|
||||
}
|
||||
});
|
||||
|
||||
if (link !== "") {
|
||||
headers.append("Link", link);
|
||||
}
|
||||
|
||||
state.clear();
|
||||
setRenderState(null);
|
||||
}
|
||||
|
||||
@@ -30,6 +30,7 @@ import { setBuildCache } from "../src/app.ts";
|
||||
import { getBuildCache } from "../src/app.ts";
|
||||
import type { FreshConfig } from "../src/config.ts";
|
||||
import { FreshAttrs } from "./fixtures_islands/FreshAttrs.tsx";
|
||||
import { FakeServer } from "../src/test_utils.ts";
|
||||
|
||||
await buildProd(allIslandApp);
|
||||
|
||||
@@ -659,3 +660,29 @@ Deno.test({
|
||||
});
|
||||
},
|
||||
});
|
||||
|
||||
Deno.test({
|
||||
name: "islands - adds preload HTTP headers",
|
||||
fn: async () => {
|
||||
const selfCounter = getIsland("SelfCounter.tsx");
|
||||
|
||||
const app = testApp()
|
||||
.use(staticFiles())
|
||||
.island(selfCounter, "SelfCounter", SelfCounter)
|
||||
.get("/", (ctx) =>
|
||||
ctx.render(
|
||||
<Doc>
|
||||
<SelfCounter />
|
||||
</Doc>,
|
||||
));
|
||||
|
||||
const server = new FakeServer(await app.handler());
|
||||
const res = await server.get("/");
|
||||
await res.body?.cancel();
|
||||
|
||||
const link = res.headers.get("Link");
|
||||
expect(link).toMatch(
|
||||
/<\/fresh-runtime\.js\?__frsh_c=[^>]+>; rel="modulepreload"; as="script", <\/SelfCounter\.js\?__frsh_c=[^>]+>; rel="modulepreload"; as="script"/,
|
||||
);
|
||||
},
|
||||
});
|
||||
|
||||
@@ -162,7 +162,7 @@ export async function withChildProcessServer(
|
||||
}
|
||||
|
||||
if (!found) {
|
||||
throw new Error(`Could not find server addrews`);
|
||||
throw new Error(`Could not find server address`);
|
||||
}
|
||||
|
||||
try {
|
||||
|
||||
Reference in New Issue
Block a user