feat!: allow path containing . to fallback to index.html#14142
feat!: allow path containing . to fallback to index.html#14142patak-cat merged 4 commits intovitejs:mainfrom
Conversation
|
|
| // don't rewrite paths ending with .html | ||
| { | ||
| from: /\.html$/, | ||
| to({ request }: any) { | ||
| return request.url | ||
| }, | ||
| }, |
There was a problem hiding this comment.
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
| 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.
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.
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.
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.
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.
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.
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.
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.
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 👍
patak-cat
left a comment
There was a problem hiding this comment.
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: truetoconnect-history-api-fallback.I believe threre's no reason to set this to
falsefor 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).