4lejandrito / next-plausible

Simple integration for https://nextjs.org and https://plausible.io analytics
https://next-plausible.vercel.app
MIT License
603 stars 33 forks source link

Proxying causes the "event" request to redirect when trailingSlash: true #43

Closed kristjanmar closed 2 years ago

kristjanmar commented 2 years ago

I'm setting up Plausible on my NextJS site (using Vercel).

Using the default settings in next.config.js to set up the proxy:

const { withPlausibleProxy } = require('next-plausible')

module.exports = withPlausibleProxy()({
  // ...your next js config, if any
})

When visiting a page on my site, the page request to /api/event gets 308 redirected from /api/event to /api/event/ because I have "trailingSlash: true" in my next.config.js.

See screenshot:

Screenshot_49

kristjanmar commented 2 years ago

Maybe a "trailingSlash: true" option in the PlausibleProxyOptions would be the simplest way to account for this. I'm sure a lot of NextJS users have the trailingSlash option enabled.

https://nextjs.org/docs/api-reference/next.config.js/trailing-slash

Currently this redirect is causing an extra network request, plus delaying the "event" being tracked by about 100-150ms according to my testing.

Plausible has already mentioned for this in their own NextJS integration docs:

Screenshot_50

4lejandrito commented 2 years ago

Thanks @kristjanmar for reporting this!

I fixed it and published as next-plausible@3.1.9.

Please let me know if it works.

Regards

4lejandrito commented 2 years ago

By the way, you don't have to do anything for this to work. It will automatically pick up the trailingSlash option from nextConfig.

kristjanmar commented 2 years ago

Yes, it is working! Thanks for fixing this so quickly.