Skip to content

Instantly share code, notes, and snippets.

@mozeryansky
Last active June 11, 2024 06:42
Show Gist options
  • Save mozeryansky/269bf92676b072e9974a7982833b2d93 to your computer and use it in GitHub Desktop.
Save mozeryansky/269bf92676b072e9974a7982833b2d93 to your computer and use it in GitHub Desktop.
Next.js full url redirect for Sentry and PostHog
// next.config.js
const nextConfig = {
async rewrites() {
return [
// Sentry
{
source: '/sentry(/?)',
has: [
{
type: 'query',
key: 'o',
value: '(?<orgid>\\d*)',
},
{
type: 'query',
key: 'p',
value: '(?<projectid>\\d*)',
},
{
type: 'query',
key: 'r',
value: '(?<region>[a-z]{2})',
},
],
destination:
'https://o:orgid.ingest.:region.sentry.io/api/:projectid/envelope/?hsts=0',
},
{
source: '/sentry(/?)',
has: [
{
type: 'query',
key: 'o',
value: '(?<orgid>\\d*)',
},
{
type: 'query',
key: 'p',
value: '(?<projectid>\\d*)',
},
],
destination:
'https://o:orgid.ingest.sentry.io/api/:projectid/envelope/?hsts=0',
},
// PostHog
{
source: '/posthog/static/:path*',
destination: 'https://us-assets.i.posthog.com/static/:path*',
},
{
source: '/posthog/:path*',
destination: 'https://us.i.posthog.com/:path*',
},
]
},
// This is required to support PostHog trailing slash API requests
skipTrailingSlashRedirect: true,
}
module.exports = nextConfig
@mozeryansky
Copy link
Author

In the main website:

let nextConfig = { ... }
let config = withSentryConfig(nextConfig, {
    ...
    tunnelRoute: 'https://example.com/sentry',
    ...
}
module.exports = {
  ...config,
  async rewrites() {
    return await nextConfig.rewrites()
  },
}
...
posthog.init(process.env.NEXT_PUBLIC_POSTHOG_KEY!, {
  api_host: 'https://example.com/posthog',
  ui_host: 'https://us.i.posthog.com',
})
...

And use the next config in example.com

getsentry/sentry-javascript#12447

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment