Some checks failed
Build & Deploy / 🔍 Prepare (push) Successful in 8s
Build & Deploy / 🧪 QA (push) Failing after 1m13s
Build & Deploy / 🏗️ Build (push) Failing after 5m53s
Build & Deploy / 🚀 Deploy (push) Has been skipped
Build & Deploy / 🧪 Smoke Test (push) Has been skipped
Build & Deploy / ⚡ Lighthouse (push) Has been skipped
Build & Deploy / ♿ WCAG (push) Has been skipped
Build & Deploy / 🛡️ Quality Gates (push) Has been skipped
Build & Deploy / 🔔 Notify (push) Successful in 4s
79 lines
2.5 KiB
TypeScript
79 lines
2.5 KiB
TypeScript
import { NextRequest, NextResponse } from 'next/server';
|
|
import { getServerAppServices } from '@/lib/services/create-services.server';
|
|
import { config } from '@/lib/config';
|
|
|
|
/**
|
|
* Smart Proxy / Relay for Sentry/GlitchTip events.
|
|
*
|
|
* This Route Handler receives Sentry envelopes from the client,
|
|
* injects the correct DSN if needed, and forwards them to the
|
|
* internal GlitchTip/Sentry instance.
|
|
*
|
|
* This hides the real DSN from the client and bypasses ad-blockers
|
|
* that target Sentry's default ingest endpoints.
|
|
*/
|
|
export async function POST(request: NextRequest) {
|
|
const services = getServerAppServices();
|
|
const logger = services.logger.child({ component: 'sentry-relay' });
|
|
|
|
try {
|
|
// Prevent 403 Forbidden console noise in local dev
|
|
if (process.env.NODE_ENV === 'development') {
|
|
return NextResponse.json({ status: 'ignored_in_dev' }, { status: 200 });
|
|
}
|
|
|
|
const envelope = await request.text();
|
|
|
|
// Sentry envelopes can contain multiple parts separated by newlines
|
|
const lines = envelope.split('\n');
|
|
if (lines.length < 1) {
|
|
return NextResponse.json({ error: 'Empty envelope' }, { status: 400 });
|
|
}
|
|
|
|
JSON.parse(lines[0]);
|
|
const realDsn = config.errors.glitchtip.dsn;
|
|
|
|
if (!realDsn) {
|
|
logger.warn('Sentry relay received but no SENTRY_DSN configured on server');
|
|
return NextResponse.json({ status: 'ignored' }, { status: 200 });
|
|
}
|
|
|
|
const dsnUrl = new URL(realDsn);
|
|
const projectId = dsnUrl.pathname.replace('/', '');
|
|
const relayUrl = `${dsnUrl.protocol}//${dsnUrl.host}/api/${projectId}/envelope/`;
|
|
|
|
logger.debug('Relaying Sentry envelope', {
|
|
projectId,
|
|
host: dsnUrl.host,
|
|
});
|
|
|
|
const response = await fetch(relayUrl, {
|
|
method: 'POST',
|
|
body: envelope,
|
|
headers: {
|
|
'Content-Type': 'application/x-sentry-envelope',
|
|
},
|
|
});
|
|
|
|
if (!response.ok) {
|
|
const errorText = await response.text();
|
|
if (!process.env.CI) {
|
|
logger.error('Sentry/GlitchTip API responded with error', {
|
|
status: response.status,
|
|
error: errorText.slice(0, 100),
|
|
});
|
|
}
|
|
return new NextResponse(errorText, { status: response.status });
|
|
}
|
|
|
|
return NextResponse.json({ status: 'ok' });
|
|
} catch (error) {
|
|
if (!process.env.CI) {
|
|
logger.error('Failed to relay Sentry request', {
|
|
error: (error as Error).message,
|
|
});
|
|
}
|
|
return NextResponse.json({ error: 'Internal Server Error' }, { status: 500 });
|
|
}
|
|
}
|