-
-
Notifications
You must be signed in to change notification settings - Fork 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
fix: yield main thread before navigating #12225
Conversation
🦋 Changeset detectedLatest commit: 5cfa4a9 The changes in this PR will be included in the next version bump. This PR includes changesets to release 1 package
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
@@ -2027,7 +2027,7 @@ function _start_router() { | |||
} | |||
|
|||
/** @param {MouseEvent} event */ | |||
container.addEventListener('click', (event) => { | |||
container.addEventListener('click', async (event) => { |
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 think this may cause errors to be ignored because addEventListener
isn't async and doesn't accept a promise and so there's no rejection handler that gets applied in the error case
By default, SvelteKit preloads the data and code needed to navigate when you touch or hover over a link. As a consequence, it's often the case that navigation can occur immediately following a click event, with no delay for network activity.
Ironically, this means that navigation becomes less responsive, not more, because the browser doesn't get a chance to repaint before the navigation occurs and the page is re-rendered, which can cause dropped frames on slower devices. Because of this, the Interaction to Next Paint (INP) metric suffers, which harms the Core Web Vitals (CWV) score of SvelteKit apps.
This PR adds a tiny pause inside the
click
handler before navigation. The combination ofrequestAnimationFrame
andsetTimeout
allows the browser to repaint, but without creating a user-noticeable delay.No tests because I'm not sure how you would test for this.
Please don't delete this checklist! Before submitting the PR, please make sure you do the following:
Tests
pnpm test
and lint the project withpnpm lint
andpnpm check
Changesets
pnpm changeset
and following the prompts. Changesets that add features should beminor
and those that fix bugs should bepatch
. Please prefix changeset messages withfeat:
,fix:
, orchore:
.Edits