Skip to content

Commit 8f2dd20

Browse files
authored
Ensure next-server prepare only execute once (#68616)
1 parent c127262 commit 8f2dd20

File tree

3 files changed

+15
-0
lines changed

3 files changed

+15
-0
lines changed

packages/next/src/server/next-server.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,7 @@ export default class NextNodeServer extends BaseServer<
156156
protected middlewareManifestPath: string
157157
private _serverDistDir: string | undefined
158158
private imageResponseCache?: ResponseCache
159+
private registeredInstrumentation: boolean = false
159160
protected renderWorkersPromises?: Promise<void>
160161
protected dynamicRoutes?: {
161162
match: import('../shared/lib/router/utils/route-matcher').RouteMatchFn
@@ -324,6 +325,8 @@ export default class NextNodeServer extends BaseServer<
324325
}
325326

326327
protected async runInstrumentationHookIfAvailable() {
328+
if (this.registeredInstrumentation) return
329+
this.registeredInstrumentation = true
327330
await this.instrumentation?.register?.()
328331
}
329332

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
1+
let count = 0
2+
13
export function register() {
24
if (process.env.NEXT_RUNTIME === 'nodejs') {
5+
if (count > 0) {
6+
throw new Error('duplicated-register')
7+
}
38
console.log('register-log')
9+
count++
410
}
511
}

test/e2e/instrumentation-hook/register-once/register-once.test.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,10 @@ describe('instrumentation-hook - register-once', () => {
1414
await next.fetch('/foo')
1515
expect(next.cliOutput).toIncludeRepeated('register-log', 1)
1616
})
17+
18+
it('should not error when concurrent requests are made', async () => {
19+
await Promise.all([next.fetch('/foo'), next.fetch('/foo')])
20+
expect(next.cliOutput).toIncludeRepeated('register-log', 1)
21+
expect(next.cliOutput).not.toInclude('duplicated-register')
22+
})
1723
})

0 commit comments

Comments
 (0)