Skip to content
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

Merged
merged 4 commits into from
Aug 24, 2023

Conversation

sapphi-red
Copy link
Member

Description

This PR sets disableDotRule: true to connect-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?

  • Bug fix
  • New Feature
  • Documentation update
  • Other

Before submitting the PR, please make sure you do the following

  • Read the Contributing Guidelines.
  • Read the Pull Request Guidelines and follow the PR Title Convention.
  • Check that there isn't already a PR that solves the problem the same way to avoid creating a duplicate.
  • Provide a description in this PR that addresses what the PR is solving, or reference the issue that it solves (e.g. fixes #123).
  • Ideally, include relevant tests that fail without this PR but pass with it.

@sapphi-red sapphi-red added p2-to-be-discussed Enhancement under consideration (priority) breaking change labels Aug 17, 2023
@stackblitz
Copy link

stackblitz bot commented Aug 17, 2023

Review PR in StackBlitz Codeflow Run & review this pull request in StackBlitz Codeflow.

@sapphi-red sapphi-red marked this pull request as draft August 17, 2023 16:00
@sapphi-red sapphi-red marked this pull request as ready for review August 17, 2023 16:41
Comment on lines +29 to +35
// don't rewrite paths ending with .html
{
from: /\.html$/,
to({ request }: any) {
return request.url
},
},
Copy link
Member Author

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.

// 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()
}

Comment on lines +44 to +52
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',
)
Copy link
Member Author

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). 🤔

Comment on lines -55 to +70
'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',
Copy link
Member Author

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.

Copy link
Member

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.

playground/legacy/index.html Outdated Show resolved Hide resolved
Comment on lines -55 to +61
'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',
Copy link
Member

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 🤔

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member

@bluwy bluwy Aug 18, 2023

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?

Copy link
Member Author

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)

Copy link
Member

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 👍

@patak-dev patak-dev added this to the 5.0 milestone Aug 24, 2023
Copy link
Member

@patak-dev patak-dev left a 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

@patak-dev patak-dev merged commit 1ae4cbd into vitejs:main Aug 24, 2023
11 checks passed
@sapphi-red sapphi-red deleted the feat/disable-dot-rule-by-default branch August 25, 2023 16:16
wallpants added a commit to wallpants/github-preview.nvim that referenced this pull request Sep 6, 2023
wallpants added a commit to wallpants/github-preview.nvim that referenced this pull request Sep 6, 2023
wallpants added a commit to wallpants/github-preview.nvim that referenced this pull request Sep 6, 2023
@btea
Copy link
Collaborator

btea commented Sep 7, 2023

@sapphi-red @bluwy This PR has caused some questions so far.

For example, when I use the img tag to load an image on the page if I write the wrong path, the console will print a 404 error before this PR, but the console will not print any information after this PR. This is very unfriendly to development prompts.

In my opinion, this impact seems to be quite large, what do you think? 🤔

@bluwy
Copy link
Member

bluwy commented Sep 8, 2023

@btea I think that should be expected since it's an SPA, and you should be able to opt-out of that with appType: 'mpa', but currently even in MPA it doesn't work (as discussed in #14142 (comment)).

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.

@sapphi-red
Copy link
Member Author

Rewriting only when Accept header includes text/html might work. If many static hosting service does that, I think it makes sense to have that.
But I'm not sure how much apps will break if we do that.

@btea
Copy link
Collaborator

btea commented Sep 8, 2023

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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
breaking change p2-to-be-discussed Enhancement under consideration (priority)
Projects
Archived in project
Development

Successfully merging this pull request may close these issues.

Dots in URL lead to 404 (regression?)
4 participants