Commit a46ce8a2 authored by Niklas Mischkulnig's avatar Niklas Mischkulnig

split up into ssr and rsc paths

parent 7a98924e
...@@ -46,7 +46,8 @@ pub struct NapiWrittenEndpoint { ...@@ -46,7 +46,8 @@ pub struct NapiWrittenEndpoint {
pub r#type: String, pub r#type: String,
pub entry_path: Option<String>, pub entry_path: Option<String>,
pub client_paths: Vec<String>, pub client_paths: Vec<String>,
pub server_paths: Vec<NapiServerPath>, pub rsc_paths: Vec<NapiServerPath>,
pub ssr_paths: Vec<NapiServerPath>,
pub config: NapiEndpointConfig, pub config: NapiEndpointConfig,
} }
...@@ -55,22 +56,26 @@ impl From<Option<EndpointOutputPaths>> for NapiWrittenEndpoint { ...@@ -55,22 +56,26 @@ impl From<Option<EndpointOutputPaths>> for NapiWrittenEndpoint {
match written_endpoint { match written_endpoint {
Some(EndpointOutputPaths::NodeJs { Some(EndpointOutputPaths::NodeJs {
server_entry_path, server_entry_path,
server_paths, rsc_paths,
ssr_paths,
client_paths, client_paths,
}) => Self { }) => Self {
r#type: "nodejs".to_string(), r#type: "nodejs".to_string(),
entry_path: Some(server_entry_path.into_owned()), entry_path: Some(server_entry_path.into_owned()),
client_paths: client_paths.into_iter().map(From::from).collect(), client_paths: client_paths.into_iter().map(From::from).collect(),
server_paths: server_paths.into_iter().map(From::from).collect(), rsc_paths: rsc_paths.into_iter().map(From::from).collect(),
ssr_paths: ssr_paths.into_iter().map(From::from).collect(),
..Default::default() ..Default::default()
}, },
Some(EndpointOutputPaths::Edge { Some(EndpointOutputPaths::Edge {
server_paths, rsc_paths,
ssr_paths,
client_paths, client_paths,
}) => Self { }) => Self {
r#type: "edge".to_string(), r#type: "edge".to_string(),
client_paths: client_paths.into_iter().map(From::from).collect(), client_paths: client_paths.into_iter().map(From::from).collect(),
server_paths: server_paths.into_iter().map(From::from).collect(), rsc_paths: rsc_paths.into_iter().map(From::from).collect(),
ssr_paths: ssr_paths.into_iter().map(From::from).collect(),
..Default::default() ..Default::default()
}, },
Some(EndpointOutputPaths::NotFound) | None => Self { Some(EndpointOutputPaths::NotFound) | None => Self {
......
This diff is collapsed.
...@@ -252,7 +252,8 @@ impl Endpoint for InstrumentationEndpoint { ...@@ -252,7 +252,8 @@ impl Endpoint for InstrumentationEndpoint {
Ok(EndpointOutput { Ok(EndpointOutput {
output_assets: output_assets.to_resolved().await?, output_assets: output_assets.to_resolved().await?,
output_paths: EndpointOutputPaths::Edge { output_paths: EndpointOutputPaths::Edge {
server_paths, rsc_paths: server_paths,
ssr_paths: vec![],
client_paths: vec![], client_paths: vec![],
} }
.resolved_cell(), .resolved_cell(),
......
...@@ -381,7 +381,8 @@ impl Endpoint for MiddlewareEndpoint { ...@@ -381,7 +381,8 @@ impl Endpoint for MiddlewareEndpoint {
Ok(EndpointOutput { Ok(EndpointOutput {
output_paths: EndpointOutputPaths::Edge { output_paths: EndpointOutputPaths::Edge {
server_paths, rsc_paths: server_paths,
ssr_paths: vec![],
client_paths, client_paths,
} }
.resolved_cell(), .resolved_cell(),
......
...@@ -1676,14 +1676,14 @@ impl Endpoint for PageEndpoint { ...@@ -1676,14 +1676,14 @@ impl Endpoint for PageEndpoint {
let node_root = this.pages_project.project().node_root().owned().await?; let node_root = this.pages_project.project().node_root().owned().await?;
let (server_paths, client_paths) = if this let (ssr_paths, client_paths) = if this
.pages_project .pages_project
.project() .project()
.next_mode() .next_mode()
.await? .await?
.is_development() .is_development()
{ {
let server_paths = all_server_paths(output_assets, node_root.clone()) let ssr_paths = all_server_paths(output_assets, node_root.clone())
.owned() .owned()
.await?; .await?;
...@@ -1697,7 +1697,7 @@ impl Endpoint for PageEndpoint { ...@@ -1697,7 +1697,7 @@ impl Endpoint for PageEndpoint {
.owned() .owned()
.instrument(tracing::info_span!("client_paths")) .instrument(tracing::info_span!("client_paths"))
.await?; .await?;
(server_paths, client_paths) (ssr_paths, client_paths)
} else { } else {
(vec![], vec![]) (vec![], vec![])
}; };
...@@ -1718,12 +1718,14 @@ impl Endpoint for PageEndpoint { ...@@ -1718,12 +1718,14 @@ impl Endpoint for PageEndpoint {
EndpointOutputPaths::NodeJs { EndpointOutputPaths::NodeJs {
server_entry_path, server_entry_path,
server_paths, rsc_paths: vec![],
ssr_paths,
client_paths, client_paths,
} }
} }
PageEndpointOutput::Edge { .. } => EndpointOutputPaths::Edge { PageEndpointOutput::Edge { .. } => EndpointOutputPaths::Edge {
server_paths, rsc_paths: vec![],
ssr_paths,
client_paths, client_paths,
}, },
}; };
......
...@@ -148,11 +148,13 @@ pub enum EndpointOutputPaths { ...@@ -148,11 +148,13 @@ pub enum EndpointOutputPaths {
NodeJs { NodeJs {
/// Relative to the root_path /// Relative to the root_path
server_entry_path: RcStr, server_entry_path: RcStr,
server_paths: Vec<ServerPath>, ssr_paths: Vec<ServerPath>,
rsc_paths: Vec<ServerPath>,
client_paths: Vec<RcStr>, client_paths: Vec<RcStr>,
}, },
Edge { Edge {
server_paths: Vec<ServerPath>, ssr_paths: Vec<ServerPath>,
rsc_paths: Vec<ServerPath>,
client_paths: Vec<RcStr>, client_paths: Vec<RcStr>,
}, },
NotFound, NotFound,
......
...@@ -292,6 +292,11 @@ impl ClientReferenceManifest { ...@@ -292,6 +292,11 @@ impl ClientReferenceManifest {
ManifestNodeEntry { ManifestNodeEntry {
name: rcstr!("*"), name: rcstr!("*"),
id: (&*rsc_chunk_item_id).into(), id: (&*rsc_chunk_item_id).into(),
// When a use-cache function runs, it's in the same Node.js context as
// the parent RSC page renderer, so all needed chunks are loaded already
// anyway. So if the use-cache function returns client references in the
// RSC payload that the page context needs, then these modules are
// already loaded.
chunks: vec![], chunks: vec![],
r#async: rsc_is_async, r#async: rsc_is_async,
}, },
......
...@@ -61,7 +61,8 @@ export interface NapiWrittenEndpoint { ...@@ -61,7 +61,8 @@ export interface NapiWrittenEndpoint {
type: string type: string
entryPath?: string entryPath?: string
clientPaths: Array<string> clientPaths: Array<string>
serverPaths: Array<NapiServerPath> rscPaths: Array<NapiServerPath>
ssrPaths: Array<NapiServerPath>
config: NapiEndpointConfig config: NapiEndpointConfig
} }
export declare function endpointWriteToDisk(endpoint: { export declare function endpointWriteToDisk(endpoint: {
......
...@@ -333,22 +333,27 @@ export type WrittenEndpoint = ...@@ -333,22 +333,27 @@ export type WrittenEndpoint =
entryPath: string entryPath: string
/** All client paths that have been written for the endpoint. */ /** All client paths that have been written for the endpoint. */
clientPaths: string[] clientPaths: string[]
/** All server paths that have been written for the endpoint. */ /** All SSR server paths that have been written for the endpoint. */
serverPaths: ServerPath[] ssrPaths: ServerPath[]
/** All RSC server paths that have been written for the endpoint. */
rscPaths: ServerPath[]
config: EndpointConfig config: EndpointConfig
} }
| { | {
type: 'edge' type: 'edge'
/** All client paths that have been written for the endpoint. */ /** All client paths that have been written for the endpoint. */
clientPaths: string[] clientPaths: string[]
/** All server paths that have been written for the endpoint. */ /** All SSR server paths that have been written for the endpoint. */
serverPaths: ServerPath[] ssrPaths: ServerPath[]
/** All RSC server paths that have been written for the endpoint. */
rscPaths: ServerPath[]
config: EndpointConfig config: EndpointConfig
} }
| { | {
type: 'none' type: 'none'
clientPaths: [] clientPaths: []
serverPaths: [] ssrPaths: []
rscPaths: []
config: EndpointConfig config: EndpointConfig
} }
......
...@@ -23,6 +23,7 @@ import type { ...@@ -23,6 +23,7 @@ import type {
TurbopackResult, TurbopackResult,
Project, Project,
Entrypoints, Entrypoints,
ServerPath,
} from '../../build/swc/types' } from '../../build/swc/types'
import { createDefineEnv } from '../../build/swc' import { createDefineEnv } from '../../build/swc'
import * as Log from '../../build/output/log' import * as Log from '../../build/output/log'
...@@ -312,64 +313,77 @@ export async function createHotReloaderTurbopack( ...@@ -312,64 +313,77 @@ export async function createHotReloaderTurbopack(
force?: boolean force?: boolean
} = {} } = {}
): boolean { ): boolean {
if (force) { function shouldClear(paths: ServerPath[]): boolean {
for (const { path, contentHash } of writtenEndpoint.serverPaths) { if (force) {
// We ignore source maps for (const { path, contentHash } of paths) {
if (path.endsWith('.map')) continue // We ignore source maps
const localKey = `${key}:${path}` if (path.endsWith('.map')) continue
serverPathState.set(localKey, contentHash) const localKey = `${key}:${path}`
serverPathState.set(path, contentHash)
}
} else {
// Figure out if the server files have changed
let hasChange = false
for (const { path, contentHash } of writtenEndpoint.serverPaths) {
// We ignore source maps
if (path.endsWith('.map')) continue
const localKey = `${key}:${path}`
const localHash = serverPathState.get(localKey)
const globalHash = serverPathState.get(path)
if (
(localHash && localHash !== contentHash) ||
(globalHash && globalHash !== contentHash)
) {
hasChange = true
serverPathState.set(localKey, contentHash) serverPathState.set(localKey, contentHash)
serverPathState.set(path, contentHash) serverPathState.set(path, contentHash)
} else { }
if (!localHash) { return true
} else {
// Figure out if the server files have changed
let hasChange = false
for (const { path, contentHash } of paths) {
// We ignore source maps
if (path.endsWith('.map')) continue
const localKey = `${key}:${path}`
const localHash = serverPathState.get(localKey)
const globalHash = serverPathState.get(path)
if (
(localHash && localHash !== contentHash) ||
(globalHash && globalHash !== contentHash)
) {
hasChange = true
serverPathState.set(localKey, contentHash) serverPathState.set(localKey, contentHash)
}
if (!globalHash) {
serverPathState.set(path, contentHash) serverPathState.set(path, contentHash)
} else {
if (!localHash) {
serverPathState.set(localKey, contentHash)
}
if (!globalHash) {
serverPathState.set(path, contentHash)
}
} }
} }
}
if (!hasChange) { return hasChange
return false
} }
} }
resetFetch() let hasCleared = false
for (const paths of [writtenEndpoint.ssrPaths, writtenEndpoint.rscPaths]) {
let x = shouldClear(paths)
console.log(
key,
x,
paths === writtenEndpoint.ssrPaths ? 'ssr' : 'rsc',
writtenEndpoint
)
if (x) {
hasCleared = true
// Not available in: resetFetch()
// - Pages Router (no server-side HMR)
// - Edge Runtime (uses browser runtime which already disposes chunks individually)
if (typeof __next__clear_chunk_cache__ === 'function') {
__next__clear_chunk_cache__()
}
const serverPaths = writtenEndpoint.serverPaths.map(({ path: p }) => // Not available in:
join(distDir, p) // - Pages Router (no server-side HMR)
) // - Edge Runtime (uses browser runtime which already disposes chunks individually)
if (typeof __next__clear_chunk_cache__ === 'function') {
__next__clear_chunk_cache__()
}
const serverPaths = paths.map(({ path: p }) => join(distDir, p))
for (const file of serverPaths) { for (const file of serverPaths) {
clearModuleContext(file) clearModuleContext(file)
deleteCache(file) deleteCache(file)
}
}
} }
return true return hasCleared
} }
const buildingIds = new Set() const buildingIds = new Set()
......
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