|
1 |
| -import { NextResponse } from 'next/server'; |
2 |
| -import type { NextFetchEvent, NextRequest } from 'next/server'; |
| 1 | +import { NextMiddleware, NextResponse } from 'next/server'; |
3 | 2 | import { config as axiomConfig } from './config';
|
4 | 3 | import { EndpointType } from './shared';
|
5 | 4 |
|
6 | 5 | const webVitalsEndpoint = axiomConfig.getIngestURL(EndpointType.webVitals);
|
7 | 6 | const logsEndpoint = axiomConfig.getIngestURL(EndpointType.logs);
|
8 | 7 |
|
9 |
| -const headers = { |
10 |
| - authorization: 'Bearer ' + axiomConfig.token, |
11 |
| - 'Content-Type': 'application/json', |
12 |
| -}; |
13 |
| - |
14 |
| -export async function middleware(request: NextRequest, event: NextFetchEvent): Promise<NextResponse<unknown> | void> { |
15 |
| - // If the request is not for axiom, do nothing |
16 |
| - // This is a safety check, as users may add a custom matcher |
17 |
| - if (!request.nextUrl.pathname.startsWith('/_axiom')) return; |
18 |
| - |
19 |
| - // Web vitals |
20 |
| - if (request.nextUrl.pathname.startsWith('/_axiom/web-vitals')) { |
21 |
| - // Forward the request to the axiom ingest endpoint |
22 |
| - event.waitUntil( |
23 |
| - fetch(webVitalsEndpoint, { |
24 |
| - body: request.body, |
25 |
| - method: 'POST', |
26 |
| - headers, |
27 |
| - }).catch(console.error) |
28 |
| - ); |
29 |
| - |
30 |
| - // Return a 204 to the client |
31 |
| - return new NextResponse(null, { status: 204 }); |
32 |
| - } |
33 |
| - |
34 |
| - // Logs |
35 |
| - if (request.nextUrl.pathname.startsWith('/_axiom/logs')) { |
36 |
| - // Forward the request to the axiom ingest endpoint |
37 |
| - event.waitUntil( |
38 |
| - fetch(logsEndpoint, { |
| 8 | +export const axiomMiddleware = |
| 9 | + (middleware?: NextMiddleware): NextMiddleware => |
| 10 | + async (request, event) => { |
| 11 | + // If the request is not for axiom, do nothing |
| 12 | + // This is a safety check, as users may add a custom matcher |
| 13 | + if (!request.nextUrl.pathname.startsWith('/_axiom') || typeof axiomConfig.customEndpoint !== 'undefined') { |
| 14 | + return middleware ? middleware(request, event) : undefined; |
| 15 | + } |
| 16 | + |
| 17 | + const headers = new Headers(request.headers); |
| 18 | + |
| 19 | + // If we send the host and referrer headers, the request will fail |
| 20 | + headers.delete('host'); |
| 21 | + headers.delete('referrer'); |
| 22 | + |
| 23 | + // Add the authorization header |
| 24 | + headers.set('Authorization', 'Bearer ' + axiomConfig.token); |
| 25 | + headers.set('Content-Type', 'application/json'); |
| 26 | + |
| 27 | + let endpoint: string | null = null; |
| 28 | + |
| 29 | + if (request.nextUrl.pathname.startsWith('/_axiom/web-vitals')) { |
| 30 | + endpoint = webVitalsEndpoint; |
| 31 | + } else if (request.nextUrl.pathname.startsWith('/_axiom/logs')) { |
| 32 | + endpoint = logsEndpoint; |
| 33 | + } |
| 34 | + |
| 35 | + // Web vitals |
| 36 | + if (endpoint) { |
| 37 | + const response = fetch(endpoint, { |
39 | 38 | body: request.body,
|
40 | 39 | method: 'POST',
|
41 |
| - headers, |
42 |
| - }).catch(console.error) |
43 |
| - ); |
44 |
| - |
45 |
| - // Return a 204 to the client |
46 |
| - return new NextResponse(null, { status: 204 }); |
47 |
| - } |
48 |
| -} |
49 |
| - |
50 |
| -export const config = { |
51 |
| - matcher: '/_axiom/:path*', |
52 |
| -}; |
| 40 | + headers: headers, |
| 41 | + }).catch(console.error); |
| 42 | + if (typeof event.waitUntil !== 'undefined') { |
| 43 | + // Forward the request to the axiom ingest endpoint |
| 44 | + event.waitUntil(response); |
| 45 | + // Return a 204 to the client because we are not waiting for the response |
| 46 | + return new NextResponse(null, { status: 204 }); |
| 47 | + } else { |
| 48 | + const res = await response; |
| 49 | + if (res) { |
| 50 | + return new NextResponse(res.body, { status: res.status }); |
| 51 | + } |
| 52 | + return new NextResponse(null, { status: 500 }); |
| 53 | + } |
| 54 | + } |
| 55 | + }; |
0 commit comments