-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d07b88e
commit 6b6ef2f
Showing
6 changed files
with
272 additions
and
14 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import { NextResponse } from 'next/server'; | ||
import type { NextFetchEvent, NextRequest } from 'next/server'; | ||
import { config as axiomConfig } from './config'; | ||
import { EndpointType } from './shared'; | ||
|
||
const webVitalsEndpoint = axiomConfig.getIngestURL(EndpointType.webVitals); | ||
const logsEndpoint = axiomConfig.getIngestURL(EndpointType.logs); | ||
|
||
const headers = { | ||
authorization: 'Bearer ' + axiomConfig.token, | ||
'Content-Type': 'application/json', | ||
}; | ||
|
||
export async function middleware(request: NextRequest, event: NextFetchEvent): Promise<NextResponse<unknown> | void> { | ||
// If the request is not for axiom, do nothing | ||
// This is a safety check, as users may add a custom matcher | ||
if (!request.nextUrl.pathname.startsWith('/_axiom')) return; | ||
|
||
// Web vitals | ||
if (request.nextUrl.pathname.startsWith('/_axiom/web-vitals')) { | ||
// Forward the request to the axiom ingest endpoint | ||
event.waitUntil( | ||
fetch(webVitalsEndpoint, { | ||
body: request.body, | ||
method: 'POST', | ||
headers, | ||
}).catch(console.error) | ||
); | ||
|
||
// Return a 204 to the client | ||
return new NextResponse(null, { status: 204 }); | ||
} | ||
|
||
// Logs | ||
if (request.nextUrl.pathname.startsWith('/_axiom/logs')) { | ||
// Forward the request to the axiom ingest endpoint | ||
event.waitUntil( | ||
fetch(logsEndpoint, { | ||
body: request.body, | ||
method: 'POST', | ||
headers, | ||
}).catch(console.error) | ||
); | ||
|
||
// Return a 204 to the client | ||
return new NextResponse(null, { status: 204 }); | ||
} | ||
} | ||
|
||
export const config = { | ||
matcher: '/_axiom/:path*', | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.