Skip to content

Commit 43b446a

Browse files
committed
fix(prod-server): serve static files for beforeFiles rewrite targets
Add static file serving for beforeFiles rewrite targets in the Pages Router production server. This matches Next.js behavior where beforeFiles rewrites can resolve to static files in public/ or other filesystem paths. The fix passes middleware headers (including Set-Cookie) to the static file response, ensuring middleware-set headers appear on static assets. This resolves the issue from PR cloudflare#776 where beforeFiles rewrites that target static files were not being served correctly.
1 parent 29dde11 commit 43b446a

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

packages/vinext/src/server/prod-server.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1563,6 +1563,9 @@ async function startPagesRouterServer(options: PagesRouterServerOptions) {
15631563
}
15641564

15651565
// ── 7. Apply beforeFiles rewrites from next.config.js ─────────
1566+
// Serve static files for beforeFiles rewrite targets. This matches
1567+
// Next.js behavior where beforeFiles rewrites can resolve to static
1568+
// files in public/ or other direct filesystem paths.
15661569
if (configRewrites.beforeFiles?.length) {
15671570
const rewritten = matchRewrite(resolvedPathname, configRewrites.beforeFiles, postMwReqCtx);
15681571
if (rewritten) {
@@ -1573,6 +1576,22 @@ async function startPagesRouterServer(options: PagesRouterServerOptions) {
15731576
}
15741577
resolvedUrl = rewritten;
15751578
resolvedPathname = rewritten.split("?")[0];
1579+
1580+
// Try serving static file at the rewritten path
1581+
if (
1582+
path.extname(resolvedPathname) &&
1583+
(await tryServeStatic(
1584+
req,
1585+
res,
1586+
clientDir,
1587+
resolvedPathname,
1588+
compress,
1589+
staticCache,
1590+
middlewareHeaders,
1591+
))
1592+
) {
1593+
return;
1594+
}
15761595
}
15771596
}
15781597

0 commit comments

Comments
 (0)