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;
|
||||
|
||||
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>();
|
||||
function addImport(path: string): string {
|
||||
const randomNonce = crypto.randomUUID().replace(/-/g, "");
|
||||
@@ -219,7 +231,6 @@ export async function render<Data>(
|
||||
];
|
||||
}
|
||||
const url = bundleAssetUrl(`/${path}`);
|
||||
imports.push([url, randomNonce]);
|
||||
preloadSet.add(url);
|
||||
for (const depPath of opts.dependenciesFn(path)) {
|
||||
const url = bundleAssetUrl(`/${depPath}`);
|
||||
@@ -228,17 +239,6 @@ export async function render<Data>(
|
||||
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 styleTags: PluginRenderStyleTag[] = [];
|
||||
const pluginScripts: [string, string, number][] = [];
|
||||
@@ -342,7 +342,7 @@ export async function render<Data>(
|
||||
const html = template({
|
||||
bodyHtml,
|
||||
headComponents,
|
||||
imports,
|
||||
moduleScripts,
|
||||
preloads,
|
||||
lang: ctx.lang,
|
||||
});
|
||||
@@ -353,7 +353,7 @@ export async function render<Data>(
|
||||
export interface TemplateOptions {
|
||||
bodyHtml: string;
|
||||
headComponents: ComponentChildren[];
|
||||
imports: (readonly [string, string])[];
|
||||
moduleScripts: (readonly [string, string])[];
|
||||
preloads: string[];
|
||||
lang: string;
|
||||
}
|
||||
@@ -373,7 +373,7 @@ export function template(opts: TemplateOptions): string {
|
||||
opts.preloads.map((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" })
|
||||
),
|
||||
opts.headComponents,
|
||||
|
||||
@@ -6,7 +6,7 @@ Deno.test("check lang", () => {
|
||||
const body = template({
|
||||
bodyHtml: "",
|
||||
headComponents: [],
|
||||
imports: [],
|
||||
moduleScripts: [],
|
||||
preloads: [],
|
||||
lang,
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user