Skip to content

Commit a1ac506

Browse files
authored
fix: handle nuxt 3 and vue-router 4x api (#49)
1 parent e019d1e commit a1ac506

File tree

3 files changed

+26
-17
lines changed

3 files changed

+26
-17
lines changed

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
},
3838
"dependencies": {
3939
"defu": "^5.0.0",
40-
"ufo": "^0.7.9",
40+
"ufo": "^0.8.5",
4141
"web-vitals": "^2.1.0"
4242
},
4343
"devDependencies": {

src/runtime/vitals.client.mjs

+21-12
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,26 @@ import { sendToAnalytics } from '~vitals-provider'
33

44
const options = <%= JSON.stringify(options, null, 2) %>
55

6-
export default function ({ app: { router } }) {
7-
router.onReady((to) => {
8-
webVitals({
9-
options,
10-
sendToAnalytics,
11-
route: to
6+
const sendVitals = (to) => webVitals({
7+
options,
8+
sendToAnalytics,
9+
route: to
10+
})
11+
12+
export default async function (ctx) {
13+
const router = ctx.app?.router || ctx.$router
14+
if (!router) { return }
15+
16+
if ('isReady' in router) {
17+
// vue-router 4
18+
await router.isReady()
19+
sendVitals(router.currentRoute)
20+
router.afterEach((to) => sendVitals(to))
21+
} else {
22+
// vue-router 3
23+
router.onReady((to) => {
24+
sendVitals(to)
25+
router.afterEach((to) => sendVitals(to))
1226
})
13-
router.afterEach(to => webVitals({
14-
options,
15-
sendToAnalytics,
16-
route: to
17-
}))
18-
})
27+
}
1928
}

yarn.lock

+4-4
Original file line numberDiff line numberDiff line change
@@ -9434,10 +9434,10 @@ ufo@^0.7.7:
94349434
resolved "https://registry.yarnpkg.com/ufo/-/ufo-0.7.7.tgz#0062f9e5e790819b0fb23ca24d7c63a4011c036a"
94359435
integrity sha512-N25aY3HBkJBnahm+2l4JRBBrX5I+JPakF/tDHYDTjd3wUR7iFLdyiPhj8mBwBz21v728BKwM9L9tgBfCntgdlw==
94369436

9437-
ufo@^0.7.9:
9438-
version "0.7.9"
9439-
resolved "https://registry.yarnpkg.com/ufo/-/ufo-0.7.9.tgz#0268e3734b413c9ed6f3510201f42372821b875c"
9440-
integrity sha512-6t9LrLk3FhqTS+GW3IqlITtfRB5JAVr5MMNjpBECfK827W+Vh5Ilw/LhTcHWrt6b3hkeBvcbjx4Ti7QVFzmcww==
9437+
ufo@^0.8.5:
9438+
version "0.8.5"
9439+
resolved "https://registry.yarnpkg.com/ufo/-/ufo-0.8.5.tgz#e367b4205ece9d9723f2fa54f887d43ed1bce5d0"
9440+
integrity sha512-e4+UtA5IRO+ha6hYklwj6r7BjiGMxS0O+UaSg9HbaTefg4kMkzj4tXzEBajRR+wkxf+golgAWKzLbytCUDMJAA==
94419441

94429442
uglify-js@^3.1.4, uglify-js@^3.5.1:
94439443
version "3.14.1"

0 commit comments

Comments
 (0)