refactor: don't inline script if there are no islands (#101)
This commit is contained in:
@@ -8,12 +8,7 @@ import {
|
|||||||
} from "./deps.ts";
|
} from "./deps.ts";
|
||||||
import { Manifest } from "./mod.ts";
|
import { Manifest } from "./mod.ts";
|
||||||
import { Bundler } from "./bundle.ts";
|
import { Bundler } from "./bundle.ts";
|
||||||
import {
|
import { ALIVE_URL, INTERNAL_PREFIX, REFRESH_JS_URL } from "./constants.ts";
|
||||||
ALIVE_URL,
|
|
||||||
bundleAssetUrl,
|
|
||||||
INTERNAL_PREFIX,
|
|
||||||
REFRESH_JS_URL,
|
|
||||||
} from "./constants.ts";
|
|
||||||
import { JS_PREFIX } from "./constants.ts";
|
import { JS_PREFIX } from "./constants.ts";
|
||||||
import { BUILD_ID } from "./constants.ts";
|
import { BUILD_ID } from "./constants.ts";
|
||||||
import {
|
import {
|
||||||
@@ -299,7 +294,7 @@ export class ServerContext {
|
|||||||
page: Page<Data> | UnknownPage | ErrorPage,
|
page: Page<Data> | UnknownPage | ErrorPage,
|
||||||
status: number,
|
status: number,
|
||||||
) => {
|
) => {
|
||||||
const imports: string[] = [bundleAssetUrl("/main.js")];
|
const imports: string[] = [];
|
||||||
if (this.#dev) {
|
if (this.#dev) {
|
||||||
imports.push(REFRESH_JS_URL);
|
imports.push(REFRESH_JS_URL);
|
||||||
}
|
}
|
||||||
|
|||||||
+39
-24
@@ -196,9 +196,41 @@ export function render<Data>(
|
|||||||
return [url, randomNonce] as const;
|
return [url, randomNonce] as const;
|
||||||
});
|
});
|
||||||
|
|
||||||
let islandImports = "";
|
if (ENCOUNTERED_ISLANDS.size > 0) {
|
||||||
let islandRegistry = "";
|
// Load the main.js script
|
||||||
for (const island of ENCOUNTERED_ISLANDS) {
|
{
|
||||||
|
const randomNonce = crypto.randomUUID().replace(/-/g, "");
|
||||||
|
if (csp) {
|
||||||
|
csp.directives.scriptSrc = [
|
||||||
|
...csp.directives.scriptSrc ?? [],
|
||||||
|
nonce(randomNonce),
|
||||||
|
];
|
||||||
|
}
|
||||||
|
const url = bundleAssetUrl("/main.js");
|
||||||
|
imports.push([url, randomNonce] as const);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Prepare the inline script that loads and revives the islands
|
||||||
|
let islandImports = "";
|
||||||
|
let islandRegistry = "";
|
||||||
|
for (const island of ENCOUNTERED_ISLANDS) {
|
||||||
|
const randomNonce = crypto.randomUUID().replace(/-/g, "");
|
||||||
|
if (csp) {
|
||||||
|
csp.directives.scriptSrc = [
|
||||||
|
...csp.directives.scriptSrc ?? [],
|
||||||
|
nonce(randomNonce),
|
||||||
|
];
|
||||||
|
}
|
||||||
|
const url = bundleAssetUrl(`/island-${island.id}.js`);
|
||||||
|
imports.push([url, randomNonce] as const);
|
||||||
|
islandImports += `\nimport ${island.name} from "${url}";`;
|
||||||
|
islandRegistry += `\n ${island.id}: ${island.name},`;
|
||||||
|
}
|
||||||
|
const initCode = `import { revive } from "${
|
||||||
|
bundleAssetUrl("/main.js")
|
||||||
|
}";${islandImports}\nrevive({${islandRegistry}\n});`;
|
||||||
|
|
||||||
|
// Append the inline script to the body
|
||||||
const randomNonce = crypto.randomUUID().replace(/-/g, "");
|
const randomNonce = crypto.randomUUID().replace(/-/g, "");
|
||||||
if (csp) {
|
if (csp) {
|
||||||
csp.directives.scriptSrc = [
|
csp.directives.scriptSrc = [
|
||||||
@@ -206,29 +238,12 @@ export function render<Data>(
|
|||||||
nonce(randomNonce),
|
nonce(randomNonce),
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
const url = bundleAssetUrl(`/island-${island.id}.js`);
|
(bodyHtml as string) +=
|
||||||
imports.push([url, randomNonce] as const);
|
`<script id="__FRSH_ISLAND_PROPS" type="application/json">${
|
||||||
islandImports += `\nimport ${island.name} from "${url}";`;
|
JSON.stringify(ISLAND_PROPS)
|
||||||
islandRegistry += `\n ${island.id}: ${island.name},`;
|
}</script><script type="module" nonce="${randomNonce}">${initCode}</script>`;
|
||||||
}
|
}
|
||||||
|
|
||||||
const initCode = `import { revive } from "${
|
|
||||||
bundleAssetUrl("/main.js")
|
|
||||||
}";${islandImports}\nrevive({${islandRegistry}\n});`;
|
|
||||||
|
|
||||||
const randomNonce = crypto.randomUUID().replace(/-/g, "");
|
|
||||||
if (csp) {
|
|
||||||
csp.directives.scriptSrc = [
|
|
||||||
...csp.directives.scriptSrc ?? [],
|
|
||||||
nonce(randomNonce),
|
|
||||||
];
|
|
||||||
}
|
|
||||||
|
|
||||||
(bodyHtml as string) +=
|
|
||||||
`<script id="__FRSH_ISLAND_PROPS" type="application/json">${
|
|
||||||
JSON.stringify(ISLAND_PROPS)
|
|
||||||
}</script><script type="module" nonce="${randomNonce}">${initCode}</script>`;
|
|
||||||
|
|
||||||
const html = template({
|
const html = template({
|
||||||
bodyHtml,
|
bodyHtml,
|
||||||
headComponents,
|
headComponents,
|
||||||
|
|||||||
+6
-5
@@ -52,7 +52,7 @@ Deno.test("/intercept - GET html", async () => {
|
|||||||
assert(resp);
|
assert(resp);
|
||||||
assertEquals(resp.status, 200);
|
assertEquals(resp.status, 200);
|
||||||
const body = await resp.text();
|
const body = await resp.text();
|
||||||
assert(body.includes("<div>This is HTML</div>"));
|
assertStringIncludes(body, "<div>This is HTML</div>");
|
||||||
});
|
});
|
||||||
|
|
||||||
Deno.test("/intercept - GET text", async () => {
|
Deno.test("/intercept - GET text", async () => {
|
||||||
@@ -94,7 +94,7 @@ Deno.test("/intercept_args - GET html", async () => {
|
|||||||
assert(resp);
|
assert(resp);
|
||||||
assertEquals(resp.status, 200);
|
assertEquals(resp.status, 200);
|
||||||
const body = await resp.text();
|
const body = await resp.text();
|
||||||
assert(body.includes("<div>intercepted</div>"));
|
assertStringIncludes(body, "<div>intercepted</div>");
|
||||||
});
|
});
|
||||||
|
|
||||||
Deno.test("/api/get_only - NOTAMETHOD", async () => {
|
Deno.test("/api/get_only - NOTAMETHOD", async () => {
|
||||||
@@ -112,7 +112,7 @@ Deno.test("/api/xyz not found", async () => {
|
|||||||
assert(resp);
|
assert(resp);
|
||||||
assertEquals(resp.status, 404);
|
assertEquals(resp.status, 404);
|
||||||
const body = await resp.text();
|
const body = await resp.text();
|
||||||
assert(body.includes("404 not found: /api/xyz"));
|
assertStringIncludes(body, "404 not found: /api/xyz");
|
||||||
});
|
});
|
||||||
|
|
||||||
Deno.test("/static page prerender", async () => {
|
Deno.test("/static page prerender", async () => {
|
||||||
@@ -121,9 +121,10 @@ Deno.test("/static page prerender", async () => {
|
|||||||
assertEquals(resp.status, 200);
|
assertEquals(resp.status, 200);
|
||||||
assertEquals(resp.headers.get("content-type"), "text/html; charset=utf-8");
|
assertEquals(resp.headers.get("content-type"), "text/html; charset=utf-8");
|
||||||
const body = await resp.text();
|
const body = await resp.text();
|
||||||
assert(!body.includes(`static.js`));
|
assert(!body.includes(`main.js`));
|
||||||
|
assert(!body.includes(`island-test.js`));
|
||||||
assertStringIncludes(body, "<p>This is a static page.</p>");
|
assertStringIncludes(body, "<p>This is a static page.</p>");
|
||||||
assert(!body.includes("__FRSH_PROPS"));
|
assert(!body.includes("__FRSH_ISLAND_PROPS"));
|
||||||
});
|
});
|
||||||
|
|
||||||
Deno.test("/books/:id page - /books/123", async () => {
|
Deno.test("/books/:id page - /books/123", async () => {
|
||||||
|
|||||||
Reference in New Issue
Block a user