From 35fd8bcb0388420618a6ffb08ad5e93f716016c1 Mon Sep 17 00:00:00 2001 From: Tobias Koppers Date: Fri, 15 Sep 2023 13:23:39 +0000 Subject: [PATCH] fix pages server side changes by ignoring source maps --- packages/next-swc/crates/next-api/src/app.rs | 17 ++++-- .../crates/next-api/src/middleware.rs | 5 +- .../next-swc/crates/next-api/src/pages.rs | 17 ++++-- .../next-swc/crates/next-api/src/project.rs | 59 ++++++++++++++++++- test/development/basic/next-rs-api.test.ts | 3 +- 5 files changed, 83 insertions(+), 18 deletions(-) diff --git a/packages/next-swc/crates/next-api/src/app.rs b/packages/next-swc/crates/next-api/src/app.rs index 5dcda1a39660a..da21b09f6a8bd 100644 --- a/packages/next-swc/crates/next-api/src/app.rs +++ b/packages/next-swc/crates/next-api/src/app.rs @@ -40,7 +40,6 @@ use turbopack_binding::{ turbopack::{ core::{ asset::{Asset, AssetContent}, - changed::any_content_changed_of_output_assets, chunk::{ChunkableModule, ChunkingContext, EvaluatableAssets}, file_source::FileSource, output::{OutputAsset, OutputAssets}, @@ -905,13 +904,21 @@ impl Endpoint for AppEndpoint { } #[turbo_tasks::function] - fn server_changed(self: Vc) -> Vc { - any_content_changed_of_output_assets(self.output().server_assets()) + async fn server_changed(self: Vc) -> Result> { + Ok(self + .await? + .app_project + .project() + .server_changed(self.output().server_assets())) } #[turbo_tasks::function] - fn client_changed(self: Vc) -> Vc { - any_content_changed_of_output_assets(self.output().client_assets()) + async fn client_changed(self: Vc) -> Result> { + Ok(self + .await? + .app_project + .project() + .client_changed(self.output().client_assets())) } } diff --git a/packages/next-swc/crates/next-api/src/middleware.rs b/packages/next-swc/crates/next-api/src/middleware.rs index a2f365fbafa3a..3de3f0345a12f 100644 --- a/packages/next-swc/crates/next-api/src/middleware.rs +++ b/packages/next-swc/crates/next-api/src/middleware.rs @@ -14,7 +14,6 @@ use turbopack_binding::{ turbopack::{ core::{ asset::AssetContent, - changed::any_content_changed_of_output_assets, chunk::{ChunkableModule, ChunkingContext}, context::AssetContext, module::Module, @@ -209,8 +208,8 @@ impl Endpoint for MiddlewareEndpoint { } #[turbo_tasks::function] - fn server_changed(self: Vc) -> Vc { - any_content_changed_of_output_assets(self.output_assets()) + async fn server_changed(self: Vc) -> Result> { + Ok(self.await?.project.server_changed(self.output_assets())) } #[turbo_tasks::function] diff --git a/packages/next-swc/crates/next-api/src/pages.rs b/packages/next-swc/crates/next-api/src/pages.rs index cebb932770913..a744d2a1c9d6e 100644 --- a/packages/next-swc/crates/next-api/src/pages.rs +++ b/packages/next-swc/crates/next-api/src/pages.rs @@ -37,7 +37,6 @@ use turbopack_binding::{ build::BuildChunkingContext, core::{ asset::AssetContent, - changed::any_content_changed_of_output_assets, chunk::{ChunkableModule, ChunkingContext, EvaluatableAssets}, context::AssetContext, file_source::FileSource, @@ -944,13 +943,21 @@ impl Endpoint for PageEndpoint { } #[turbo_tasks::function] - fn server_changed(self: Vc) -> Vc { - any_content_changed_of_output_assets(self.output().server_assets()) + async fn server_changed(self: Vc) -> Result> { + Ok(self + .await? + .pages_project + .project() + .server_changed(self.output().server_assets())) } #[turbo_tasks::function] - fn client_changed(self: Vc) -> Vc { - any_content_changed_of_output_assets(self.output().client_assets()) + async fn client_changed(self: Vc) -> Result> { + Ok(self + .await? + .pages_project + .project() + .client_changed(self.output().client_assets())) } } diff --git a/packages/next-swc/crates/next-api/src/project.rs b/packages/next-swc/crates/next-api/src/project.rs index 1c2e71d5646a2..ce93149ab885e 100644 --- a/packages/next-swc/crates/next-api/src/project.rs +++ b/packages/next-swc/crates/next-api/src/project.rs @@ -19,8 +19,11 @@ use next_core::{ }; use serde::{Deserialize, Serialize}; use turbo_tasks::{ - debug::ValueDebugFormat, trace::TraceRawVcs, Completion, IntoTraitRef, State, TaskInput, - TransientInstance, Value, Vc, + debug::ValueDebugFormat, + graph::{AdjacencyMap, GraphTraversal}, + trace::TraceRawVcs, + Completion, Completions, IntoTraitRef, State, TaskInput, TransientInstance, TryFlatJoinIterExt, + Value, Vc, }; use turbopack_binding::{ turbo::{ @@ -30,13 +33,14 @@ use turbopack_binding::{ turbopack::{ build::BuildChunkingContext, core::{ + changed::content_changed, chunk::ChunkingContext, compile_time_info::CompileTimeInfo, context::AssetContext, diagnostics::DiagnosticExt, environment::ServerAddr, file_source::FileSource, - output::OutputAssets, + output::{OutputAsset, OutputAssets}, reference_type::{EntryReferenceSubType, ReferenceType}, resolve::{find_context_file, FindContextFileResult}, source::Source, @@ -657,6 +661,55 @@ impl Project { .versioned_content_map .keys_in_path(self.client_relative_path())) } + + /// Completion when server side changes are detected in output assets + /// referenced from the roots + #[turbo_tasks::function] + pub fn server_changed(self: Vc, roots: Vc) -> Vc { + let path = self.node_root(); + any_output_changed(roots, path) + } + + /// Completion when client side changes are detected in output assets + /// referenced from the roots + #[turbo_tasks::function] + pub fn client_changed(self: Vc, roots: Vc) -> Vc { + let path = self.client_root(); + any_output_changed(roots, path) + } +} + +#[turbo_tasks::function] +async fn any_output_changed( + roots: Vc, + path: Vc, +) -> Result> { + let path = &path.await?; + let completions = AdjacencyMap::new() + .skip_duplicates() + .visit(roots.await?.iter().copied(), get_referenced_output_assets) + .await + .completed()? + .into_inner() + .into_reverse_topological() + .map(|m| async move { + let asset_path = m.ident().path().await?; + if !asset_path.path.ends_with(".map") && asset_path.is_inside_ref(path) { + Ok(Some(content_changed(Vc::upcast(m)))) + } else { + Ok(None) + } + }) + .try_flat_join() + .await?; + + Ok(Vc::::cell(completions).completed()) +} + +async fn get_referenced_output_assets( + parent: Vc>, +) -> Result>> + Send> { + Ok(parent.references().await?.clone_value().into_iter()) } #[turbo_tasks::function] diff --git a/test/development/basic/next-rs-api.test.ts b/test/development/basic/next-rs-api.test.ts index 12470803c51c4..c8b0e604152b1 100644 --- a/test/development/basic/next-rs-api.test.ts +++ b/test/development/basic/next-rs-api.test.ts @@ -353,8 +353,7 @@ describe('next.rs api', () => { file: 'pages/index.js', content: pagesIndexCode('hello world2'), expectedUpdate: '/pages/index.js', - // TODO(sokra) this should be false, but source maps change on server side - expectedServerSideChange: true, + expectedServerSideChange: false, }, { name: 'server-side change on a page',