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