fix(rewrites): serve static files from public/ when rewrite target is a .html path#217
Open
yunus25jmi1 wants to merge 1 commit intocloudflare:mainfrom
Open
fix(rewrites): serve static files from public/ when rewrite target is a .html path#217yunus25jmi1 wants to merge 1 commit intocloudflare:mainfrom
yunus25jmi1 wants to merge 1 commit intocloudflare:mainfrom
Conversation
… a .html path Fixes cloudflare#199. When next.config rewrites() map a clean URL to a static .html file in public/ (a common pattern for serving pre-built static pages), vinext was returning 404 because no Next.js route matches .html paths. Changes: - index.ts: Pages Router dev middleware now checks public/ before falling through to the SSR handler when the resolved URL has a file extension after rewrite processing. - app-dev-server.ts: Generated RSC entry (App Router dev handler) checks public/ for the rewritten pathname before returning the 404 response. Accepts a new optional `root` parameter so the public directory path can be embedded in the generated virtual module. - prod-server.ts: After afterFiles and fallback rewrites resolve to a path with a file extension, tryServeStatic() is called against the built clientDir (which includes public/ files) before passing to SSR. - index.ts: Added staticMimeType() helper mapping common file extensions to MIME types for correct Content-Type headers on served files. Tests: - Integration tests in app-router.test.ts and pages-router.test.ts verify that GET /static-html-page (rewritten to /static-html-page.html) returns 200 with correct HTML content and text/html Content-Type. - Unit test in app-router.test.ts verifies generateRscEntry() embeds the public directory path and __nodeFs.statSync static-file lookup code. - Fixture public/static-html-page.html files added to both app-basic and pages-basic fixtures, and their next.config rewrites updated accordingly.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #199.
Problem
When
next.configrewrites() map a clean URL to a static.htmlfile inpublic/(a common pattern for serving pre-built static pages like/auth/no-access→/auth/no-access.html), vinext was returning 404 because no Next.js route matches.htmlpaths.Root Cause
After
afterFilesrewrites resolve a path to*.html, the routing logic in all three server paths would find no matching app/pages route for the.htmlURL and return 404, without ever checking the filesystem'spublic/directory.Fix
Three server paths updated:
Pages Router dev (
packages/vinext/src/index.ts): After afterFiles/fallback rewrites, before the finalhandler()call, check if the resolved URL points to a file inpublic/and serve it directly.App Router dev (
packages/vinext/src/server/app-dev-server.ts): The generated RSC entry now checkspublic/for the rewritten pathname before rendering the 404 page. Added a new optionalrootparameter togenerateRscEntry()so the public directory path is embedded in the generated virtual module.Production server (
packages/vinext/src/server/prod-server.ts): After afterFiles rewrites and after fallback rewrites produce a path with a file extension,tryServeStatic()is called against the builtclientDir(which containspublic/files) before passing to SSR.Also added a
staticMimeType()helper inindex.tsto ensure correctContent-Typeheaders.Tests
app-router.test.tsandpages-router.test.ts:GET /static-html-page(rewritten →/static-html-page.html) returns 200 with correct HTML content andtext/htmlContent-Type.app-router.test.ts:generateRscEntry()embeds thepublic/path in the generated code.public/static-html-page.htmlfiles added to bothapp-basicandpages-basicfixtures;next.configrewrites updated.What async rewrites() flat array does
Per Next.js semantics, when
rewrites()returns a flat array, all rules go intoafterFiles. vinext already handles this correctly viaresolveNextConfig(). This PR fixes the serving of the rewritten destination when it's a static file.