fix: bring back island module preloading (#2503)
This commit is contained in:
@@ -14,6 +14,7 @@ import {
|
|||||||
setRenderState,
|
setRenderState,
|
||||||
} from "./runtime/server/preact_hooks.tsx";
|
} from "./runtime/server/preact_hooks.tsx";
|
||||||
import { DEV_ERROR_OVERLAY_URL } from "./constants.ts";
|
import { DEV_ERROR_OVERLAY_URL } from "./constants.ts";
|
||||||
|
import { asset } from "./runtime/shared.ts";
|
||||||
|
|
||||||
export interface Island {
|
export interface Island {
|
||||||
file: string | URL;
|
file: string | URL;
|
||||||
@@ -188,6 +189,7 @@ export class FreshReqContext<State>
|
|||||||
this.#islandRegistry,
|
this.#islandRegistry,
|
||||||
this.#buildCache,
|
this.#buildCache,
|
||||||
partialId,
|
partialId,
|
||||||
|
headers,
|
||||||
);
|
);
|
||||||
return new Response(html, responseInit);
|
return new Response(html, responseInit);
|
||||||
}
|
}
|
||||||
@@ -211,6 +213,7 @@ function preactRender<State, Data>(
|
|||||||
islandRegistry: ServerIslandRegistry,
|
islandRegistry: ServerIslandRegistry,
|
||||||
buildCache: BuildCache,
|
buildCache: BuildCache,
|
||||||
partialId: string,
|
partialId: string,
|
||||||
|
headers: Headers,
|
||||||
) {
|
) {
|
||||||
const state = new RenderState(ctx, islandRegistry, buildCache, partialId);
|
const state = new RenderState(ctx, islandRegistry, buildCache, partialId);
|
||||||
setRenderState(state);
|
setRenderState(state);
|
||||||
@@ -234,6 +237,23 @@ function preactRender<State, Data>(
|
|||||||
|
|
||||||
return `<!DOCTYPE html>${res}`;
|
return `<!DOCTYPE html>${res}`;
|
||||||
} finally {
|
} 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();
|
state.clear();
|
||||||
setRenderState(null);
|
setRenderState(null);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -30,6 +30,7 @@ import { setBuildCache } from "../src/app.ts";
|
|||||||
import { getBuildCache } from "../src/app.ts";
|
import { getBuildCache } from "../src/app.ts";
|
||||||
import type { FreshConfig } from "../src/config.ts";
|
import type { FreshConfig } from "../src/config.ts";
|
||||||
import { FreshAttrs } from "./fixtures_islands/FreshAttrs.tsx";
|
import { FreshAttrs } from "./fixtures_islands/FreshAttrs.tsx";
|
||||||
|
import { FakeServer } from "../src/test_utils.ts";
|
||||||
|
|
||||||
await buildProd(allIslandApp);
|
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) {
|
if (!found) {
|
||||||
throw new Error(`Could not find server addrews`);
|
throw new Error(`Could not find server address`);
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
|||||||
Reference in New Issue
Block a user