Unverified Commit 48725b22 authored by Niklas Mischkulnig's avatar Niklas Mischkulnig Committed by GitHub

Reapply "Turbopack: layout segment optimization for Pages" (#77339) (#77696)

A clean revert of the revert #77339 of #74815

Closes PACK-3715
parent 3d490ce3
This diff is collapsed.
......@@ -895,7 +895,7 @@ impl Project {
}
#[turbo_tasks::function]
pub async fn module_graph_for_entries(
pub async fn module_graph_for_modules(
self: Vc<Self>,
evaluatable_assets: Vc<EvaluatableAssets>,
) -> Result<Vc<ModuleGraph>> {
......@@ -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]
pub async fn whole_app_module_graphs(self: ResolvedVc<Self>) -> Result<Vc<ModuleGraphs>> {
async move {
......
......@@ -23,6 +23,13 @@ use crate::{
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]
pub async fn create_page_ssr_entry_module(
pathname: Vc<RcStr>,
......@@ -34,7 +41,7 @@ pub async fn create_page_ssr_entry_module(
pages_structure: Vc<PagesStructure>,
runtime: NextRuntime,
next_config: Vc<NextConfig>,
) -> Result<Vc<Box<dyn Module>>> {
) -> Result<Vc<PageSsrEntryModule>> {
let definition_page = &*next_original_name.await?;
let definition_pathname = &*pathname.await?;
......@@ -118,28 +125,28 @@ pub async fn create_page_ssr_entry_module(
let pages_structure_ref = pages_structure.await?;
if reference_type == ReferenceType::Entry(EntryReferenceSubType::Page) {
inner_assets.insert(
INNER_DOCUMENT.into(),
process_global_item(
let (app_module, document_module) =
if reference_type == ReferenceType::Entry(EntryReferenceSubType::Page) {
let document_module = process_global_item(
*pages_structure_ref.document,
Value::new(reference_type.clone()),
ssr_module_context,
)
.to_resolved()
.await?,
);
inner_assets.insert(
INNER_APP.into(),
process_global_item(
.await?;
let app_module = process_global_item(
*pages_structure_ref.app,
Value::new(reference_type.clone()),
ssr_module_context,
)
.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
.process(
......@@ -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]
......
......@@ -296,7 +296,7 @@ export async function createHotReloaderTurbopack(
// Always clear the cache, don't check if files have changed
force?: boolean
} = {}
): void {
): boolean {
if (force) {
for (const { path, contentHash } of writtenEndpoint.serverPaths) {
serverPathState.set(path, contentHash)
......@@ -328,7 +328,7 @@ export async function createHotReloaderTurbopack(
}
if (!hasChange) {
return
return false
}
}
......@@ -351,7 +351,7 @@ export async function createHotReloaderTurbopack(
deleteCache(file)
}
return
return true
}
const buildingIds = new Set()
......@@ -601,9 +601,9 @@ export async function createHotReloaderTurbopack(
serverFields,
hooks: {
handleWrittenEndpoint: (id, result) => {
handleWrittenEndpoint: (id, result, forceDeleteCache) => {
currentWrittenEntrypoints.set(id, result)
clearRequireCache(id, result)
return clearRequireCache(id, result, { force: forceDeleteCache })
},
propagateServerField: propagateServerField.bind(null, opts),
sendHmr,
......@@ -997,10 +997,12 @@ export async function createHotReloaderTurbopack(
logErrors: true,
hooks: {
subscribeToChanges,
handleWrittenEndpoint: (id, result) => {
clearRequireCache(id, result)
handleWrittenEndpoint: (id, result, forceDeleteCache) => {
currentWrittenEntrypoints.set(id, result)
assetMapper.setPathsForKey(id, result.clientPaths)
return clearRequireCache(id, result, {
force: forceDeleteCache,
})
},
},
})
......@@ -1061,10 +1063,12 @@ export async function createHotReloaderTurbopack(
hooks: {
subscribeToChanges,
handleWrittenEndpoint: (id, result) => {
handleWrittenEndpoint: (id, result, forceDeleteCache) => {
currentWrittenEntrypoints.set(id, result)
clearRequireCache(id, result)
assetMapper.setPathsForKey(id, result.clientPaths)
return clearRequireCache(id, result, {
force: forceDeleteCache,
})
},
},
})
......
......@@ -98,8 +98,9 @@ export type ChangeSubscriptions = Map<
export type HandleWrittenEndpoint = (
key: EntryKey,
result: TurbopackResult<WrittenEndpoint>
) => void
result: TurbopackResult<WrittenEndpoint>,
forceDeleteCache: boolean
) => boolean
export type StartChangeSubscription = (
key: EntryKey,
......@@ -177,11 +178,17 @@ export async function handleRouteType({
const serverKey = getEntryKey('pages', 'server', page)
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) {
const key = getEntryKey('pages', 'server', '_app')
const writtenEndpoint = await entrypoints.global.app.writeToDisk()
hooks?.handleWrittenEndpoint(key, writtenEndpoint)
documentOrAppChanged ||=
hooks?.handleWrittenEndpoint(key, writtenEndpoint, false) ?? false
processIssues(
currentEntryIssues,
key,
......@@ -198,7 +205,8 @@ export async function handleRouteType({
const writtenEndpoint =
await entrypoints.global.document.writeToDisk()
hooks?.handleWrittenEndpoint(key, writtenEndpoint)
documentOrAppChanged ||=
hooks?.handleWrittenEndpoint(key, writtenEndpoint, false) ?? false
processIssues(
currentEntryIssues,
key,
......@@ -210,7 +218,11 @@ export async function handleRouteType({
await manifestLoader.loadPagesManifest('_document')
const writtenEndpoint = await route.htmlEndpoint.writeToDisk()
hooks?.handleWrittenEndpoint(serverKey, writtenEndpoint)
hooks?.handleWrittenEndpoint(
serverKey,
writtenEndpoint,
documentOrAppChanged
)
const type = writtenEndpoint?.type
......@@ -308,7 +320,7 @@ export async function handleRouteType({
const key = getEntryKey('pages', 'server', page)
const writtenEndpoint = await route.endpoint.writeToDisk()
hooks?.handleWrittenEndpoint(key, writtenEndpoint)
hooks?.handleWrittenEndpoint(key, writtenEndpoint, false)
const type = writtenEndpoint.type
......@@ -333,7 +345,7 @@ export async function handleRouteType({
const key = getEntryKey('app', 'server', page)
const writtenEndpoint = await route.htmlEndpoint.writeToDisk()
hooks?.handleWrittenEndpoint(key, writtenEndpoint)
hooks?.handleWrittenEndpoint(key, writtenEndpoint, false)
if (dev) {
// TODO subscriptions should only be caused by the WebSocket connections
......@@ -396,7 +408,7 @@ export async function handleRouteType({
const key = getEntryKey('app', 'server', page)
const writtenEndpoint = await route.endpoint.writeToDisk()
hooks?.handleWrittenEndpoint(key, writtenEndpoint)
hooks?.handleWrittenEndpoint(key, writtenEndpoint, false)
const type = writtenEndpoint.type
......@@ -654,7 +666,7 @@ export async function handleEntrypoints({
const key = getEntryKey('root', 'server', name)
const writtenEndpoint = await instrumentation[prop].writeToDisk()
dev.hooks.handleWrittenEndpoint(key, writtenEndpoint)
dev.hooks.handleWrittenEndpoint(key, writtenEndpoint, false)
processIssues(currentEntryIssues, key, writtenEndpoint, false, logErrors)
}
await processInstrumentation('instrumentation.nodeJs', 'nodeJs')
......@@ -689,7 +701,7 @@ export async function handleEntrypoints({
async function processMiddleware() {
const writtenEndpoint = await endpoint.writeToDisk()
dev.hooks.handleWrittenEndpoint(key, writtenEndpoint)
dev.hooks.handleWrittenEndpoint(key, writtenEndpoint, false)
processIssues(currentEntryIssues, key, writtenEndpoint, false, logErrors)
await manifestLoader.loadMiddlewareManifest('middleware', 'middleware')
const middlewareConfig =
......@@ -840,7 +852,7 @@ export async function handlePagesErrorRoute({
const key = getEntryKey('pages', 'server', '_app')
const writtenEndpoint = await entrypoints.global.app.writeToDisk()
hooks.handleWrittenEndpoint(key, writtenEndpoint)
hooks.handleWrittenEndpoint(key, writtenEndpoint, false)
hooks.subscribeToChanges(
key,
false,
......@@ -867,7 +879,7 @@ export async function handlePagesErrorRoute({
const key = getEntryKey('pages', 'server', '_document')
const writtenEndpoint = await entrypoints.global.document.writeToDisk()
hooks.handleWrittenEndpoint(key, writtenEndpoint)
hooks.handleWrittenEndpoint(key, writtenEndpoint, false)
hooks.subscribeToChanges(
key,
false,
......@@ -893,7 +905,7 @@ export async function handlePagesErrorRoute({
const key = getEntryKey('pages', 'server', '_error')
const writtenEndpoint = await entrypoints.global.error.writeToDisk()
hooks.handleWrittenEndpoint(key, writtenEndpoint)
hooks.handleWrittenEndpoint(key, writtenEndpoint, false)
hooks.subscribeToChanges(
key,
false,
......
import { retry } from 'next-test-utils'
import { nextTestSetup } from 'e2e-utils'
describe('Client side', () => {
describe('Document and App - Client side', () => {
const { next, isNextDev } = nextTestSetup({
files: __dirname,
skipDeployment: true,
......
import { nextTestSetup } from 'e2e-utils'
describe('With CSP enabled', () => {
describe('Document and App - With CSP enabled', () => {
const { next } = nextTestSetup({
files: __dirname,
})
......
import { retry } from 'next-test-utils'
import { nextTestSetup } from 'e2e-utils'
describe('app-document - Rendering via HTTP', () => {
describe('Document and App - Rendering via HTTP', () => {
const { next, isNextDev } = nextTestSetup({
files: __dirname,
})
......
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