Unverified Commit 775ee37b authored by Niklas Mischkulnig's avatar Niklas Mischkulnig Committed by GitHub

Run instrumentation for Node.js Middleware without Adapter (#95547)

Fixup for #95357

`.handler()` is used by adapters
but `.default()` (=`internalHandler`) is the legacy entrypoint and used
by the non-adapter setup

Also needs https://github.com/vercel/vercel/pull/16944
parent 9097ef6f
......@@ -74,24 +74,7 @@ function errorHandledHandler(fn: AdapterOptions['handler']) {
}
}
const internalHandler: EdgeHandler = (opts) => {
return adapter({
...opts,
IncrementalCache,
incrementalCacheHandler,
page,
handler: errorHandledHandler(handlerUserland),
})
}
export async function handler(
request: Request,
ctx: {
waitUntil?: (prom: Promise<void>) => void
signal?: AbortSignal
requestMeta?: RequestMeta
}
): Promise<Response> {
const internalHandler: EdgeHandler = async (opts) => {
if (process.env.NEXT_RUNTIME !== 'edge') {
// This mirrors what `RouteModule#prepare` does for routes
// edge runtime handles loading instrumentation at the edge adapter level
......@@ -102,9 +85,9 @@ export async function handler(
const absoluteProjectDir = join(
/* turbopackIgnore: true */
process.cwd(),
ctx.requestMeta?.relativeProjectDir || ''
opts.request.requestMeta?.relativeProjectDir || ''
)
const absoluteDistDir = ctx.requestMeta?.distDir
const absoluteDistDir = opts.request.requestMeta?.distDir
const distDir = absoluteDistDir
? relative(absoluteProjectDir, absoluteDistDir)
: '.next'
......@@ -112,6 +95,23 @@ export async function handler(
await ensureInstrumentationRegistered(absoluteProjectDir, distDir)
}
return adapter({
...opts,
IncrementalCache,
incrementalCacheHandler,
page,
handler: errorHandledHandler(handlerUserland),
})
}
export async function handler(
request: Request,
ctx: {
waitUntil?: (prom: Promise<void>) => void
signal?: AbortSignal
requestMeta?: RequestMeta
}
): Promise<Response> {
const result = await internalHandler({
request: {
url: request.url,
......@@ -148,5 +148,5 @@ export async function handler(
return result.response
}
// backwards compat
// backwards compat for non-adapter setups
export default internalHandler
import { nextTestSetup } from 'e2e-utils'
import { check } from 'next-test-utils'
import { retry } from 'next-test-utils'
import path from 'path'
const describeCase = (
......@@ -17,6 +17,7 @@ const describeCase = (
callback(context)
})
}
describe('Instrumentation Hook', () => {
describeCase('with-esm-import', ({ next }) => {
it('with-esm-import should run the instrumentation hook', async () => {
......@@ -103,6 +104,7 @@ describe('Instrumentation Hook', () => {
const page = await next.render('/instrumentation')
expect(page).toContain('Hello')
})
if (isNextDev) {
// TODO: Implement handling for changing the instrument file.
it.skip('should reload the server when the instrumentation hook changes', async () => {
......@@ -111,14 +113,15 @@ describe('Instrumentation Hook', () => {
'./instrumentation.js',
`export function register() {console.log('toast')}`
)
await check(() => next.cliOutput, /toast/)
await retry(() => expect(next.cliOutput).toContain('toast'))
await next.renameFile(
'./instrumentation.js',
'./instrumentation.js.bak'
)
await check(
() => next.cliOutput,
/The instrumentation file has been removed/
await retry(() =>
expect(next.cliOutput).toContain(
'The instrumentation file has been removed'
)
)
await next.patchFile(
'./instrumentation.js.bak',
......@@ -128,8 +131,10 @@ describe('Instrumentation Hook', () => {
'./instrumentation.js.bak',
'./instrumentation.js'
)
await check(() => next.cliOutput, /The instrumentation file was added/)
await check(() => next.cliOutput, /bread/)
await retry(() => {
expect(next.cliOutput).toContain('The instrumentation file was added')
expect(next.cliOutput).toContain('bread')
})
})
}
})
......
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