fix: handle paths with whitespaces in staticFiles (#3659)
Fix #3657 Static files with special characters in their names (spaces, `#`, unicode, etc.) fail to load due to path encoding issues. ## Root cause `prepareStaticFile()` uses `new URL(pathname, base)` to normalize paths, but `new URL` doesn't properly encode characters that are already percent-encoded in the filesystem path. For example, a file literally named `test %20encodeUri` becomes `test%20%20encodeUri` (the space and literal `%20` are indistinguishable), instead of the correct `test%20%2520encodeUri`. --------- Co-authored-by: Bartek Iwańczuk <biwanczuk@gmail.com> Co-authored-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
co-authored by
Bartek Iwańczuk
Claude Opus 4.6
parent
4b4e495194
commit
c863097779
@@ -529,6 +529,12 @@ ${serializedFsRoutes}
|
||||
`.replaceAll(/\n[\n]+/g, "\n\n");
|
||||
}
|
||||
|
||||
export function systemPathToUrlEncoded(systemPath: string): string {
|
||||
const normalized = systemPath.replaceAll(WINDOWS_SEPARATOR, "/");
|
||||
const components = normalized.split("/").filter((comp) => comp);
|
||||
return "/" + components.map((comp) => encodeURIComponent(comp)).join("/");
|
||||
}
|
||||
|
||||
export async function prepareStaticFile(
|
||||
item: PendingStaticFile,
|
||||
outDir: string,
|
||||
@@ -537,10 +543,10 @@ export async function prepareStaticFile(
|
||||
> {
|
||||
const file = await Deno.open(item.filePath);
|
||||
const hash = item.hash ? item.hash : await hashContent(file.readable);
|
||||
const url = new URL(item.pathname, "http://localhost");
|
||||
const encodedPathname = systemPathToUrlEncoded(item.pathname);
|
||||
|
||||
return {
|
||||
name: url.pathname,
|
||||
name: encodedPathname,
|
||||
hash,
|
||||
filePath: path.isAbsolute(item.filePath)
|
||||
? path.relative(outDir, item.filePath)
|
||||
|
||||
@@ -18,7 +18,7 @@ export function staticFiles<T>(): Middleware<T> {
|
||||
const buildCache = getBuildCache(ctx);
|
||||
if (buildCache === null) return await ctx.next();
|
||||
|
||||
let pathname = decodeURIComponent(url.pathname);
|
||||
let pathname = url.pathname;
|
||||
if (config.basePath) {
|
||||
pathname = pathname !== config.basePath
|
||||
? pathname.slice(config.basePath.length)
|
||||
|
||||
@@ -6,7 +6,10 @@ import { ASSET_CACHE_BUST_KEY } from "../constants.ts";
|
||||
import { BUILD_ID } from "@fresh/build-id";
|
||||
import type { Command } from "../commands.ts";
|
||||
import type { ServerIslandRegistry } from "../context.ts";
|
||||
import { getContentType } from "../dev/dev_build_cache.ts";
|
||||
import {
|
||||
getContentType,
|
||||
systemPathToUrlEncoded,
|
||||
} from "../dev/dev_build_cache.ts";
|
||||
|
||||
class MockBuildCache implements BuildCache {
|
||||
root = "";
|
||||
@@ -223,12 +226,17 @@ Deno.test("static files - enables caching in production", async () => {
|
||||
);
|
||||
});
|
||||
|
||||
Deno.test("static files - decoded pathname", async () => {
|
||||
const buildCache = new MockBuildCache({
|
||||
"C#.svg": { content: "body {}", hash: null },
|
||||
"西安市.png": { content: "body {}", hash: null },
|
||||
"인천.avif": { content: "body {}", hash: null },
|
||||
});
|
||||
Deno.test("static files - encoded pathname", async () => {
|
||||
// Build cache stores URL-encoded paths (matching what prepareStaticFile produces)
|
||||
const fileKeys = ["C#.svg", "西安市.png", "인천.avif"];
|
||||
const entries = Object.fromEntries(
|
||||
fileKeys.map((key) => [
|
||||
systemPathToUrlEncoded(key),
|
||||
{ content: "body {}", hash: null },
|
||||
]),
|
||||
);
|
||||
|
||||
const buildCache = new MockBuildCache(entries);
|
||||
const server = serveMiddleware(
|
||||
staticFiles(),
|
||||
{ buildCache },
|
||||
|
||||
Reference in New Issue
Block a user