fix(vite): pathname treated as static file (#3326)

Fixes https://github.com/denoland/fresh/issues/3322
This commit is contained in:
Marvin Hagemeister
2025-09-03 11:34:19 +02:00
committed by GitHub
parent 717aa32111
commit 08ef05fc1d
3 changed files with 38 additions and 5 deletions
@@ -0,0 +1 @@
export const handler = () => new Response("ok");
+13 -3
View File
@@ -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");
+24 -2
View File
@@ -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,
});