-
-
Notifications
You must be signed in to change notification settings - Fork 5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(nuxt): defer registering inp handler until nuxt is mounted
- Loading branch information
Showing
1 changed file
with
17 additions
and
13 deletions.
There are no files selected for viewing
30 changes: 17 additions & 13 deletions
30
packages/nuxt/src/app/plugins/navigation-repaint.client.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 1,23 @@ | ||
import { defineNuxtPlugin } from '../nuxt' | ||
import { useRouter } from '../composables' | ||
import { onNuxtReady } from '../composables/ready' | ||
import { useRouter } from '../composables/router' | ||
|
||
export default defineNuxtPlugin(() => { | ||
useRouter().beforeResolve(async () => { | ||
/** | ||
* This gives an opportunity for the browser to repaint, acknowledging user interaction. | ||
* It can reduce INP when navigating on prerendered routes. | ||
* | ||
* @see https://github.com/nuxt/nuxt/issues/26271#issuecomment-2178582037 | ||
* @see https://vercel.com/blog/demystifying-inp-new-tools-and-actionable-insights | ||
*/ | ||
await new Promise((resolve) => { | ||
// Ensure we always resolve, even if the animation frame never fires | ||
setTimeout(resolve, 100) | ||
requestAnimationFrame(() => { setTimeout(resolve, 0) }) | ||
const router = useRouter() | ||
onNuxtReady(() => { | ||
router.beforeResolve(async () => { | ||
/** | ||
* This gives an opportunity for the browser to repaint, acknowledging user interaction. | ||
* It can reduce INP when navigating on prerendered routes. | ||
* | ||
* @see https://github.com/nuxt/nuxt/issues/26271#issuecomment-2178582037 | ||
* @see https://vercel.com/blog/demystifying-inp-new-tools-and-actionable-insights | ||
*/ | ||
await new Promise((resolve) => { | ||
// Ensure we always resolve, even if the animation frame never fires | ||
setTimeout(resolve, 100) | ||
requestAnimationFrame(() => { setTimeout(resolve, 0) }) | ||
}) | ||
}) | ||
}) | ||
}) |