Unverified Commit 42c991ff authored by Will Binns-Smith's avatar Will Binns-Smith Committed by GitHub

Prevent unnecessary rsc reloads on navigation (#68049)

In larger apps, when compilation can take time and endpoints are often
build concurrently, `endpoint_server_changed_subscribe`’s subscription
to turbotasks can result in unnecessary calls of the JS callback even
when turbotasks input has not changed.

This wraps subscribing to issues and diagnostics in
`endpoint_server_changed_subscribe` within a turbotasks function that
ensures that inputs are properly consistent (e.g. diagnostics have been
sorted and are value-equal) so this callback is not called unnecessary.

Test Plan: Navigate within a large app and verify
`serverComponentChanges` messages are not sent through the web socket
connection and the browser does not perform unnecessary fetch requests
to rsc endpoints.

### Turbopack Changes

* https://github.com/vercel/turbo/pull/8777 <!-- Tobias Koppers - merge
InProgressDirty into InProgress -->
* https://github.com/vercel/turbo/pull/8785 <!-- Tobias Koppers - fix
watcher bug with file creation -->
* https://github.com/vercel/turbo/pull/8793 <!-- Tobias Koppers - get
rid of notify-debouncer in favor of our own debouncing -->
* https://github.com/vercel/turbo/pull/8805 <!-- Tobias Koppers - impl
Ord for Diagnostics and Issue so they are sorted correctly -->

---------
Co-authored-by: 's avatarTobias Koppers <tobias.koppers@googlemail.com>
parent 668f8edb
This diff is collapsed.
......@@ -38,11 +38,11 @@ swc_core = { version = "0.96.9", features = [
testing = { version = "0.36.0" }
# Turbo crates
turbopack-binding = { git = "https://github.com/vercel/turbo.git", tag = "turbopack-240718.2" }
turbopack-binding = { git = "https://github.com/vercel/turbo.git", tag = "turbopack-240722.3" }
# [TODO]: need to refactor embed_directory! macro usages, as well as resolving turbo_tasks::function, macros..
turbo-tasks = { git = "https://github.com/vercel/turbo.git", tag = "turbopack-240718.2" }
turbo-tasks = { git = "https://github.com/vercel/turbo.git", tag = "turbopack-240722.3" }
# [TODO]: need to refactor embed_directory! macro usage in next-core
turbo-tasks-fs = { git = "https://github.com/vercel/turbo.git", tag = "turbopack-240718.2" }
turbo-tasks-fs = { git = "https://github.com/vercel/turbo.git", tag = "turbopack-240722.3" }
# General Deps
......
......@@ -7,7 +7,7 @@ use next_api::{
route::{Endpoint, WrittenEndpoint},
};
use tracing::Instrument;
use turbo_tasks::{ReadRef, Vc};
use turbo_tasks::{Completion, ReadRef, Vc};
use turbopack_binding::turbopack::core::{
diagnostics::PlainDiagnostic, error::PrettyPrintError, issue::PlainIssue,
};
......@@ -153,29 +153,75 @@ pub fn endpoint_server_changed_subscribe(
func,
move || {
async move {
let changed = endpoint.server_changed();
changed.strongly_consistent().await?;
if issues {
let issues = get_issues(changed).await?;
let diags = get_diagnostics(changed).await?;
Ok((issues, diags))
} else {
Ok((Arc::new(vec![]), Arc::new(vec![])))
}
subscribe_issues_and_diags(endpoint, issues)
.strongly_consistent()
.await
}
.instrument(tracing::info_span!("server changes subscription"))
},
|ctx| {
let (issues, diags) = ctx.value;
let EndpointIssuesAndDiags {
changed: _,
issues,
diagnostics,
} = &*ctx.value;
Ok(vec![TurbopackResult {
result: (),
issues: issues.iter().map(|i| NapiIssue::from(&**i)).collect(),
diagnostics: diags.iter().map(|d| NapiDiagnostic::from(d)).collect(),
diagnostics: diagnostics
.iter()
.map(|d| NapiDiagnostic::from(d))
.collect(),
}])
},
)
}
#[turbo_tasks::value(shared, serialization = "none", eq = "manual")]
struct EndpointIssuesAndDiags {
changed: ReadRef<Completion>,
issues: Arc<Vec<ReadRef<PlainIssue>>>,
diagnostics: Arc<Vec<ReadRef<PlainDiagnostic>>>,
}
impl PartialEq for EndpointIssuesAndDiags {
fn eq(&self, other: &Self) -> bool {
ReadRef::ptr_eq(&self.changed, &other.changed)
&& self.issues == other.issues
&& self.diagnostics == other.diagnostics
}
}
impl Eq for EndpointIssuesAndDiags {}
#[turbo_tasks::function]
async fn subscribe_issues_and_diags(
endpoint: Vc<Box<dyn Endpoint>>,
should_include_issues: bool,
) -> Result<Vc<EndpointIssuesAndDiags>> {
let changed = endpoint.server_changed();
let changed_value = changed.strongly_consistent().await?;
if should_include_issues {
let issues = get_issues(changed).await?;
let diagnostics = get_diagnostics(changed).await?;
Ok(EndpointIssuesAndDiags {
changed: changed_value,
issues,
diagnostics,
}
.cell())
} else {
Ok(EndpointIssuesAndDiags {
changed: changed_value,
issues: Arc::new(vec![]),
diagnostics: Arc::new(vec![]),
}
.cell())
}
}
#[napi(ts_return_type = "{ __napiType: \"RootTask\" }")]
pub fn endpoint_client_changed_subscribe(
#[napi(ts_arg_type = "{ __napiType: \"Endpoint\" }")] endpoint: External<ExternalEndpoint>,
......
......@@ -88,15 +88,16 @@ pub async fn get_issues<T: Send>(source: Vc<T>) -> Result<Arc<Vec<ReadRef<PlainI
/// not consume any Diagnostics held by the source.
pub async fn get_diagnostics<T: Send>(source: Vc<T>) -> Result<Arc<Vec<ReadRef<PlainDiagnostic>>>> {
let captured_diags = source.peek_diagnostics().await?;
let mut diags = captured_diags
.diagnostics
.iter()
.map(|d| d.into_plain())
.try_join()
.await?;
Ok(Arc::new(
captured_diags
.diagnostics
.iter()
.map(|d| d.into_plain())
.try_join()
.await?,
))
diags.sort();
Ok(Arc::new(diags))
}
#[napi(object)]
......
use std::collections::HashMap;
use indexmap::IndexMap;
use turbo_tasks::RcStr;
use turbopack_binding::{
turbo::tasks::Vc,
......@@ -39,7 +38,7 @@ impl Diagnostic for NextFeatureTelemetry {
#[turbo_tasks::function]
fn payload(&self) -> Vc<DiagnosticPayload> {
Vc::cell(HashMap::from([(
Vc::cell(IndexMap::from([(
self.feature_name.clone(),
self.enabled.to_string().into(),
)]))
......@@ -79,7 +78,7 @@ impl Diagnostic for ModuleFeatureTelemetry {
#[turbo_tasks::function]
fn payload(&self) -> Vc<DiagnosticPayload> {
Vc::cell(HashMap::from([(
Vc::cell(IndexMap::from([(
self.feature_name.clone(),
self.invocation_count.to_string().into(),
)]))
......
......@@ -206,7 +206,7 @@
"@types/ws": "8.2.0",
"@vercel/ncc": "0.34.0",
"@vercel/nft": "0.27.1",
"@vercel/turbopack-ecmascript-runtime": "https://gitpkg-fork.vercel.sh/vercel/turbo/crates/turbopack-ecmascript-runtime/js?turbopack-240718.2",
"@vercel/turbopack-ecmascript-runtime": "https://gitpkg-fork.vercel.sh/vercel/turbo/crates/turbopack-ecmascript-runtime/js?turbopack-240722.3",
"acorn": "8.11.3",
"amphtml-validator": "1.0.35",
"anser": "1.4.9",
......
......@@ -1119,8 +1119,8 @@ importers:
specifier: 0.27.1
version: 0.27.1(encoding@0.1.13)
'@vercel/turbopack-ecmascript-runtime':
specifier: https://gitpkg-fork.vercel.sh/vercel/turbo/crates/turbopack-ecmascript-runtime/js?turbopack-240718.2
version: https://gitpkg-fork.vercel.sh/vercel/turbo/crates/turbopack-ecmascript-runtime/js?turbopack-240718.2
specifier: https://gitpkg-fork.vercel.sh/vercel/turbo/crates/turbopack-ecmascript-runtime/js?turbopack-240722.3
version: https://gitpkg-fork.vercel.sh/vercel/turbo/crates/turbopack-ecmascript-runtime/js?turbopack-240722.3
acorn:
specifier: 8.11.3
version: 8.11.3
......@@ -5234,8 +5234,8 @@ packages:
resolution: {integrity: sha512-OTe0KE37F5Y2eTys6eMnfopC+P4qr2ooXUTFyFPTplYSPwowmFk/HLD1FXtbKLjqsIH0SgekcJWad+C5uX4nkg==}
engines: {node: '>=16'}
'@vercel/turbopack-ecmascript-runtime@https://gitpkg-fork.vercel.sh/vercel/turbo/crates/turbopack-ecmascript-runtime/js?turbopack-240718.2':
resolution: {tarball: https://gitpkg-fork.vercel.sh/vercel/turbo/crates/turbopack-ecmascript-runtime/js?turbopack-240718.2}
'@vercel/turbopack-ecmascript-runtime@https://gitpkg-fork.vercel.sh/vercel/turbo/crates/turbopack-ecmascript-runtime/js?turbopack-240722.3':
resolution: {tarball: https://gitpkg-fork.vercel.sh/vercel/turbo/crates/turbopack-ecmascript-runtime/js?turbopack-240722.3}
version: 0.0.0
'@webassemblyjs/ast@1.11.6':
......@@ -19827,7 +19827,7 @@ snapshots:
satori: 0.10.9
yoga-wasm-web: 0.3.3
'@vercel/turbopack-ecmascript-runtime@https://gitpkg-fork.vercel.sh/vercel/turbo/crates/turbopack-ecmascript-runtime/js?turbopack-240718.2':
'@vercel/turbopack-ecmascript-runtime@https://gitpkg-fork.vercel.sh/vercel/turbo/crates/turbopack-ecmascript-runtime/js?turbopack-240722.3':
dependencies:
'@types/node': 20.12.3
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