-
-
Notifications
You must be signed in to change notification settings - Fork 6.2k
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
feat!: allow path containing . to fallback to index.html #14142
feat!: allow path containing . to fallback to index.html #14142
Conversation
Run & review this pull request in StackBlitz Codeflow. |
// don't rewrite paths ending with .html | ||
{ | ||
from: /\.html$/, | ||
to({ request }: any) { | ||
return request.url | ||
}, | ||
}, |
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.
Because serveStaticMiddleware
doesn't handle html files and the fallback middleware runs before html middleware.
vite/packages/vite/src/node/server/middlewares/static.ts
Lines 91 to 102 in bd9b749
// only serve the file if it's not an html request or ends with `/` | |
// so that html requests can fallthrough to our html middleware for | |
// special processing | |
// also skip internal requests `/@fs/ /@vite-client` etc... | |
const cleanedUrl = cleanUrl(req.url!) | |
if ( | |
cleanedUrl[cleanedUrl.length - 1] === '/' || | |
path.extname(cleanedUrl) === '.html' || | |
isInternalRequest(req.url!) | |
) { | |
return next() | |
} |
expect((await fetchPath('ICON.png')).headers.get('Content-Type')).toBe( | ||
isBuild ? 'text/html; charset=utf-8' : 'text/html', | ||
) | ||
|
||
expect((await fetchPath('bar')).status).toBe(200) | ||
expect((await fetchPath('bar')).headers.get('Content-Type')).toBe('') | ||
// fallback to index.html | ||
const incorrectBarFetch = await fetchPath('BAR') | ||
expect(incorrectBarFetch.status).toBe(200) | ||
expect(incorrectBarFetch.headers.get('Content-Type')).toContain('text/html') | ||
expect((await fetchPath('BAR')).headers.get('Content-Type')).toContain( | ||
isBuild ? 'text/html;charset=utf-8' : 'text/html', | ||
) |
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.
I haven't checked why one is text/html; charset=utf-8
(with space) and the other is text/html;charset=utf-8
(without space). 🤔
'index: 404', | ||
'index-legacy: 404', | ||
'chunk-async: 404', | ||
'chunk-async-legacy: 404', | ||
'immutable-chunk: 200', | ||
'immutable-chunk-legacy: 200', | ||
'polyfills-legacy: 404', | ||
'index: text/html; charset=utf-8', | ||
'index-legacy: text/html; charset=utf-8', | ||
'chunk-async: text/html; charset=utf-8', | ||
'chunk-async-legacy: text/html; charset=utf-8', | ||
'immutable-chunk: application/javascript', | ||
'immutable-chunk-legacy: application/javascript', | ||
'polyfills-legacy: text/html; charset=utf-8', | ||
].join('\n') | ||
: [ | ||
'index: 404', | ||
'index-legacy: 404', | ||
'chunk-async: 404', | ||
'chunk-async-legacy: 404', | ||
'immutable-chunk: 404', | ||
'immutable-chunk-legacy: 404', | ||
'polyfills-legacy: 404', | ||
'index: text/html', | ||
'index-legacy: text/html', | ||
'chunk-async: text/html', | ||
'chunk-async-legacy: text/html', | ||
'immutable-chunk: text/html', | ||
'immutable-chunk-legacy: text/html', | ||
'polyfills-legacy: text/html', |
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.
sirv adds ; charset=utf-8
to html files (lukeed/sirv#122). But in vite dev
, Vite doesn't.
Maybe we should add it?
I didn't reproduce Firefox/Chromium showing a warning described in lukeed/sirv#106 though.
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.
I remember seeing some issues regarding charsets too, but I think it's better to leave no charset in dev. Not all prod servers at ; charset=utf-8
and some might not want that, so it would be safer for us to not decide on behalf.
'index: 404', | ||
'index-legacy: 404', | ||
'chunk-async: 404', | ||
'chunk-async-legacy: 404', | ||
'immutable-chunk: 200', | ||
'immutable-chunk-legacy: 200', | ||
'polyfills-legacy: 404', | ||
'index: text/html; charset=utf-8', | ||
'index-legacy: text/html; charset=utf-8', | ||
'chunk-async: text/html; charset=utf-8', | ||
'chunk-async-legacy: text/html; charset=utf-8', | ||
'immutable-chunk: application/javascript', | ||
'immutable-chunk-legacy: application/javascript', | ||
'polyfills-legacy: text/html; charset=utf-8', |
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.
Should we leave them 404 in appType: 'mpa'
? It would be odd if they fallback to the index.html
🤔
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.
Legacy playground is not appType: 'mpa'
. This part is the result of fetching all chunks.
https://github.com/vitejs/vite/pull/14142/files/79491351b244ee9ceff9cccbacb810fe47744b28#diff-b17ad79ba0d84b7069324d7eb460e718df66793c5cc903f5b6ff92b5adb73c20
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.
Yeah the test here is fine since it's an spa, but for MPAs (which I manually tested) it also has this same result, and I wonder if that's correct. For example, if we fetch index
(which doesn't exist), in SPA it should fallback to /index.html
, but in MPA, it should 404 (no Content-Type) instead?
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.
Ah, I see. That behavior was there before this PR.
This PR just makes the paths with .
work like the paths without .
.
Access | Response before this PR | Response after this PR |
---|---|---|
/ |
/index.html |
/index.html |
/foo |
/index.html |
/index.html |
/foo/ |
404 | 404 |
/foo.png |
404 | /index.html |
/nested |
/index.html |
/index.html |
/nested/ |
/nested/index.html |
/nested/index.html |
/nested/foo |
/index.html |
/index.html |
/nested/foo.png |
404 | /index.html |
(in MPA)
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.
Thanks. My bad too, I thought the 404 before was an actual 404 without index.html
. Thanks for the table that makes it clearer that this is fine for now 👍
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.
We discussed this PR in today's team meeting, and decided to move forward with it
@sapphi-red @bluwy This PR has caused some questions so far. For example, when I use the In my opinion, this impact seems to be quite large, what do you think? 🤔 |
@btea I think that should be expected since it's an SPA, and you should be able to opt-out of that with It looks like this happens because of #9865. The proper fix would be something on the last line of this comment: #9865 (comment) We also can't revert this as the linked issue #2415 had a lot of request, and between two tradeoffs, I think it's better to lean into this PR instead. |
Rewriting only when Accept header includes |
Thank you for your replies! Regarding the problems that may be caused by the PR modification I mentioned, perhaps a detailed explanation should be added to the official website. Or is it possible to provide users with relevant configurations to actively set fallback policies? |
Description
This PR sets
disableDotRule: true
toconnect-history-api-fallback
.I believe threre's no reason to set this to
false
for Vite.fixes #2415
supersedes close #2634
Additional context
What is the purpose of this pull request?
Before submitting the PR, please make sure you do the following
fixes #123
).