fix: properly prefix all scripts with base path (#2334)
closes https://github.com/denoland/fresh/issues/2326 There was a big misunderstanding on my part -- in particular that a different server might be listening at the root, so the redirect built into `context.ts` will never happen. The existing test (as mentioned in the issue) passes, but only because no one else is listening, and fresh is able to perform the redirect. I briefly considered writing a docker container to orchestrate this properly, but then I didn't want to have docker running in order to run these tests. (My laptop is old and bad.) So instead the two new tests very thoroughly assert that scripts included in the html are properly prefixed with the base path. And yes, they fail if the fix is reverted. As for the fix itself: I just needed to prepend the asset URL with the base path.
This commit is contained in:
@@ -358,6 +358,7 @@ export async function render<Data>(
|
||||
dependenciesFn: opts.dependenciesFn,
|
||||
styles: ctx.styles,
|
||||
pluginRenderResults: renderResults,
|
||||
basePath,
|
||||
});
|
||||
|
||||
// Append error overlay in dev mode
|
||||
|
||||
@@ -23,6 +23,7 @@ export function renderFreshTags(
|
||||
dependenciesFn: (path: string) => string[];
|
||||
styles: string[];
|
||||
pluginRenderResults: [Plugin, PluginRenderResult][];
|
||||
basePath: string;
|
||||
},
|
||||
) {
|
||||
const { isPartial } = renderState;
|
||||
@@ -41,7 +42,7 @@ export function renderFreshTags(
|
||||
|
||||
const preloadSet = new Set<string>();
|
||||
function addImport(path: string): string {
|
||||
const url = bundleAssetUrl(`/${path}`);
|
||||
const url = opts.basePath + bundleAssetUrl(`/${path}`);
|
||||
if (!isPartial) {
|
||||
preloadSet.add(url);
|
||||
for (const depPath of opts.dependenciesFn(path)) {
|
||||
|
||||
@@ -4,6 +4,8 @@ import {
|
||||
assertMatch,
|
||||
assertStringIncludes,
|
||||
delay,
|
||||
Page,
|
||||
Project,
|
||||
puppeteer,
|
||||
STATUS_CODE,
|
||||
} from "./deps.ts";
|
||||
@@ -150,6 +152,46 @@ Deno.test("island tests", async (t) => {
|
||||
);
|
||||
});
|
||||
|
||||
Deno.test("assets prefixed dev", async (t) => {
|
||||
await withPageName(
|
||||
"./tests/fixture_base_path/dev.ts",
|
||||
async (page, address) => {
|
||||
await page.goto(`${address}/islands`);
|
||||
|
||||
await t.step("ensure every preload link is prefixed", async () => {
|
||||
await checkPreloadLinks(page, "/foo/bar");
|
||||
});
|
||||
|
||||
await t.step("ensure every script link is prefixed", async () => {
|
||||
await checkScriptSrcs(page, "/foo/bar");
|
||||
});
|
||||
|
||||
await t.step("ensure inline content is prefixed", async () => {
|
||||
await checkInlineScripts(page, "/foo/bar");
|
||||
});
|
||||
},
|
||||
);
|
||||
});
|
||||
|
||||
Deno.test("assets prefixed main", async (t) => {
|
||||
await withPageName(
|
||||
"./tests/fixture_base_path/main.ts",
|
||||
async (page, address) => {
|
||||
await page.goto(`${address}/islands`);
|
||||
|
||||
await t.step("ensure every preload link is prefixed", async () => {
|
||||
await checkPreloadLinks(page, "/foo/bar");
|
||||
});
|
||||
|
||||
// no script sent out, because dev sends out dev_client.js
|
||||
|
||||
await t.step("ensure inline content is prefixed", async () => {
|
||||
await checkInlineScripts(page, "/foo/bar");
|
||||
});
|
||||
},
|
||||
);
|
||||
});
|
||||
|
||||
Deno.test("renders error boundary", async () => {
|
||||
await withPageName(
|
||||
"./tests/fixture_base_path/main.ts",
|
||||
@@ -247,3 +289,62 @@ Deno.test("TailwindCSS - middleware only css", async () => {
|
||||
{ loadConfig: true },
|
||||
);
|
||||
});
|
||||
|
||||
function extractImportUrls(scriptContent: string): string[] {
|
||||
const project = new Project({
|
||||
useInMemoryFileSystem: true,
|
||||
});
|
||||
|
||||
const sourceFile = project.createSourceFile("script.js", scriptContent);
|
||||
|
||||
const importDeclarations = sourceFile.getImportDeclarations();
|
||||
|
||||
return importDeclarations.map((importDeclaration) =>
|
||||
importDeclaration.getModuleSpecifierValue()
|
||||
);
|
||||
}
|
||||
|
||||
async function checkPreloadLinks(page: Page, basePath: string) {
|
||||
const preloadLinks: string[] = await page.$$eval(
|
||||
'link[rel="modulepreload"]',
|
||||
(links) => links.map((link) => link.getAttribute("href")),
|
||||
);
|
||||
assert(preloadLinks.length > 0, "No preload links found");
|
||||
preloadLinks.forEach((href) => {
|
||||
assert(
|
||||
href.startsWith(basePath),
|
||||
`Preload link ${href} does not include the correct base path`,
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
async function checkScriptSrcs(page: Page, basePath: string) {
|
||||
const scriptSrcs: string[] = await page.$$eval(
|
||||
"script[src]",
|
||||
(scripts) => scripts.map((script) => script.getAttribute("src")),
|
||||
);
|
||||
assert(scriptSrcs.length > 0, "No script srcs found");
|
||||
scriptSrcs.forEach((src) => {
|
||||
assert(
|
||||
src.startsWith(basePath),
|
||||
`Script src ${src} does not include the correct base path`,
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
async function checkInlineScripts(page: Page, basePath: string) {
|
||||
const inlineScripts = await page.$$eval(
|
||||
"script:not([src])",
|
||||
(scripts) => scripts.map((script) => script.textContent),
|
||||
);
|
||||
assert(inlineScripts.length > 0, "No inline scripts found");
|
||||
inlineScripts.forEach((scriptContent) => {
|
||||
const importUrls = extractImportUrls(scriptContent);
|
||||
importUrls.forEach((url) => {
|
||||
assert(
|
||||
url.startsWith(basePath),
|
||||
`Import URL ${url} does not include the correct base path`,
|
||||
);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
@@ -49,3 +49,4 @@ export * as JSONC from "https://deno.land/std@0.216.0/jsonc/mod.ts";
|
||||
export * as colors from "https://deno.land/std@0.216.0/fmt/colors.ts";
|
||||
export { STATUS_CODE } from "https://deno.land/std@0.216.0/http/status.ts";
|
||||
export { stripAnsiCode } from "https://deno.land/std@0.216.0/fmt/colors.ts";
|
||||
export { Project } from "https://deno.land/x/ts_morph@21.0.1/mod.ts";
|
||||
|
||||
Reference in New Issue
Block a user