Skip to content

Commit 8eba593

Browse files
feat: use /api/__plausible_event__ as proxy default path
1 parent 1e94ec9 commit 8eba593

File tree

3 files changed

+16
-18
lines changed

3 files changed

+16
-18
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ export default defineNuxtConfig({
8383
```
8484

8585
> [!NOTE]
86-
> When enabled, all Plausible events will be sent to your server first, which then forwards them to Plausible's API. The default proxy endpoint is `/api/event`, but you can customize it using the `proxyBaseURL` option.
86+
> When enabled, all Plausible events will be sent to your server first, which then forwards them to Plausible's API. The default proxy endpoint is `/api/__plausible_event__`, but you can customize the path using the `proxyBaseURL` module option.
8787
8888
## Module Options
8989

@@ -99,7 +99,7 @@ export default defineNuxtConfig({
9999
| `autoOutboundTracking` | `boolean` | `false` | Track all outbound link clicks automatically. If enabled, a [MutationObserver](https://developer.mozilla.org/en-US/docs/Web/API/MutationObserver) automagically detects link nodes throughout the application and binds `click` events to them. |
100100
| `logIgnoredEvents` | `boolean` | `false` | Log events to the console if they are ignored. |
101101
| `proxy` | `boolean` | `false` | Whether to proxy the event endpoint through the current origin. |
102-
| `proxyBaseURL` | `string` | `'/api/event'` | The base URL to proxy the event endpoint through. |
102+
| `proxyBaseURL` | `string` | `'/api/__plausible_event__'` | The base URL to proxy the event endpoint through. |
103103

104104
## Composables
105105

src/module.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -100,9 +100,9 @@ export interface ModuleOptions {
100100
proxy?: boolean
101101

102102
/**
103-
* The base URL to proxy the event endpoint through.
103+
* The base URL to proxy the Plausible event endpoint through.
104104
*
105-
* @default '/api/event'
105+
* @default '/api/__plausible_event__'
106106
*/
107107
proxyBaseURL?: string
108108
}
@@ -128,7 +128,7 @@ export default defineNuxtModule<ModuleOptions>({
128128
autoOutboundTracking: false,
129129
logIgnoredEvents: false,
130130
proxy: false,
131-
proxyBaseURL: '/api/event',
131+
proxyBaseURL: '/api/__plausible_event__',
132132
},
133133
setup(options, nuxt) {
134134
const logger = useLogger('plausible')
@@ -160,7 +160,7 @@ export default defineNuxtModule<ModuleOptions>({
160160
nuxt.options.build.transpile.push(resolve('runtime'))
161161

162162
if (nuxt.options.runtimeConfig.public.plausible.proxy) {
163-
const proxyBaseURL = nuxt.options.runtimeConfig.public.plausible.proxyBaseURL
163+
const { proxyBaseURL } = nuxt.options.runtimeConfig.public.plausible
164164

165165
const hasUserProvidedProxyBaseURL
166166
= nuxt.options.serverHandlers.find(handler => handler.route?.startsWith(proxyBaseURL))
@@ -171,7 +171,7 @@ export default defineNuxtModule<ModuleOptions>({
171171

172172
addServerHandler({
173173
route: proxyBaseURL,
174-
handler: resolve('runtime/server/api/event.post'),
174+
handler: resolve('runtime/server/event-handler'),
175175
method: 'post',
176176
})
177177
}

src/runtime/server/api/event.post.ts renamed to src/runtime/server/event-handler.ts

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { ModuleOptions } from '../../../module'
1+
import type { ModuleOptions } from '../../module'
22
import { useRuntimeConfig } from '#imports'
33
import { createError, defineEventHandler, getRequestHeader, getRequestIP, readBody } from 'h3'
44
import { joinURL } from 'ufo'
@@ -18,17 +18,15 @@ export default defineEventHandler(async (event) => {
1818
const target = joinURL(options.apiHost, 'api/event')
1919
const body = await readBody(event)
2020

21-
const headers = new Headers({
22-
'Content-Type': 'application/json',
23-
...Object.fromEntries([
24-
['User-Agent', getRequestHeader(event, 'user-agent')],
25-
['X-Forwarded-For', getRequestIP(event, { xForwardedFor: true })],
26-
].filter(([_, value]) => value != null)),
27-
})
28-
2921
const result = await globalThis.$fetch(target, {
3022
method: 'POST',
31-
headers: headers,
23+
headers: {
24+
'Content-Type': 'application/json',
25+
...Object.fromEntries([
26+
['User-Agent', getRequestHeader(event, 'user-agent')],
27+
['X-Forwarded-For', getRequestIP(event, { xForwardedFor: true })],
28+
].filter(([_, value]) => value != null)),
29+
},
3230
body,
3331
retry: 2,
3432
timeout: 5000,
@@ -43,7 +41,7 @@ export default defineEventHandler(async (event) => {
4341
if (error instanceof Error && error.name === 'FetchError') {
4442
throw createError({
4543
statusCode: 502,
46-
message: 'Failed to reach Plausible analytics service',
44+
message: 'Failed to reach Plausible Analytics service',
4745
})
4846
}
4947

0 commit comments

Comments
 (0)