Match API paths the way the router does

The router mounts /api without regard to letter case, accepts a '.' as well
as '/' after the prefix, and routes absolute-form request targets, so the
API response headers were missing on those spellings. Match the same way, on
both the raw and the parsed path. The test now runs real dev and preview
servers.
This commit is contained in:
Sameh Khamis committed 2026-10-21 15:33:13 -07:00
1 parent 3e80afe713
commit d80162bea2
3 files changed
+113 -106

No files matched your search

+16 -3
View File
@@ -60,10 +60,23 @@ export const API_HEADERS = Object.freeze({
'X-Content-Type-Options': 'nosniff',
});
/** Whether a request path is an API route (`/api` or `/api/...`). */
/**
* Whether a request reaches an `/api` mount, by the router's own rule: the
* prefix compared without case and followed by the end, `/` or `.`. Both the
* raw path and the parsed one (which also covers an absolute-form target) are
* checked, so no spelling the router mounts can miss the headers.
*/
function isApiPath(url) {
const path = String(url || '/').split(/[?#]/, 1)[0];
return path === '/api' || path.startsWith('/api/');
const raw = String(url || '/');
let parsed = '';
try {
parsed = new URL(raw, 'http://localhost').pathname;
} catch {
/* the raw path still decides */
}
return [raw.split(/[?#]/, 1)[0], parsed].some((path) =>
/^\/api(?:$|[/.])/i.test(path),
);
}
/**