Skip to content

Commit 54f006e

Browse files
refactor: simplify hostname filter
1 parent e2c57f4 commit 54f006e

File tree

1 file changed

+11
-11
lines changed

1 file changed

+11
-11
lines changed

src/runtime/plugin.client.ts

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
1-
import type { PlausibleEventOptions, PlausibleRequestPayload } from '@plausible-analytics/tracker'
1+
import type { PlausibleConfig, PlausibleEventOptions, PlausibleRequestPayload } from '@plausible-analytics/tracker'
22
import type {} from 'nuxt/app'
3-
import type { ModuleOptions } from '../module'
43
import { defineNuxtPlugin, useRuntimeConfig } from '#imports'
54
import { init, track } from '@plausible-analytics/tracker'
65
import { joinURL, withLeadingSlash } from 'ufo'
76

87
export default defineNuxtPlugin({
98
name: 'plausible',
109
setup() {
11-
const { plausible: options } = useRuntimeConfig().public as { plausible: Required<ModuleOptions> }
10+
const { plausible: options } = useRuntimeConfig().public
1211

1312
if (!options.enabled)
1413
return
@@ -26,7 +25,6 @@ export default defineNuxtPlugin({
2625
outboundLinks: options.autoOutboundTracking,
2726
fileDownloads: options.fileDownloads,
2827
formSubmissions: options.formSubmissions,
29-
// The tracker's built-in check also covers 127.x.x.x, [::1], and file: protocol
3028
captureOnLocalhost: !ignoredHostnames.includes('localhost'),
3129
logging: options.logIgnoredEvents,
3230
// Handle non-localhost ignored hostnames (e.g. staging/preview domains)
@@ -50,18 +48,20 @@ export default defineNuxtPlugin({
5048
},
5149
})
5250

53-
function buildHostnameFilter(ignoredHostnames: string[], ignoreSubDomains: boolean) {
54-
const customIgnoredHostnames = ignoredHostnames.filter(h => h !== 'localhost')
51+
function buildHostnameFilter(ignoredHostnames: string[], ignoreSubDomains: boolean): PlausibleConfig['transformRequest'] {
52+
const customIgnoredHostnames = ignoredHostnames.filter(hostname => hostname !== 'localhost')
53+
5554
if (customIgnoredHostnames.length === 0)
56-
return undefined
55+
return
5756

5857
return (payload: PlausibleRequestPayload) => {
59-
const hostname = window.location.hostname
60-
const isIgnored = customIgnoredHostnames.some(ignored =>
58+
const { hostname } = window.location
59+
const isIgnored = customIgnoredHostnames.some(i =>
6160
ignoreSubDomains
62-
? hostname === ignored || hostname.endsWith(`.${ignored}`)
63-
: hostname === ignored,
61+
? hostname === i || hostname.endsWith(`.${i}`)
62+
: hostname === i,
6463
)
64+
6565
return isIgnored ? null : payload
6666
}
6767
}

0 commit comments

Comments
 (0)