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

perf: pre transform requests while opening the browser #12809

Merged
merged 3 commits into from Oct 16, 2023

Conversation

patak-dev
Copy link
Member

@patak-dev patak-dev commented Apr 10, 2023

Description

There is a ~500-600ms blank when opening the browser where we only run the scanner and optimize step (these two are off the main thread for the most part and are generally really fast so we can still run other things in parallel).

@bluwy developed vite-plugin-warmup, which we are trying to include directly in core so frameworks can start the pre-process earlier.

This PR is complementary to a future warmup and implements an optimization that doesn't require configuration so users can take advantage of it directly. When the --open flag is used, we know what is the first request that the browser will emit. So, no matter what framework is used to process entry points (or if our index HTML middleware kicks in) we can do the same request upfront. For this to work in frameworks that replace the index HTML middleware, they should also call server.transformRequest(url) for the scripts they find. This is a good idea in general, independent of this PR, so we should push for that across the ecosystem.

One caveat of the approach in this PR is that the entry point will be processed twice. I think this isn't a problem because it is what normally occurs when several tabs are opened and the server is reloaded. We could add caching to the index HTML middleware but I don't know if the complexity is worth it because processing the HTML during dev is a really fast operation.


What is the purpose of this pull request?

  • Bug fix
  • New Feature
  • Documentation update
  • Other

@stackblitz
Copy link

stackblitz bot commented Apr 10, 2023

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

@patak-dev patak-dev added performance Performance related enhancement p4-important Violate documented behavior or significantly improves performance (priority) labels Apr 10, 2023
@Akryum
Copy link
Contributor

Akryum commented Apr 10, 2023

Couldn't this also work when --open isn't used?

@patak-dev
Copy link
Member Author

@Akryum when --open is used, we are sure of what the first request will be. If it isn't used, then we could also send a request for / and that would generally help, but the user could start on a different page if it is a MPA. So we could be making cold start slower if the user doesn't end up visiting / first.

@Akryum
Copy link
Contributor

Akryum commented Apr 10, 2023

Maybe the last opened route can be saved as it is very likely to be the one being loaded by the user when her browser is opened again.

@patak-dev
Copy link
Member Author

That's an interesting proposal @Akryum. We're discussing with @bluwy how the API for warmup would look like and my feeling is that having too much magic here wouldn't be needed if there is a good way to configure Vite (and if this is mostly taken care of by frameworks). So I would prefer to keep the strategy in the PR simple. We still need to check how it will fit in the big picture, but I think that whatever the API is going to look like, this addition is a good nice to have. We probably will only do it if custom warmup is not defined by the user, so I don't think it would interfere with it.

@ArnaudBarre
Copy link
Member

For usage without open, I think we should have a performance page for documentation where we can advice using the warmup plugin

@patak-dev patak-dev added this to the 5.0 milestone Jul 13, 2023
@patak-dev
Copy link
Member Author

We discussed this PR on the last team meeting, and we agreed on moving forward with it in Vite 5.

@bluwy bluwy merged commit 96a4ce3 into main Oct 16, 2023
14 of 15 checks passed
@bluwy bluwy deleted the perf/pre-transform-requests-while-opening-the-browser branch October 16, 2023 15:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
p4-important Violate documented behavior or significantly improves performance (priority) performance Performance related enhancement
Projects
Archived in project
Development

Successfully merging this pull request may close these issues.

None yet

4 participants