-
-
Notifications
You must be signed in to change notification settings - Fork 2.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix: better dev routing with base using middleware #3942
Conversation
🦋 Changeset detectedLatest commit: c2bdcd2 The changes in this PR will be included in the next version bump. This PR includes changesets to release 9 packages
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This makes a lot of sense to me, thanks for the PR!
Hoping somebody more familiar with this original logic can review as well.
Can you provide some examples of some URLs and how they would previous resolve and how they will resolve now? That would help understand what you have changed here.
Why? It seems like |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Very nice, glad to see that redirect hack go away! 🎉
Thanks for filing this PR and including clean test coverage for it!
If you look at the code here: astro/packages/astro/src/vite-plugin-astro-server/index.ts Lines 124 to 140 in 40a45e3
Example 1:
Example 2:
Example 3:
|
Thanks @AllanChain, those specific cases seem much better in this PR! Really appreciate the contribution. |
Changes
Use a base middle for better base handling in dev.
The previous dev routing design is not very friendly with a base path: it has to prioritize the custom 404 page over redirecting to public files, causing some problems:
base
is set #3808http://localhost:3000/blog/blog/blog/blog/
will redirect tohttp://localhost:3000/blog/
Here I'm using the middleware approach inspired by Vite: return 404, or strip the base path and continue. In other words, the logic for returning 404 based on the base path is moved into the middleware. Thus the public files are correctly handled by the static serving middleware without the need to redirect.
However, just like Vite and the previous approach, requests for public files not starting with the base path are not responded to with 404.
Testing
Added some tests.
Tested with my real project with base path and custom 404 enabled.
Docs
No need.