Skip to content

Commit 28b8d9c

Browse files
committed
fix: nuxt-module-plausible no longer throws an error on navigation
1 parent a65554f commit 28b8d9c

File tree

3 files changed

+29
-24
lines changed

3 files changed

+29
-24
lines changed

modules/nuxt-module-plausible/index.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,10 @@ const plugins = [
2828

2929
// ///////////////////////////////////////////////////////////// registerPlugins
3030
const registerPlugins = () => {
31-
plugins.forEach((plugin) => {
32-
addPlugin(plugin)
33-
console.log('🔌 [nuxt-module-plausible:plugin]')
34-
})
31+
plugins.forEach((plugin) => {
32+
addPlugin(plugin)
33+
console.log('🔌 [nuxt-module-plausible:plugin]')
34+
})
3535
}
3636

3737
// /////////////////////////////////////////////////////////////////////// Setup

modules/nuxt-module-plausible/plugin.js

+24-19
Original file line numberDiff line numberDiff line change
@@ -4,37 +4,42 @@ import { defineNuxtPlugin } from '#imports'
44

55
// ////////////////////////////////////////////////////////////////////// Export
66
// -----------------------------------------------------------------------------
7-
export default defineNuxtPlugin((nuxtApp) => {
7+
export default defineNuxtPlugin(() => {
8+
if (process.server) { return } // only run client-side
89

9-
// Do not fire Plausible if not in production mode
10-
// unless tracking localhost is explicitly enabled in the config
11-
if (process.env.NODE_ENV !== 'production') {
12-
if (!nuxtApp.$config.public.plausible.trackLocalhost) {
13-
return
14-
}
10+
const router = useRouter()
11+
const config = useRuntimeConfig().public
12+
13+
// Only run in production OR if trackLocalhost is explcitly set to true
14+
if (process.env.NODE_ENV !== 'production' || !config.trackLocalhost === true) {
15+
return
1516
}
1617

17-
const router = nuxtApp.$router
18-
const config = nuxtApp.$config.public.siteUrl
18+
console.log('PASS')
19+
1920
let isInitialPageLoad = true
2021

21-
router.afterEach((to) => {
22+
router.afterEach(to => {
23+
24+
console.log(to)
2225

2326
// Ignore initial page because it's fired in the head
2427
if (isInitialPageLoad) {
2528
isInitialPageLoad = false
2629
return
2730
}
2831

29-
// Check if we're on client-side
30-
if (process.client) {
31-
// Track virtual navigation changes
32-
window.plausible = window.plausible || function() {
33-
(window.plausible.q = window.plausible.q || []).push(arguments)
34-
}
35-
window.plausible('pageview', {
36-
url: `${config.public.siteUrl}${to.fullPath}`
37-
})
32+
// Track virtual navigation changes
33+
window.plausible = window.plausible || function () {
34+
(window.plausible.q = window.plausible.q || []).push(arguments)
3835
}
36+
37+
console.log({
38+
url: `${config.siteUrl}${to.fullPath}`
39+
})
40+
41+
window.plausible('pageview', {
42+
url: `${config.siteUrl}${to.fullPath}`
43+
})
3944
})
4045
})

nuxt.config.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ export default defineNuxtConfig({
132132
plausible: {
133133
include: true,
134134
domain: 'singularity.storage',
135-
trackLocalhost: true,
135+
trackLocalhost: false,
136136
autoOutboundTracking: true
137137
},
138138
// //////////////////////////////////////////////////// [Module] @nuxt/content

0 commit comments

Comments
 (0)