fix: remove top level <script> imports (#1246)
Instead, rely only on modulepreload for script preloading.
This commit is contained in:
+16
-16
@@ -208,7 +208,19 @@ export async function render<Data>(
|
|||||||
|
|
||||||
bodyHtml = bodyHtml as string;
|
bodyHtml = bodyHtml as string;
|
||||||
|
|
||||||
const imports: [string, string][] = [];
|
const moduleScripts: [string, string][] = [];
|
||||||
|
|
||||||
|
for (const url of opts.imports) {
|
||||||
|
const randomNonce = crypto.randomUUID().replace(/-/g, "");
|
||||||
|
if (csp) {
|
||||||
|
csp.directives.scriptSrc = [
|
||||||
|
...csp.directives.scriptSrc ?? [],
|
||||||
|
nonce(randomNonce),
|
||||||
|
];
|
||||||
|
}
|
||||||
|
moduleScripts.push([url, randomNonce]);
|
||||||
|
}
|
||||||
|
|
||||||
const preloadSet = new Set<string>();
|
const preloadSet = new Set<string>();
|
||||||
function addImport(path: string): string {
|
function addImport(path: string): string {
|
||||||
const randomNonce = crypto.randomUUID().replace(/-/g, "");
|
const randomNonce = crypto.randomUUID().replace(/-/g, "");
|
||||||
@@ -219,7 +231,6 @@ export async function render<Data>(
|
|||||||
];
|
];
|
||||||
}
|
}
|
||||||
const url = bundleAssetUrl(`/${path}`);
|
const url = bundleAssetUrl(`/${path}`);
|
||||||
imports.push([url, randomNonce]);
|
|
||||||
preloadSet.add(url);
|
preloadSet.add(url);
|
||||||
for (const depPath of opts.dependenciesFn(path)) {
|
for (const depPath of opts.dependenciesFn(path)) {
|
||||||
const url = bundleAssetUrl(`/${depPath}`);
|
const url = bundleAssetUrl(`/${depPath}`);
|
||||||
@@ -228,17 +239,6 @@ export async function render<Data>(
|
|||||||
return url;
|
return url;
|
||||||
}
|
}
|
||||||
|
|
||||||
for (const url of opts.imports) {
|
|
||||||
const randomNonce = crypto.randomUUID().replace(/-/g, "");
|
|
||||||
if (csp) {
|
|
||||||
csp.directives.scriptSrc = [
|
|
||||||
...csp.directives.scriptSrc ?? [],
|
|
||||||
nonce(randomNonce),
|
|
||||||
];
|
|
||||||
}
|
|
||||||
imports.push([url, randomNonce]);
|
|
||||||
}
|
|
||||||
|
|
||||||
const state: [islands: unknown[], plugins: unknown[]] = [ISLAND_PROPS, []];
|
const state: [islands: unknown[], plugins: unknown[]] = [ISLAND_PROPS, []];
|
||||||
const styleTags: PluginRenderStyleTag[] = [];
|
const styleTags: PluginRenderStyleTag[] = [];
|
||||||
const pluginScripts: [string, string, number][] = [];
|
const pluginScripts: [string, string, number][] = [];
|
||||||
@@ -342,7 +342,7 @@ export async function render<Data>(
|
|||||||
const html = template({
|
const html = template({
|
||||||
bodyHtml,
|
bodyHtml,
|
||||||
headComponents,
|
headComponents,
|
||||||
imports,
|
moduleScripts,
|
||||||
preloads,
|
preloads,
|
||||||
lang: ctx.lang,
|
lang: ctx.lang,
|
||||||
});
|
});
|
||||||
@@ -353,7 +353,7 @@ export async function render<Data>(
|
|||||||
export interface TemplateOptions {
|
export interface TemplateOptions {
|
||||||
bodyHtml: string;
|
bodyHtml: string;
|
||||||
headComponents: ComponentChildren[];
|
headComponents: ComponentChildren[];
|
||||||
imports: (readonly [string, string])[];
|
moduleScripts: (readonly [string, string])[];
|
||||||
preloads: string[];
|
preloads: string[];
|
||||||
lang: string;
|
lang: string;
|
||||||
}
|
}
|
||||||
@@ -373,7 +373,7 @@ export function template(opts: TemplateOptions): string {
|
|||||||
opts.preloads.map((src) =>
|
opts.preloads.map((src) =>
|
||||||
h("link", { rel: "modulepreload", href: src })
|
h("link", { rel: "modulepreload", href: src })
|
||||||
),
|
),
|
||||||
opts.imports.map(([src, nonce]) =>
|
opts.moduleScripts.map(([src, nonce]) =>
|
||||||
h("script", { src: src, nonce: nonce, type: "module" })
|
h("script", { src: src, nonce: nonce, type: "module" })
|
||||||
),
|
),
|
||||||
opts.headComponents,
|
opts.headComponents,
|
||||||
|
|||||||
@@ -6,7 +6,7 @@ Deno.test("check lang", () => {
|
|||||||
const body = template({
|
const body = template({
|
||||||
bodyHtml: "",
|
bodyHtml: "",
|
||||||
headComponents: [],
|
headComponents: [],
|
||||||
imports: [],
|
moduleScripts: [],
|
||||||
preloads: [],
|
preloads: [],
|
||||||
lang,
|
lang,
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user