Unverified Commit 618263df authored by Niklas Mischkulnig's avatar Niklas Mischkulnig Committed by GitHub

Turbopack: layout segment optimization for Pages (#74815)

Closes PACK-3715

This also required some changes to the dev loader, as since
https://github.com/vercel/next.js/commit/bc05ce9, app and document are
used from the page bundle itself, and not from the app and document.

~~`_app.js` and `_document.js` are chunked first before the page (this
result is then cached and reused across pages).~~

~~Here, `_document.js` which includes lodash is only chunked once as
part of `page3.js` and the other two reuse that.~~

![Bildschirmfoto 2025-01-13 um 12 03
16](https://github.com/user-attachments/assets/a1149bb3-053a-44dd-a233-47fa4f456a83)
parent d832662b
This diff is collapsed.
...@@ -895,7 +895,7 @@ impl Project { ...@@ -895,7 +895,7 @@ impl Project {
} }
#[turbo_tasks::function] #[turbo_tasks::function]
pub async fn module_graph_for_entries( pub async fn module_graph_for_modules(
self: Vc<Self>, self: Vc<Self>,
evaluatable_assets: Vc<EvaluatableAssets>, evaluatable_assets: Vc<EvaluatableAssets>,
) -> Result<Vc<ModuleGraph>> { ) -> Result<Vc<ModuleGraph>> {
...@@ -912,6 +912,18 @@ impl Project { ...@@ -912,6 +912,18 @@ impl Project {
}) })
} }
#[turbo_tasks::function]
pub async fn module_graph_for_entries(
self: Vc<Self>,
entries: Vc<GraphEntries>,
) -> Result<Vc<ModuleGraph>> {
Ok(if *self.per_page_module_graph().await? {
ModuleGraph::from_modules(entries)
} else {
*self.whole_app_module_graphs().await?.full
})
}
#[turbo_tasks::function] #[turbo_tasks::function]
pub async fn whole_app_module_graphs(self: ResolvedVc<Self>) -> Result<Vc<ModuleGraphs>> { pub async fn whole_app_module_graphs(self: ResolvedVc<Self>) -> Result<Vc<ModuleGraphs>> {
async move { async move {
......
...@@ -23,6 +23,13 @@ use crate::{ ...@@ -23,6 +23,13 @@ use crate::{
util::{file_content_rope, load_next_js_template, NextRuntime}, util::{file_content_rope, load_next_js_template, NextRuntime},
}; };
#[turbo_tasks::value]
pub struct PageSsrEntryModule {
pub ssr_module: ResolvedVc<Box<dyn Module>>,
pub app_module: Option<ResolvedVc<Box<dyn Module>>>,
pub document_module: Option<ResolvedVc<Box<dyn Module>>>,
}
#[turbo_tasks::function] #[turbo_tasks::function]
pub async fn create_page_ssr_entry_module( pub async fn create_page_ssr_entry_module(
pathname: Vc<RcStr>, pathname: Vc<RcStr>,
...@@ -34,7 +41,7 @@ pub async fn create_page_ssr_entry_module( ...@@ -34,7 +41,7 @@ pub async fn create_page_ssr_entry_module(
pages_structure: Vc<PagesStructure>, pages_structure: Vc<PagesStructure>,
runtime: NextRuntime, runtime: NextRuntime,
next_config: Vc<NextConfig>, next_config: Vc<NextConfig>,
) -> Result<Vc<Box<dyn Module>>> { ) -> Result<Vc<PageSsrEntryModule>> {
let definition_page = &*next_original_name.await?; let definition_page = &*next_original_name.await?;
let definition_pathname = &*pathname.await?; let definition_pathname = &*pathname.await?;
...@@ -118,28 +125,28 @@ pub async fn create_page_ssr_entry_module( ...@@ -118,28 +125,28 @@ pub async fn create_page_ssr_entry_module(
let pages_structure_ref = pages_structure.await?; let pages_structure_ref = pages_structure.await?;
if reference_type == ReferenceType::Entry(EntryReferenceSubType::Page) { let (app_module, document_module) =
inner_assets.insert( if reference_type == ReferenceType::Entry(EntryReferenceSubType::Page) {
INNER_DOCUMENT.into(), let document_module = process_global_item(
process_global_item(
*pages_structure_ref.document, *pages_structure_ref.document,
Value::new(reference_type.clone()), Value::new(reference_type.clone()),
ssr_module_context, ssr_module_context,
) )
.to_resolved() .to_resolved()
.await?, .await?;
); let app_module = process_global_item(
inner_assets.insert(
INNER_APP.into(),
process_global_item(
*pages_structure_ref.app, *pages_structure_ref.app,
Value::new(reference_type.clone()), Value::new(reference_type.clone()),
ssr_module_context, ssr_module_context,
) )
.to_resolved() .to_resolved()
.await?, .await?;
); inner_assets.insert(INNER_DOCUMENT.into(), document_module);
} inner_assets.insert(INNER_APP.into(), app_module);
(Some(app_module), Some(document_module))
} else {
(None, None)
};
let mut ssr_module = ssr_module_context let mut ssr_module = ssr_module_context
.process( .process(
...@@ -170,7 +177,12 @@ pub async fn create_page_ssr_entry_module( ...@@ -170,7 +177,12 @@ pub async fn create_page_ssr_entry_module(
} }
} }
Ok(ssr_module) Ok(PageSsrEntryModule {
ssr_module: ssr_module.to_resolved().await?,
app_module,
document_module,
}
.cell())
} }
#[turbo_tasks::function] #[turbo_tasks::function]
......
...@@ -296,7 +296,7 @@ export async function createHotReloaderTurbopack( ...@@ -296,7 +296,7 @@ export async function createHotReloaderTurbopack(
// Always clear the cache, don't check if files have changed // Always clear the cache, don't check if files have changed
force?: boolean force?: boolean
} = {} } = {}
): void { ): boolean {
if (force) { if (force) {
for (const { path, contentHash } of writtenEndpoint.serverPaths) { for (const { path, contentHash } of writtenEndpoint.serverPaths) {
serverPathState.set(path, contentHash) serverPathState.set(path, contentHash)
...@@ -328,7 +328,7 @@ export async function createHotReloaderTurbopack( ...@@ -328,7 +328,7 @@ export async function createHotReloaderTurbopack(
} }
if (!hasChange) { if (!hasChange) {
return return false
} }
} }
...@@ -351,7 +351,7 @@ export async function createHotReloaderTurbopack( ...@@ -351,7 +351,7 @@ export async function createHotReloaderTurbopack(
deleteCache(file) deleteCache(file)
} }
return return true
} }
const buildingIds = new Set() const buildingIds = new Set()
...@@ -601,9 +601,9 @@ export async function createHotReloaderTurbopack( ...@@ -601,9 +601,9 @@ export async function createHotReloaderTurbopack(
serverFields, serverFields,
hooks: { hooks: {
handleWrittenEndpoint: (id, result) => { handleWrittenEndpoint: (id, result, forceDeleteCache) => {
currentWrittenEntrypoints.set(id, result) currentWrittenEntrypoints.set(id, result)
clearRequireCache(id, result) return clearRequireCache(id, result, { force: forceDeleteCache })
}, },
propagateServerField: propagateServerField.bind(null, opts), propagateServerField: propagateServerField.bind(null, opts),
sendHmr, sendHmr,
...@@ -997,10 +997,12 @@ export async function createHotReloaderTurbopack( ...@@ -997,10 +997,12 @@ export async function createHotReloaderTurbopack(
logErrors: true, logErrors: true,
hooks: { hooks: {
subscribeToChanges, subscribeToChanges,
handleWrittenEndpoint: (id, result) => { handleWrittenEndpoint: (id, result, forceDeleteCache) => {
clearRequireCache(id, result)
currentWrittenEntrypoints.set(id, result) currentWrittenEntrypoints.set(id, result)
assetMapper.setPathsForKey(id, result.clientPaths) assetMapper.setPathsForKey(id, result.clientPaths)
return clearRequireCache(id, result, {
force: forceDeleteCache,
})
}, },
}, },
}) })
...@@ -1061,10 +1063,12 @@ export async function createHotReloaderTurbopack( ...@@ -1061,10 +1063,12 @@ export async function createHotReloaderTurbopack(
hooks: { hooks: {
subscribeToChanges, subscribeToChanges,
handleWrittenEndpoint: (id, result) => { handleWrittenEndpoint: (id, result, forceDeleteCache) => {
currentWrittenEntrypoints.set(id, result) currentWrittenEntrypoints.set(id, result)
clearRequireCache(id, result)
assetMapper.setPathsForKey(id, result.clientPaths) assetMapper.setPathsForKey(id, result.clientPaths)
return clearRequireCache(id, result, {
force: forceDeleteCache,
})
}, },
}, },
}) })
......
...@@ -98,8 +98,9 @@ export type ChangeSubscriptions = Map< ...@@ -98,8 +98,9 @@ export type ChangeSubscriptions = Map<
export type HandleWrittenEndpoint = ( export type HandleWrittenEndpoint = (
key: EntryKey, key: EntryKey,
result: TurbopackResult<WrittenEndpoint> result: TurbopackResult<WrittenEndpoint>,
) => void forceDeleteCache: boolean
) => boolean
export type StartChangeSubscription = ( export type StartChangeSubscription = (
key: EntryKey, key: EntryKey,
...@@ -177,11 +178,17 @@ export async function handleRouteType({ ...@@ -177,11 +178,17 @@ export async function handleRouteType({
const serverKey = getEntryKey('pages', 'server', page) const serverKey = getEntryKey('pages', 'server', page)
try { try {
// In the best case scenario, Turbopack chunks document, app, page separately in that order,
// so it can happen that the chunks of document change, but the chunks of app and page
// don't. We still need to reload the page chunks in that case though, otherwise the version
// of the document or app component export from the pages template is stale.
let documentOrAppChanged = false
if (entrypoints.global.app) { if (entrypoints.global.app) {
const key = getEntryKey('pages', 'server', '_app') const key = getEntryKey('pages', 'server', '_app')
const writtenEndpoint = await entrypoints.global.app.writeToDisk() const writtenEndpoint = await entrypoints.global.app.writeToDisk()
hooks?.handleWrittenEndpoint(key, writtenEndpoint) documentOrAppChanged ||=
hooks?.handleWrittenEndpoint(key, writtenEndpoint, false) ?? false
processIssues( processIssues(
currentEntryIssues, currentEntryIssues,
key, key,
...@@ -198,7 +205,8 @@ export async function handleRouteType({ ...@@ -198,7 +205,8 @@ export async function handleRouteType({
const writtenEndpoint = const writtenEndpoint =
await entrypoints.global.document.writeToDisk() await entrypoints.global.document.writeToDisk()
hooks?.handleWrittenEndpoint(key, writtenEndpoint) documentOrAppChanged ||=
hooks?.handleWrittenEndpoint(key, writtenEndpoint, false) ?? false
processIssues( processIssues(
currentEntryIssues, currentEntryIssues,
key, key,
...@@ -210,7 +218,11 @@ export async function handleRouteType({ ...@@ -210,7 +218,11 @@ export async function handleRouteType({
await manifestLoader.loadPagesManifest('_document') await manifestLoader.loadPagesManifest('_document')
const writtenEndpoint = await route.htmlEndpoint.writeToDisk() const writtenEndpoint = await route.htmlEndpoint.writeToDisk()
hooks?.handleWrittenEndpoint(serverKey, writtenEndpoint) hooks?.handleWrittenEndpoint(
serverKey,
writtenEndpoint,
documentOrAppChanged
)
const type = writtenEndpoint?.type const type = writtenEndpoint?.type
...@@ -308,7 +320,7 @@ export async function handleRouteType({ ...@@ -308,7 +320,7 @@ export async function handleRouteType({
const key = getEntryKey('pages', 'server', page) const key = getEntryKey('pages', 'server', page)
const writtenEndpoint = await route.endpoint.writeToDisk() const writtenEndpoint = await route.endpoint.writeToDisk()
hooks?.handleWrittenEndpoint(key, writtenEndpoint) hooks?.handleWrittenEndpoint(key, writtenEndpoint, false)
const type = writtenEndpoint.type const type = writtenEndpoint.type
...@@ -333,7 +345,7 @@ export async function handleRouteType({ ...@@ -333,7 +345,7 @@ export async function handleRouteType({
const key = getEntryKey('app', 'server', page) const key = getEntryKey('app', 'server', page)
const writtenEndpoint = await route.htmlEndpoint.writeToDisk() const writtenEndpoint = await route.htmlEndpoint.writeToDisk()
hooks?.handleWrittenEndpoint(key, writtenEndpoint) hooks?.handleWrittenEndpoint(key, writtenEndpoint, false)
if (dev) { if (dev) {
// TODO subscriptions should only be caused by the WebSocket connections // TODO subscriptions should only be caused by the WebSocket connections
...@@ -396,7 +408,7 @@ export async function handleRouteType({ ...@@ -396,7 +408,7 @@ export async function handleRouteType({
const key = getEntryKey('app', 'server', page) const key = getEntryKey('app', 'server', page)
const writtenEndpoint = await route.endpoint.writeToDisk() const writtenEndpoint = await route.endpoint.writeToDisk()
hooks?.handleWrittenEndpoint(key, writtenEndpoint) hooks?.handleWrittenEndpoint(key, writtenEndpoint, false)
const type = writtenEndpoint.type const type = writtenEndpoint.type
...@@ -654,7 +666,7 @@ export async function handleEntrypoints({ ...@@ -654,7 +666,7 @@ export async function handleEntrypoints({
const key = getEntryKey('root', 'server', name) const key = getEntryKey('root', 'server', name)
const writtenEndpoint = await instrumentation[prop].writeToDisk() const writtenEndpoint = await instrumentation[prop].writeToDisk()
dev.hooks.handleWrittenEndpoint(key, writtenEndpoint) dev.hooks.handleWrittenEndpoint(key, writtenEndpoint, false)
processIssues(currentEntryIssues, key, writtenEndpoint, false, logErrors) processIssues(currentEntryIssues, key, writtenEndpoint, false, logErrors)
} }
await processInstrumentation('instrumentation.nodeJs', 'nodeJs') await processInstrumentation('instrumentation.nodeJs', 'nodeJs')
...@@ -689,7 +701,7 @@ export async function handleEntrypoints({ ...@@ -689,7 +701,7 @@ export async function handleEntrypoints({
async function processMiddleware() { async function processMiddleware() {
const writtenEndpoint = await endpoint.writeToDisk() const writtenEndpoint = await endpoint.writeToDisk()
dev.hooks.handleWrittenEndpoint(key, writtenEndpoint) dev.hooks.handleWrittenEndpoint(key, writtenEndpoint, false)
processIssues(currentEntryIssues, key, writtenEndpoint, false, logErrors) processIssues(currentEntryIssues, key, writtenEndpoint, false, logErrors)
await manifestLoader.loadMiddlewareManifest('middleware', 'middleware') await manifestLoader.loadMiddlewareManifest('middleware', 'middleware')
const middlewareConfig = const middlewareConfig =
...@@ -840,7 +852,7 @@ export async function handlePagesErrorRoute({ ...@@ -840,7 +852,7 @@ export async function handlePagesErrorRoute({
const key = getEntryKey('pages', 'server', '_app') const key = getEntryKey('pages', 'server', '_app')
const writtenEndpoint = await entrypoints.global.app.writeToDisk() const writtenEndpoint = await entrypoints.global.app.writeToDisk()
hooks.handleWrittenEndpoint(key, writtenEndpoint) hooks.handleWrittenEndpoint(key, writtenEndpoint, false)
hooks.subscribeToChanges( hooks.subscribeToChanges(
key, key,
false, false,
...@@ -867,7 +879,7 @@ export async function handlePagesErrorRoute({ ...@@ -867,7 +879,7 @@ export async function handlePagesErrorRoute({
const key = getEntryKey('pages', 'server', '_document') const key = getEntryKey('pages', 'server', '_document')
const writtenEndpoint = await entrypoints.global.document.writeToDisk() const writtenEndpoint = await entrypoints.global.document.writeToDisk()
hooks.handleWrittenEndpoint(key, writtenEndpoint) hooks.handleWrittenEndpoint(key, writtenEndpoint, false)
hooks.subscribeToChanges( hooks.subscribeToChanges(
key, key,
false, false,
...@@ -893,7 +905,7 @@ export async function handlePagesErrorRoute({ ...@@ -893,7 +905,7 @@ export async function handlePagesErrorRoute({
const key = getEntryKey('pages', 'server', '_error') const key = getEntryKey('pages', 'server', '_error')
const writtenEndpoint = await entrypoints.global.error.writeToDisk() const writtenEndpoint = await entrypoints.global.error.writeToDisk()
hooks.handleWrittenEndpoint(key, writtenEndpoint) hooks.handleWrittenEndpoint(key, writtenEndpoint, false)
hooks.subscribeToChanges( hooks.subscribeToChanges(
key, key,
false, false,
......
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