Unverified Commit 931dcc00 authored by vercel-fleet-prod[bot]'s avatar vercel-fleet-prod[bot] Committed by GitHub

Avoid instrumenting Next.js telemetry requests

Co-authored-by: 's avatarDavid Ilie <david@davidilie.com>
Co-authored-by: 's avatarWill Binns-Smith <755844+wbinnssmith@users.noreply.github.com>
parent 193c88f3
...@@ -45,6 +45,92 @@ describe('postNextTelemetryPayload', () => { ...@@ -45,6 +45,92 @@ describe('postNextTelemetryPayload', () => {
) )
}) })
it('bypasses only the Next.js patched fetch wrapper', async () => {
const originFetch: jest.MockedFunction<typeof fetch> = jest.fn(
async (_input: Parameters<typeof fetch>[0]) =>
new Response(null, { status: 204 })
)
const patchedFetch: jest.MockedFunction<typeof fetch> = jest.fn(
(input, init) => originFetch(input, init)
)
Object.assign(patchedFetch, {
__nextPatched: true,
_nextOriginalFetch: originFetch,
})
global.fetch = patchedFetch
const payload = {
meta: {},
context: {
anonymousId: 'test-id',
projectId: 'test-project',
sessionId: 'test-session',
},
events: [],
}
await postNextTelemetryPayload(payload)
expect(patchedFetch).not.toHaveBeenCalled()
expect(originFetch).toHaveBeenCalledTimes(1)
await global.fetch('https://telemetry.nextjs.org/api/v1/record')
expect(patchedFetch).toHaveBeenCalledTimes(1)
expect(originFetch).toHaveBeenCalledTimes(2)
})
it('does not trust an unbranded _nextOriginalFetch property', async () => {
const unrelatedOriginalFetch = jest.fn(
async () => new Response(null, { status: 204 })
)
const customFetch: jest.MockedFunction<typeof fetch> = jest.fn(
async (_input: Parameters<typeof fetch>[0]) =>
new Response(null, { status: 204 })
)
Object.assign(customFetch, {
_nextOriginalFetch: unrelatedOriginalFetch,
})
global.fetch = customFetch
await postNextTelemetryPayload({
meta: {},
context: {
anonymousId: 'test-id',
projectId: 'test-project',
sessionId: 'test-session',
},
events: [],
})
expect(customFetch).toHaveBeenCalledTimes(1)
expect(unrelatedOriginalFetch).not.toHaveBeenCalled()
})
it('does not trust a non-callable _nextOriginalFetch property', async () => {
const customFetch: jest.MockedFunction<typeof fetch> = jest.fn(
async (_input: Parameters<typeof fetch>[0]) =>
new Response(null, { status: 204 })
)
Object.assign(customFetch, {
__nextPatched: true,
_nextOriginalFetch: 'not a function',
})
global.fetch = customFetch
await postNextTelemetryPayload({
meta: {},
context: {
anonymousId: 'test-id',
projectId: 'test-project',
sessionId: 'test-session',
},
events: [],
})
expect(customFetch).toHaveBeenCalledTimes(1)
})
it('retries on failure', async () => { it('retries on failure', async () => {
const mockFetch = jest const mockFetch = jest
.fn() .fn()
......
...@@ -15,6 +15,23 @@ interface Payload { ...@@ -15,6 +15,23 @@ interface Payload {
}> }>
} }
type MaybePatchedFetch = typeof fetch & {
readonly __nextPatched?: true
readonly _nextOriginalFetch?: typeof fetch
}
function getTelemetryFetch(): typeof fetch {
const currentFetch = globalThis.fetch as MaybePatchedFetch
// Framework telemetry is process activity, not application request work.
// Avoid inheriting an active render's patched-fetch context while preserving
// any user-provided fetch implementation when Next has not patched it.
return currentFetch.__nextPatched === true &&
typeof currentFetch._nextOriginalFetch === 'function'
? currentFetch._nextOriginalFetch
: currentFetch
}
export function postNextTelemetryPayload(payload: Payload, signal?: any) { export function postNextTelemetryPayload(payload: Payload, signal?: any) {
if (!signal && 'timeout' in AbortSignal) { if (!signal && 'timeout' in AbortSignal) {
signal = AbortSignal.timeout(5000) signal = AbortSignal.timeout(5000)
...@@ -22,7 +39,7 @@ export function postNextTelemetryPayload(payload: Payload, signal?: any) { ...@@ -22,7 +39,7 @@ export function postNextTelemetryPayload(payload: Payload, signal?: any) {
return ( return (
retry( retry(
() => () =>
fetch('https://telemetry.nextjs.org/api/v1/record', { getTelemetryFetch()('https://telemetry.nextjs.org/api/v1/record', {
method: 'POST', method: 'POST',
body: JSON.stringify(payload), body: JSON.stringify(payload),
headers: { 'content-type': 'application/json' }, headers: { 'content-type': 'application/json' },
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment