Unverified Commit c9564849 authored by Tim Neutkens's avatar Tim Neutkens Committed by GitHub

Start client compiler when server components are finished compiling (#44716)

parent 9c6d5612
...@@ -121,7 +121,7 @@ export class FlightClientEntryPlugin { ...@@ -121,7 +121,7 @@ export class FlightClientEntryPlugin {
} }
async createClientEntries(compiler: any, compilation: any) { async createClientEntries(compiler: any, compilation: any) {
const promises: Array< const addClientEntryAndSSRModulesList: Array<
ReturnType<typeof this.injectClientEntryAndSSRModules> ReturnType<typeof this.injectClientEntryAndSSRModules>
> = [] > = []
...@@ -210,7 +210,7 @@ export class FlightClientEntryPlugin { ...@@ -210,7 +210,7 @@ export class FlightClientEntryPlugin {
relativeRequest.replace(/\.(js|ts)x?$/, '').replace(/^src[\\/]/, '') relativeRequest.replace(/\.(js|ts)x?$/, '').replace(/^src[\\/]/, '')
) )
promises.push( addClientEntryAndSSRModulesList.push(
this.injectClientEntryAndSSRModules({ this.injectClientEntryAndSSRModules({
compiler, compiler,
compilation, compilation,
...@@ -222,7 +222,7 @@ export class FlightClientEntryPlugin { ...@@ -222,7 +222,7 @@ export class FlightClientEntryPlugin {
} }
// Create internal app // Create internal app
promises.push( addClientEntryAndSSRModulesList.push(
this.injectClientEntryAndSSRModules({ this.injectClientEntryAndSSRModules({
compiler, compiler,
compilation, compilation,
...@@ -366,14 +366,25 @@ export class FlightClientEntryPlugin { ...@@ -366,14 +366,25 @@ export class FlightClientEntryPlugin {
} }
) )
const res = await Promise.all(promises)
// Invalidate in development to trigger recompilation // Invalidate in development to trigger recompilation
const invalidator = getInvalidator() const invalidator = getInvalidator()
// Check if any of the entry injections need an invalidation // Check if any of the entry injections need an invalidation
if (invalidator && res.includes(true)) { if (
invalidator &&
addClientEntryAndSSRModulesList.some(
([shouldInvalidate]) => shouldInvalidate === true
)
) {
invalidator.invalidate([COMPILER_NAMES.client]) invalidator.invalidate([COMPILER_NAMES.client])
} }
// Client compiler is invalidated before awaiting the compilation of the SSR client component entries
// so that the client compiler is running in parallel to the server compiler.
await Promise.all(
addClientEntryAndSSRModulesList.map(
(addClientEntryAndSSRModules) => addClientEntryAndSSRModules[1]
)
)
} }
collectClientComponentsAndCSSForDependency({ collectClientComponentsAndCSSForDependency({
...@@ -475,7 +486,7 @@ export class FlightClientEntryPlugin { ...@@ -475,7 +486,7 @@ export class FlightClientEntryPlugin {
return [clientComponentImports, serverCSSImports] return [clientComponentImports, serverCSSImports]
} }
async injectClientEntryAndSSRModules({ injectClientEntryAndSSRModules({
compiler, compiler,
compilation, compilation,
entryName, entryName,
...@@ -487,7 +498,7 @@ export class FlightClientEntryPlugin { ...@@ -487,7 +498,7 @@ export class FlightClientEntryPlugin {
entryName: string entryName: string
clientComponentImports: ClientComponentImports clientComponentImports: ClientComponentImports
bundlePath: string bundlePath: string
}): Promise<boolean> { }): [shouldInvalidate: boolean, addEntryPromise: Promise<void>] {
let shouldInvalidate = false let shouldInvalidate = false
const loaderOptions: NextFlightClientEntryLoaderOptions = { const loaderOptions: NextFlightClientEntryLoaderOptions = {
...@@ -549,22 +560,25 @@ export class FlightClientEntryPlugin { ...@@ -549,22 +560,25 @@ export class FlightClientEntryPlugin {
} }
) )
// Add the dependency to the server compiler. return [
await this.addEntry( shouldInvalidate,
compilation, // Add the dependency to the server compiler.
// Reuse compilation context. // This promise is awaited later using `Promise.all` in order to parallelize adding the entries.
compiler.context, // It ensures we can parallelize the SSR and Client compiler entries.
clientComponentEntryDep, this.addEntry(
{ compilation,
// By using the same entry name // Reuse compilation context.
name: entryName, compiler.context,
// Layer should be client for the SSR modules clientComponentEntryDep,
// This ensures the client components are bundled on client layer {
layer: WEBPACK_LAYERS.client, // By using the same entry name
} name: entryName,
) // Layer should be client for the SSR modules
// This ensures the client components are bundled on client layer
return shouldInvalidate layer: WEBPACK_LAYERS.client,
}
),
]
} }
addEntry( addEntry(
......
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