Unverified Commit f5054deb authored by Jiachi Liu's avatar Jiachi Liu Committed by GitHub

[next-server] ensure prepare is done before preloading entry (#78454)

parent 06190595
...@@ -1720,9 +1720,11 @@ export default abstract class Server< ...@@ -1720,9 +1720,11 @@ export default abstract class Server<
public async prepare(): Promise<void> { public async prepare(): Promise<void> {
if (this.prepared) return if (this.prepared) return
if (this.preparedPromise === null) { // Get instrumentation module
// Get instrumentation module if (!this.instrumentation) {
this.instrumentation = await this.loadInstrumentationModule() this.instrumentation = await this.loadInstrumentationModule()
}
if (this.preparedPromise === null) {
this.preparedPromise = this.prepareImpl().then(() => { this.preparedPromise = this.prepareImpl().then(() => {
this.prepared = true this.prepared = true
this.preparedPromise = null this.preparedPromise = null
......
...@@ -376,6 +376,9 @@ export default class NextNodeServer extends BaseServer< ...@@ -376,6 +376,9 @@ export default class NextNodeServer extends BaseServer<
} }
public async unstable_preloadEntries(): Promise<void> { public async unstable_preloadEntries(): Promise<void> {
// Ensure prepare process will be finished before preloading entries.
await this.prepare()
const appPathsManifest = this.getAppPathsManifest() const appPathsManifest = this.getAppPathsManifest()
const pagesManifest = this.getPagesManifest() const pagesManifest = this.getPagesManifest()
......
import { ReactNode } from 'react'
export default function Root({ children }: { children: ReactNode }) {
return (
<html>
<body>{children}</body>
</html>
)
}
import { connection } from 'next/server'
console.log('global-side-effect:app-router-page')
export default async function Page() {
await connection()
return <p>hello world</p>
}
import { nextTestSetup } from 'e2e-utils'
import { waitFor } from 'next-test-utils'
const ORDERED_LOGS = [
'instrumentation:side-effect',
'instrumentation:register:begin',
'instrumentation:register:timeout',
'instrumentation:register:end',
'global-side-effect:app-router-page',
]
describe('instrumentation-order', () => {
const { next, isNextDev } = nextTestSetup({
files: __dirname,
skipDeployment: true,
})
it('should work using cheerio', async () => {
// Wait for the timeout in the instrumentation to complete
await waitFor(500)
// Dev mode requires to render the page to trigger the build of the page
if (isNextDev) {
await next.render$('/')
}
const serverLog = next.cliOutput.split('Starting...')[1]
const cliOutputLines = serverLog.split('\n')
const searchedLines = cliOutputLines.filter((line) =>
ORDERED_LOGS.includes(line.trim())
)
expect(searchedLines).toEqual(ORDERED_LOGS)
})
})
console.log('instrumentation:side-effect')
export async function register() {
console.log('instrumentation:register:begin')
await new Promise((resolve) => {
setTimeout(() => {
console.log('instrumentation:register:timeout')
resolve(true)
}, 300)
})
console.log('instrumentation:register:end')
}
/**
* @type {import('next').NextConfig}
*/
const nextConfig = {}
module.exports = nextConfig
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