Skip to content

Commit dada952

Browse files
committed
feat: translate nuxt-module-plausible to nuxt3
1 parent be7765e commit dada952

File tree

4 files changed

+39
-31
lines changed

4 files changed

+39
-31
lines changed
+29-17
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,23 @@
1-
/*
2-
*
3-
* 📦 [Module] NuxtModulePlausible
4-
*
5-
*/
1+
console.log('📦 [load:module] nuxt-module-plausible')
62

73
// ///////////////////////////////////////////////////////////////////// Imports
84
// -----------------------------------------------------------------------------
9-
// ///////////////////////////////////////////////////////////////////// General
105
import Path from 'path'
6+
7+
import { defineNuxtModule, addPlugin } from 'nuxt/kit'
8+
9+
// ////////////////////////////////////////////////////////////////////// Config
10+
// -----------------------------------------------------------------------------
11+
const meta = {
12+
name: '@undone-labs/nuxt-module-plausible',
13+
configKey: 'nuxtModulePlausible',
14+
compatibility: {
15+
nuxt: '^3.0.0'
16+
}
17+
}
18+
19+
// /////////////////////////////////////////////////////////////////// Functions
20+
// -----------------------------------------------------------------------------
1121
// ///////////////////////////////////////////////////////////////////// Plugins
1222
const plugins = [
1323
{
@@ -17,21 +27,23 @@ const plugins = [
1727
]
1828

1929
// ///////////////////////////////////////////////////////////// registerPlugins
20-
const registerPlugins = (instance, next) => {
21-
return new Promise((next) => {
30+
const registerPlugins = () => {
2231
plugins.forEach((plugin) => {
23-
instance.addPlugin(plugin)
32+
addPlugin(plugin)
33+
console.log('🔌 [nuxt-module-plausible:plugin]')
2434
})
25-
if (next) { return next() }
26-
})
2735
}
2836

29-
// ///////////////////////////////////////////////////////////////////// Liftoff
37+
// /////////////////////////////////////////////////////////////////////// Setup
3038
// -----------------------------------------------------------------------------
31-
export default async function () {
32-
if (this.options.plausible.include) {
33-
await registerPlugins(this, () => {
34-
console.log(`📦 [Module] Plausible`)
35-
})
39+
const setup = async (_, nuxt) => {
40+
if (nuxt.options.plausible.include) {
41+
registerPlugins()
3642
}
3743
}
44+
// ////////////////////////////////////////////////////////////////////// Export
45+
// -----------------------------------------------------------------------------
46+
export default defineNuxtModule({
47+
meta,
48+
setup
49+
})

modules/nuxt-module-plausible/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
}
3030
],
3131
"license": "Apache-2.0",
32+
"type": "module",
3233
"bugs": {
3334
"url": "https://github.com/undone-labs/nuxt-module-plausible/issues"
3435
},
@@ -52,4 +53,3 @@
5253
"access": "public"
5354
}
5455
}
55-
+8-13
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,22 @@
1-
/*
2-
*
3-
* 🔌 [Plugin | NuxtModulePlausible] Methods
4-
*
5-
*/
6-
7-
console.log(`🔌 [Plugin | Plausible] Plausible`)
8-
91
// ///////////////////////////////////////////////////////////////////// Imports
102
// -----------------------------------------------------------------------------
11-
import Config from '@/nuxt.config'
3+
import { defineNuxtPlugin, useRuntimeConfig, useRouter } from '#imports'
4+
// import Config from '@/nuxt.config'
125

136
// ////////////////////////////////////////////////////////////////////// Export
147
// -----------------------------------------------------------------------------
15-
export default ({ app }) => {
8+
export default defineNuxtPlugin(() => {
169

1710
// Do not fire Plausible if not in production mode
1811
if (process.env.NODE_ENV !== 'production') {
1912
return
2013
}
2114

15+
const router = useRouter()
16+
const config = useRuntimeConfig()
2217
let isInitialPageLoad = true
2318

24-
app.router.afterEach((to, from) => {
19+
router.afterEach((to) => {
2520

2621
// Ignore initial page because it's fired in the head
2722
if (isInitialPageLoad) {
@@ -36,8 +31,8 @@ export default ({ app }) => {
3631
(window.plausible.q = window.plausible.q || []).push(arguments)
3732
}
3833
window.plausible('pageview', {
39-
url: `https://ecosystem.filecoin.io${to.fullPath}`
34+
url: `${config.public.siteUrl}${to.fullPath}`
4035
})
4136
}
4237
})
43-
}
38+
})

nuxt.config.js

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

0 commit comments

Comments
 (0)