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: add vite:afterUpdate event #9810

Merged
merged 3 commits into from Nov 3, 2022

Conversation

rexxars
Copy link
Contributor

@rexxars rexxars commented Aug 24, 2022

Description

This PR introduces a vite:afterUpdate event to mirror the vite:beforeUpdate event. The intention is to be able to run logic that depends on the new modules being available. The beforeUpdate event triggers rather early - before the modules have even been downloaded. The afterUpdate event triggers after the CSS has fired an onload/onerror event, and after other modules has been imported using fetchUpdate().

One example usage of this event is to trigger updates to modules/state that do not directly depend on the modified files. As an example; an error boundary in React might take arbitrary children - catching any error they throw, and set the error to state. When you modify a file that is a child of this error boundary, the boundary doesn't know that it should throw away the error and attempt re-rendering its children. Of course, this particular example won't work out of the box, since there is a bit of debouncing and a deferred rendering cycle in React - but at the very least you know the modules have been loaded and will trigger a rerender.

Another use-case would be to display a loading state while the modules are being refetched (usually not a long wait, but theoretically over the network to another device, it might).

Closes #6379

Additional context

I've moved the hmr reload message for CSS into the onload/onerror callback. This matches the other module behavior in that it emits when it is loaded, not when it is about to load. Additionally, I've modified one test that was failing due to the order of the CSS/JS potentially not being the same because of this asynchronous console output. Instead it now asserts that both messages are there, rather than the exact order of them.


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 Commit 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 feat: hmr enhancement New feature or request labels Aug 24, 2022
@patak-dev patak-dev added p2-to-be-discussed Enhancement under consideration (priority) needs rebase and removed feat: hmr labels Aug 24, 2022
@patak-dev
Copy link
Member

Thanks for the PR @rexxars, added this feature to the team board to discuss in a future meeting. We have a big backlog of feature requests at this point, so it may take a bit of time to get to it.

@rexxars
Copy link
Contributor Author

rexxars commented Aug 25, 2022

Thanks @patak-dev!

I'd also be curious if you'd consider exposing a way to listen for/register a callback on @vite/plugin-react that lets you know once the call to RefreshRuntime.performReactRefresh() has been done (there's currently a 30ms timeout + a debounced which makes it hard to know).

EDIT: Maybe emitting a CustomEvent might be the easiest?

@patak-dev
Copy link
Member

We discussed this PR in a team meeting and we think it is a good feature to have in core. Thanks @rexxars!

@patak-dev patak-dev merged commit 1f57f84 into vitejs:main Nov 3, 2022
@rexxars rexxars deleted the feat/hmr-after-update-event branch November 3, 2022 20:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request p2-to-be-discussed Enhancement under consideration (priority)
Projects
Archived in project
Development

Successfully merging this pull request may close these issues.

Implementation in HMR event, 'vite:afterUpdate'
3 participants