Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ensure next-server prepare only execute once #68616

Merged
merged 2 commits into from
Aug 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions packages/next/src/server/next-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,7 @@ export default class NextNodeServer extends BaseServer<
protected middlewareManifestPath: string
private _serverDistDir: string | undefined
private imageResponseCache?: ResponseCache
private registeredInstrumentation: boolean = false
protected renderWorkersPromises?: Promise<void>
protected dynamicRoutes?: {
match: import('../shared/lib/router/utils/route-matcher').RouteMatchFn
Expand Down Expand Up @@ -324,6 +325,8 @@ export default class NextNodeServer extends BaseServer<
}

protected async runInstrumentationHookIfAvailable() {
if (this.registeredInstrumentation) return
this.registeredInstrumentation = true
await this.instrumentation?.register?.()
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
let count = 0

export function register() {
if (process.env.NEXT_RUNTIME === 'nodejs') {
if (count > 0) {
throw new Error('duplicated-register')
}
console.log('register-log')
count++
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,10 @@ describe('instrumentation-hook - register-once', () => {
await next.fetch('/foo')
expect(next.cliOutput).toIncludeRepeated('register-log', 1)
})

it('should not error when concurrent requests are made', async () => {
await Promise.all([next.fetch('/foo'), next.fetch('/foo')])
expect(next.cliOutput).toIncludeRepeated('register-log', 1)
expect(next.cliOutput).not.toInclude('duplicated-register')
})
})
Loading