fix(vite): pathname treated as static file (#3326)
Fixes https://github.com/denoland/fresh/issues/3322
This commit is contained in:
@@ -0,0 +1 @@
|
|||||||
|
export const handler = () => new Response("ok");
|
||||||
@@ -4,9 +4,13 @@ import { ASSET_CACHE_BUST_KEY } from "fresh/internal";
|
|||||||
import { createRequest, sendResponse } from "@mjackson/node-fetch-server";
|
import { createRequest, sendResponse } from "@mjackson/node-fetch-server";
|
||||||
|
|
||||||
export function devServer(): Plugin[] {
|
export function devServer(): Plugin[] {
|
||||||
|
let publicDir = "";
|
||||||
return [
|
return [
|
||||||
{
|
{
|
||||||
name: "fresh:dev_server",
|
name: "fresh:dev_server",
|
||||||
|
configResolved(config) {
|
||||||
|
publicDir = config.publicDir;
|
||||||
|
},
|
||||||
configureServer(server) {
|
configureServer(server) {
|
||||||
const IGNORE_URLS = /^\/(@(vite|fs|id)|\.vite)\//;
|
const IGNORE_URLS = /^\/(@(vite|fs|id)|\.vite)\//;
|
||||||
|
|
||||||
@@ -29,17 +33,23 @@ export function devServer(): Plugin[] {
|
|||||||
server.environments.client.moduleGraph.urlToModuleMap.has(
|
server.environments.client.moduleGraph.urlToModuleMap.has(
|
||||||
url.pathname,
|
url.pathname,
|
||||||
) ||
|
) ||
|
||||||
|
server.environments.ssr.moduleGraph.urlToModuleMap.has(
|
||||||
|
url.pathname,
|
||||||
|
) ||
|
||||||
url.pathname === "/.well-known/appspecific/com.chrome.devtools.json"
|
url.pathname === "/.well-known/appspecific/com.chrome.devtools.json"
|
||||||
) {
|
) {
|
||||||
return next();
|
return next();
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check if it's a static file first
|
// Check if it's a static file first
|
||||||
if (url.pathname !== "/") {
|
const staticFilePath = path.join(publicDir, url.pathname.slice(1));
|
||||||
const ext = path.extname(url.pathname);
|
try {
|
||||||
if (ext !== "") {
|
const stat = await Deno.stat(staticFilePath);
|
||||||
|
if (stat.isFile) {
|
||||||
return next();
|
return next();
|
||||||
}
|
}
|
||||||
|
} catch {
|
||||||
|
// Ignore
|
||||||
}
|
}
|
||||||
|
|
||||||
const mod = await server.ssrLoadModule("fresh:server_entry");
|
const mod = await server.ssrLoadModule("fresh:server_entry");
|
||||||
|
|||||||
@@ -175,8 +175,16 @@ Deno.test({
|
|||||||
Deno.test({
|
Deno.test({
|
||||||
name: "vite dev - serves imported assets",
|
name: "vite dev - serves imported assets",
|
||||||
fn: async () => {
|
fn: async () => {
|
||||||
await launchDevServer(tmp.dir, async (address) => {
|
await launchDevServer(DEMO_DIR, async (address) => {
|
||||||
const res = await fetch(`${address}/assets/deno-logo.png`);
|
// 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.status).toEqual(200);
|
||||||
expect(res.headers.get("Content-Type")).toEqual("image/png");
|
expect(res.headers.get("Content-Type")).toEqual("image/png");
|
||||||
});
|
});
|
||||||
@@ -328,3 +336,17 @@ Deno.test({
|
|||||||
sanitizeOps: false,
|
sanitizeOps: false,
|
||||||
sanitizeResources: 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,
|
||||||
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user