diff --git a/packages/plugin-vite/demo/routes/tests/api/[id].tsx b/packages/plugin-vite/demo/routes/tests/api/[id].tsx new file mode 100644 index 00000000..bbf645cb --- /dev/null +++ b/packages/plugin-vite/demo/routes/tests/api/[id].tsx @@ -0,0 +1 @@ +export const handler = () => new Response("ok"); diff --git a/packages/plugin-vite/src/plugins/dev_server.ts b/packages/plugin-vite/src/plugins/dev_server.ts index b931dca9..0a895a07 100644 --- a/packages/plugin-vite/src/plugins/dev_server.ts +++ b/packages/plugin-vite/src/plugins/dev_server.ts @@ -4,9 +4,13 @@ import { ASSET_CACHE_BUST_KEY } from "fresh/internal"; import { createRequest, sendResponse } from "@mjackson/node-fetch-server"; export function devServer(): Plugin[] { + let publicDir = ""; return [ { name: "fresh:dev_server", + configResolved(config) { + publicDir = config.publicDir; + }, configureServer(server) { const IGNORE_URLS = /^\/(@(vite|fs|id)|\.vite)\//; @@ -29,17 +33,23 @@ export function devServer(): Plugin[] { server.environments.client.moduleGraph.urlToModuleMap.has( url.pathname, ) || + server.environments.ssr.moduleGraph.urlToModuleMap.has( + url.pathname, + ) || url.pathname === "/.well-known/appspecific/com.chrome.devtools.json" ) { return next(); } // Check if it's a static file first - if (url.pathname !== "/") { - const ext = path.extname(url.pathname); - if (ext !== "") { + const staticFilePath = path.join(publicDir, url.pathname.slice(1)); + try { + const stat = await Deno.stat(staticFilePath); + if (stat.isFile) { return next(); } + } catch { + // Ignore } const mod = await server.ssrLoadModule("fresh:server_entry"); diff --git a/packages/plugin-vite/tests/dev_server_test.ts b/packages/plugin-vite/tests/dev_server_test.ts index 04b39ace..91e8d671 100644 --- a/packages/plugin-vite/tests/dev_server_test.ts +++ b/packages/plugin-vite/tests/dev_server_test.ts @@ -175,8 +175,16 @@ Deno.test({ Deno.test({ name: "vite dev - serves imported assets", fn: async () => { - await launchDevServer(tmp.dir, async (address) => { - const res = await fetch(`${address}/assets/deno-logo.png`); + await launchDevServer(DEMO_DIR, async (address) => { + // Vite has an internal allowlist that is refreshed when + // pathnames are requested. It will discover valid static + // files imported in JS once it encounters them. Therefore + // we must request the URL that ultimately imports the + // asset first for it to work. + let res = await fetch(`${address}/tests/assets`); + await res.body?.cancel(); + + res = await fetch(`${address}/assets/deno-logo.png`); expect(res.status).toEqual(200); expect(res.headers.get("Content-Type")).toEqual("image/png"); }); @@ -328,3 +336,17 @@ Deno.test({ sanitizeOps: false, sanitizeResources: false, }); + +// issue: https://github.com/denoland/fresh/issues/3322 +Deno.test({ + name: "vite dev - allow routes looking like static paths", + fn: async () => { + await launchDevServer(tmp.dir, async (address) => { + const res = await fetch(`${address}/tests/api/@marvinh@infosec.exchange`); + const text = await res.text(); + expect(text).toEqual("ok"); + }); + }, + sanitizeOps: false, + sanitizeResources: false, +});