From 8f3031317b9de2e900f147ff9fad875a2521fb84 Mon Sep 17 00:00:00 2001 From: Reed von Redwitz Date: Fri, 23 Feb 2024 13:30:42 +0100 Subject: [PATCH] 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. --- src/server/render.ts | 1 + src/server/rendering/fresh_tags.tsx | 3 +- tests/base_path_test.ts | 101 ++++++++++++++++++++++++++++ tests/deps.ts | 1 + 4 files changed, 105 insertions(+), 1 deletion(-) diff --git a/src/server/render.ts b/src/server/render.ts index 86a269c4..6feed668 100644 --- a/src/server/render.ts +++ b/src/server/render.ts @@ -358,6 +358,7 @@ export async function render( dependenciesFn: opts.dependenciesFn, styles: ctx.styles, pluginRenderResults: renderResults, + basePath, }); // Append error overlay in dev mode diff --git a/src/server/rendering/fresh_tags.tsx b/src/server/rendering/fresh_tags.tsx index 28e6da10..07b3612c 100644 --- a/src/server/rendering/fresh_tags.tsx +++ b/src/server/rendering/fresh_tags.tsx @@ -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(); 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)) { diff --git a/tests/base_path_test.ts b/tests/base_path_test.ts index 20533552..76435c14 100644 --- a/tests/base_path_test.ts +++ b/tests/base_path_test.ts @@ -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`, + ); + }); + }); +} diff --git a/tests/deps.ts b/tests/deps.ts index 9b15b867..388537be 100644 --- a/tests/deps.ts +++ b/tests/deps.ts @@ -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";