fix(dev-server): return 404 status code for non-existent pages#11476
fix(dev-server): return 404 status code for non-existent pages#11476cnaples79 wants to merge 1 commit intofacebook:mainfrom
Conversation
The dev server was returning HTTP 200 for all routes due to historyApiFallback, making it difficult to debug missing pages. This was especially problematic when browsers tried to load non-existent CSS/JS files and received HTML instead. This commit adds middleware that detects when the 404 page is served and sets the correct 404 status code. The detection works with: - Default English NotFound component - Translated versions (40+ languages) - Custom 404 pages containing "404" and common patterns The middleware only intercepts HTML GET requests to minimize performance impact on assets. Fixes facebook#11095
slorber
left a comment
There was a problem hiding this comment.
This code is too fragile, complex, unreliable, untested to solve a minor issue so I'm not really willing to merge this, at least not in its current state.
Is this AI-generated?
|
@slorber No, it is not. Do you have any specific feedback on a better implementation so that I can work on an update? |
|
Sorry, with Hacktoberfest and your PR formatting, it's not easy to distinguish PRs that have been created by AI from those created by a real human. I unfortunately don't know how to fix it, but the fix should be reliable and not something that only works for English, avoid hardcoding a list of asset file extensions, and avoid monkey-patching the Response object. You say the problem comes from the usage of historyApiFallback: {
rewrites: [{from: /\/*/, to: baseUrl}],
},Does it serve 404 without that configuration? If that's the case, maybe we should look there and try to provide a better configuration.
|
|
@slorber no worries. I have an idea instead of using the html inspecting middlewre, I may try a smaller helper that specifically checks incoming request paths. I'll add unit tests as well. Will update this soon! |
Summary
Fixes #11095 - Dev server now returns proper 404 status code for non-existent pages instead of 200.
Problem
The dev server's
historyApiFallbackconfiguration was serving the SPA for all routes with HTTP 200 status, even for non-existent pages. This made debugging difficult and caused issues when browsers attempted to load non-existent CSS/JS files (receiving HTML content instead).Solution
Added middleware (
docusaurus-404-status) that:Detection Strategy
The middleware identifies 404 pages using multiple patterns:
hero__titleclass (from NotFound component structure)This approach supports:
Performance Considerations
Limitations
Custom 404 pages that completely remove all "404" and "not found" text may not be detected. Users customizing their NotFound component should ensure it includes recognizable 404 indicators.
Test Plan
Tested manually:
/foo/bar/baz) returns 404Related