fix: remove top level <script> imports (#1246)

Instead, rely only on modulepreload for script preloading.
This commit is contained in:
Luca Casonato
2023-06-05 18:21:08 +02:00
committed by GitHub
parent 6e1874ccca
commit da5d507b77
2 changed files with 17 additions and 17 deletions
+16 -16
View File
@@ -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,
+1 -1
View File
@@ -6,7 +6,7 @@ Deno.test("check lang", () => {
const body = template({
bodyHtml: "",
headComponents: [],
imports: [],
moduleScripts: [],
preloads: [],
lang,
});