Unverified Commit 7b348fa0 authored by Alex Kirszenberg's avatar Alex Kirszenberg Committed by GitHub

Use ProxiedAsset instead of css_chunk_root_path to expose RSC CSS chunks (#48946)

See https://github.com/vercel/turbo/pull/4740

This enables RSC CSS HMR. The `css_chunk_root_path` way somehow broke
the propagation of invalidation for CSS chunks.

This also updates Turbopack with the following changes:

* https://github.com/vercel/turbo/pull/4738 <!-- Tobias Koppers -
improve commonjs esm interop and node ESM -->
* https://github.com/vercel/turbo/pull/4740 <!-- Alex Kirszenberg -
Replace css_chunk_root_path with ProxiedAsset -->
* https://github.com/vercel/turbo/pull/4741 <!-- Tobias Koppers - add
svg size extraction -->

---------
Co-authored-by: 's avatarTobias Koppers <tobias.koppers@googlemail.com>
parent b3071ad4
This diff is collapsed.
...@@ -42,11 +42,11 @@ swc_relay = { version = "0.2.5" } ...@@ -42,11 +42,11 @@ swc_relay = { version = "0.2.5" }
testing = { version = "0.33.4" } testing = { version = "0.33.4" }
# Turbo crates # Turbo crates
turbo-binding = { git = "https://github.com/vercel/turbo.git", tag = "turbopack-230427.3" } turbo-binding = { git = "https://github.com/vercel/turbo.git", tag = "turbopack-230428.3" }
# [TODO]: need to refactor embed_directory! macro usages, as well as resolving turbo_tasks::function, macros.. # [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-230427.3" } turbo-tasks = { git = "https://github.com/vercel/turbo.git", tag = "turbopack-230428.3" }
# [TODO]: need to refactor embed_directory! macro usage in next-core # [TODO]: need to refactor embed_directory! macro usage in next-core
turbo-tasks-fs = { git = "https://github.com/vercel/turbo.git", tag = "turbopack-230427.3" } turbo-tasks-fs = { git = "https://github.com/vercel/turbo.git", tag = "turbopack-230428.3" }
# General Deps # General Deps
......
...@@ -10,8 +10,8 @@ ...@@ -10,8 +10,8 @@
"check": "tsc --noEmit" "check": "tsc --noEmit"
}, },
"dependencies": { "dependencies": {
"@vercel/turbopack-dev": "https://gitpkg.vercel.app/vercel/turbo/crates/turbopack-dev/js?turbopack-230427.3", "@vercel/turbopack-dev": "https://gitpkg.vercel.app/vercel/turbo/crates/turbopack-dev/js?turbopack-230428.3",
"@vercel/turbopack-node": "https://gitpkg.vercel.app/vercel/turbo/crates/turbopack-node/js?turbopack-230427.3", "@vercel/turbopack-node": "https://gitpkg.vercel.app/vercel/turbo/crates/turbopack-node/js?turbopack-230428.3",
"anser": "^2.1.1", "anser": "^2.1.1",
"css.escape": "^1.5.1", "css.escape": "^1.5.1",
"next": "*", "next": "*",
......
...@@ -4,6 +4,7 @@ import type { ReactElement } from 'react' ...@@ -4,6 +4,7 @@ import type { ReactElement } from 'react'
import { version } from 'next/package.json' import { version } from 'next/package.json'
import { createFromReadableStream } from 'next/dist/compiled/react-server-dom-webpack/client' import { createFromReadableStream } from 'next/dist/compiled/react-server-dom-webpack/client'
import { callServer } from 'next/dist/client/app-call-server' import { callServer } from 'next/dist/client/app-call-server'
import { linkGc } from 'next/dist/client/app-link-gc'
import { HeadManagerContext } from 'next/dist/shared/lib/head-manager-context' import { HeadManagerContext } from 'next/dist/shared/lib/head-manager-context'
...@@ -153,6 +154,8 @@ function hydrate() { ...@@ -153,6 +154,8 @@ function hydrate() {
ReactDOMClient.hydrateRoot(appElement, reactEl) ReactDOMClient.hydrateRoot(appElement, reactEl)
}) })
} }
linkGc()
} }
window.next = { window.next = {
......
...@@ -933,7 +933,6 @@ import {}, {{ chunks as {} }} from "COMPONENT_{}"; ...@@ -933,7 +933,6 @@ import {}, {{ chunks as {} }} from "COMPONENT_{}";
context.compile_time_info().environment(), context.compile_time_info().environment(),
) )
.layer("ssr") .layer("ssr")
.css_chunk_root_path(server_root.join("_next/static/chunks"))
.reference_chunk_source_maps(false) .reference_chunk_source_maps(false)
.build(); .build();
...@@ -1013,7 +1012,6 @@ impl AppRouteVc { ...@@ -1013,7 +1012,6 @@ impl AppRouteVc {
this.context.compile_time_info().environment(), this.context.compile_time_info().environment(),
) )
.layer("ssr") .layer("ssr")
.css_chunk_root_path(this.server_root.join("_next/static/chunks"))
.reference_chunk_source_maps(false) .reference_chunk_source_maps(false)
.build(); .build();
......
...@@ -4,7 +4,7 @@ use turbo_binding::{ ...@@ -4,7 +4,7 @@ use turbo_binding::{
turbo::tasks_fs::FileSystemPathVc, turbo::tasks_fs::FileSystemPathVc,
turbopack::{ turbopack::{
core::{ core::{
asset::{Asset, AssetContentVc, AssetVc}, asset::{Asset, AssetContentVc, AssetVc, AssetsVc},
chunk::{ chunk::{
availability_info::AvailabilityInfo, ChunkItem, ChunkItemVc, ChunkVc, availability_info::AvailabilityInfo, ChunkItem, ChunkItemVc, ChunkVc,
ChunkableAsset, ChunkableAssetReference, ChunkableAssetReferenceVc, ChunkableAsset, ChunkableAssetReference, ChunkableAssetReferenceVc,
...@@ -12,6 +12,7 @@ use turbo_binding::{ ...@@ -12,6 +12,7 @@ use turbo_binding::{
ChunkingTypeOptionVc, ChunkingTypeOptionVc,
}, },
ident::AssetIdentVc, ident::AssetIdentVc,
proxied_asset::ProxiedAssetVc,
reference::{ reference::{
AssetReference, AssetReferenceVc, AssetReferencesVc, SingleAssetReferenceVc, AssetReference, AssetReferenceVc, AssetReferencesVc, SingleAssetReferenceVc,
}, },
...@@ -116,14 +117,44 @@ struct WithClientChunksChunkItem { ...@@ -116,14 +117,44 @@ struct WithClientChunksChunkItem {
#[turbo_tasks::value_impl] #[turbo_tasks::value_impl]
impl WithClientChunksChunkItemVc { impl WithClientChunksChunkItemVc {
#[turbo_tasks::function]
async fn chunks(self) -> Result<AssetsVc> {
let this = self.await?;
let inner = this.inner.await?;
Ok(this
.context
.chunk_group(inner.asset.as_root_chunk(this.context.into())))
}
#[turbo_tasks::function]
async fn client_chunks(self) -> Result<AssetsVc> {
let this = self.await?;
let inner = this.inner.await?;
let chunks = self.chunks();
let output_root = this.context.output_root().await?;
let mut client_chunks = Vec::new();
for chunk in &*chunks.await? {
let extension = chunk.ident().path().extension().await?;
// Only expose CSS chunks as client chunks.
if &*extension == "css" {
if let Some(path) = output_root.get_path_to(&*chunk.ident().path().await?) {
client_chunks
.push(ProxiedAssetVc::new(*chunk, inner.server_root.join(path)).into());
}
}
}
Ok(AssetsVc::cell(client_chunks))
}
#[turbo_tasks::function] #[turbo_tasks::function]
async fn chunks_data(self) -> Result<ChunksDataVc> { async fn chunks_data(self) -> Result<ChunksDataVc> {
let this = self.await?; let this = self.await?;
let inner = this.inner.await?; let inner = this.inner.await?;
Ok(ChunkDataVc::from_assets( Ok(ChunkDataVc::from_assets(
inner.server_root, inner.server_root,
this.context self.client_chunks(),
.chunk_group(inner.asset.as_root_chunk(this.context.into())),
)) ))
} }
} }
...@@ -188,18 +219,12 @@ impl ChunkItem for WithClientChunksChunkItem { ...@@ -188,18 +219,12 @@ impl ChunkItem for WithClientChunksChunkItem {
.cell() .cell()
.into(), .into(),
); );
let group = this let client_chunks = self_vc.client_chunks();
.context let client_chunks = client_chunks.await?;
.chunk_group(inner.asset.as_root_chunk(this.context.into()));
let server_root = inner.server_root.await?;
let chunks = group.await?;
let client_chunk = StringVc::cell("client chunk".to_string()); let client_chunk = StringVc::cell("client chunk".to_string());
for &chunk in chunks.iter() { for &chunk in client_chunks.iter() {
let path = chunk.ident().path().await?;
if path.is_inside(&server_root) {
references.push(SingleAssetReferenceVc::new(chunk, client_chunk).into()); references.push(SingleAssetReferenceVc::new(chunk, client_chunk).into());
} }
}
for chunk_data in &*self_vc.chunks_data().await? { for chunk_data in &*self_vc.chunks_data().await? {
references.extend(chunk_data.references().await?.iter().copied()); references.extend(chunk_data.references().await?.iter().copied());
} }
......
...@@ -10,6 +10,7 @@ import { GlobalLayoutRouterContext } from '../shared/lib/app-router-context' ...@@ -10,6 +10,7 @@ import { GlobalLayoutRouterContext } from '../shared/lib/app-router-context'
import onRecoverableError from './on-recoverable-error' import onRecoverableError from './on-recoverable-error'
import { callServer } from './app-call-server' import { callServer } from './app-call-server'
import { isNextRouterError } from './components/is-next-router-error' import { isNextRouterError } from './components/is-next-router-error'
import { linkGc } from './app-link-gc'
// Since React doesn't call onerror for errors caught in error boundaries. // Since React doesn't call onerror for errors caught in error boundaries.
const origConsoleError = window.console.error const origConsoleError = window.console.error
...@@ -297,55 +298,5 @@ export function hydrate() { ...@@ -297,55 +298,5 @@ export function hydrate() {
reactRoot.render(reactEl) reactRoot.render(reactEl)
} }
// TODO-APP: Remove this logic when Float has GC built-in in development. linkGc()
if (process.env.NODE_ENV !== 'production') {
const callback = (mutationList: MutationRecord[]) => {
for (const mutation of mutationList) {
if (mutation.type === 'childList') {
for (const node of mutation.addedNodes) {
if (
'tagName' in node &&
(node as HTMLLinkElement).tagName === 'LINK'
) {
const link = node as HTMLLinkElement
if (link.dataset.precedence === 'next.js') {
const href = link.getAttribute('href')
if (href) {
const [resource, version] = href.split('?v=')
if (version) {
const allLinks = document.querySelectorAll(
`link[href^="${resource}"]`
) as NodeListOf<HTMLLinkElement>
for (const otherLink of allLinks) {
if (otherLink.dataset.precedence === 'next.js') {
const otherHref = otherLink.getAttribute('href')
if (otherHref) {
const [, otherVersion] = otherHref.split('?v=')
if (!otherVersion || +otherVersion < +version) {
otherLink.remove()
const preloadLink = document.querySelector(
`link[rel="preload"][as="style"][href="${otherHref}"]`
)
if (preloadLink) {
preloadLink.remove()
}
}
}
}
}
}
}
}
}
}
}
}
}
// Create an observer instance linked to the callback function
const observer = new MutationObserver(callback)
observer.observe(document.head, {
childList: true,
})
}
} }
export function linkGc() {
// TODO-APP: Remove this logic when Float has GC built-in in development.
if (process.env.NODE_ENV !== 'production') {
const callback = (mutationList: MutationRecord[]) => {
for (const mutation of mutationList) {
if (mutation.type === 'childList') {
for (const node of mutation.addedNodes) {
if (
'tagName' in node &&
(node as HTMLLinkElement).tagName === 'LINK'
) {
const link = node as HTMLLinkElement
if (link.dataset.precedence === 'next.js') {
const href = link.getAttribute('href')
if (href) {
const [resource, version] = href.split('?v=')
if (version) {
const allLinks = document.querySelectorAll(
`link[href^="${resource}"]`
) as NodeListOf<HTMLLinkElement>
for (const otherLink of allLinks) {
if (otherLink.dataset.precedence === 'next.js') {
const otherHref = otherLink.getAttribute('href')
if (otherHref) {
const [, otherVersion] = otherHref.split('?v=')
if (!otherVersion || +otherVersion < +version) {
otherLink.remove()
const preloadLink = document.querySelector(
`link[rel="preload"][as="style"][href="${otherHref}"]`
)
if (preloadLink) {
preloadLink.remove()
}
}
}
}
}
}
}
}
}
}
}
}
}
// Create an observer instance linked to the callback function
const observer = new MutationObserver(callback)
observer.observe(document.head, {
childList: true,
})
}
}
...@@ -1018,8 +1018,8 @@ importers: ...@@ -1018,8 +1018,8 @@ importers:
'@types/react': 18.0.37 '@types/react': 18.0.37
'@types/react-dom': 18.0.11 '@types/react-dom': 18.0.11
'@vercel/ncc': ^0.36.0 '@vercel/ncc': ^0.36.0
'@vercel/turbopack-dev': https://gitpkg.vercel.app/vercel/turbo/crates/turbopack-dev/js?turbopack-230427.3 '@vercel/turbopack-dev': https://gitpkg.vercel.app/vercel/turbo/crates/turbopack-dev/js?turbopack-230428.3
'@vercel/turbopack-node': https://gitpkg.vercel.app/vercel/turbo/crates/turbopack-node/js?turbopack-230427.3 '@vercel/turbopack-node': https://gitpkg.vercel.app/vercel/turbo/crates/turbopack-node/js?turbopack-230428.3
anser: ^2.1.1 anser: ^2.1.1
css.escape: ^1.5.1 css.escape: ^1.5.1
find-up: ^6.3.0 find-up: ^6.3.0
...@@ -1031,8 +1031,8 @@ importers: ...@@ -1031,8 +1031,8 @@ importers:
stacktrace-parser: ^0.1.10 stacktrace-parser: ^0.1.10
strip-ansi: ^7.0.1 strip-ansi: ^7.0.1
dependencies: dependencies:
'@vercel/turbopack-dev': '@gitpkg.vercel.app/vercel/turbo/crates/turbopack-dev/js?turbopack-230427.3_react-refresh@0.12.0' '@vercel/turbopack-dev': '@gitpkg.vercel.app/vercel/turbo/crates/turbopack-dev/js?turbopack-230428.3_react-refresh@0.12.0'
'@vercel/turbopack-node': '@gitpkg.vercel.app/vercel/turbo/crates/turbopack-node/js?turbopack-230427.3' '@vercel/turbopack-node': '@gitpkg.vercel.app/vercel/turbo/crates/turbopack-node/js?turbopack-230428.3'
anser: 2.1.1 anser: 2.1.1
css.escape: 1.5.1 css.escape: 1.5.1
next: link:../../../../next next: link:../../../../next
...@@ -6002,7 +6002,7 @@ packages: ...@@ -6002,7 +6002,7 @@ packages:
dependencies: dependencies:
'@mdx-js/mdx': 2.2.1 '@mdx-js/mdx': 2.2.1
source-map: 0.7.3 source-map: 0.7.3
webpack: 5.74.0 webpack: 5.74.0_@swc+core@1.3.55
transitivePeerDependencies: transitivePeerDependencies:
- supports-color - supports-color
...@@ -6672,7 +6672,6 @@ packages: ...@@ -6672,7 +6672,6 @@ packages:
cpu: [arm64] cpu: [arm64]
os: [darwin] os: [darwin]
requiresBuild: true requiresBuild: true
dev: true
optional: true optional: true
/@swc/core-darwin-x64/1.3.55: /@swc/core-darwin-x64/1.3.55:
...@@ -6681,7 +6680,6 @@ packages: ...@@ -6681,7 +6680,6 @@ packages:
cpu: [x64] cpu: [x64]
os: [darwin] os: [darwin]
requiresBuild: true requiresBuild: true
dev: true
optional: true optional: true
/@swc/core-linux-arm-gnueabihf/1.3.55: /@swc/core-linux-arm-gnueabihf/1.3.55:
...@@ -6690,7 +6688,6 @@ packages: ...@@ -6690,7 +6688,6 @@ packages:
cpu: [arm] cpu: [arm]
os: [linux] os: [linux]
requiresBuild: true requiresBuild: true
dev: true
optional: true optional: true
/@swc/core-linux-arm64-gnu/1.3.55: /@swc/core-linux-arm64-gnu/1.3.55:
...@@ -6699,7 +6696,6 @@ packages: ...@@ -6699,7 +6696,6 @@ packages:
cpu: [arm64] cpu: [arm64]
os: [linux] os: [linux]
requiresBuild: true requiresBuild: true
dev: true
optional: true optional: true
/@swc/core-linux-arm64-musl/1.3.55: /@swc/core-linux-arm64-musl/1.3.55:
...@@ -6708,7 +6704,6 @@ packages: ...@@ -6708,7 +6704,6 @@ packages:
cpu: [arm64] cpu: [arm64]
os: [linux] os: [linux]
requiresBuild: true requiresBuild: true
dev: true
optional: true optional: true
/@swc/core-linux-x64-gnu/1.3.55: /@swc/core-linux-x64-gnu/1.3.55:
...@@ -6717,7 +6712,6 @@ packages: ...@@ -6717,7 +6712,6 @@ packages:
cpu: [x64] cpu: [x64]
os: [linux] os: [linux]
requiresBuild: true requiresBuild: true
dev: true
optional: true optional: true
/@swc/core-linux-x64-musl/1.3.55: /@swc/core-linux-x64-musl/1.3.55:
...@@ -6726,7 +6720,6 @@ packages: ...@@ -6726,7 +6720,6 @@ packages:
cpu: [x64] cpu: [x64]
os: [linux] os: [linux]
requiresBuild: true requiresBuild: true
dev: true
optional: true optional: true
/@swc/core-win32-arm64-msvc/1.3.55: /@swc/core-win32-arm64-msvc/1.3.55:
...@@ -6735,7 +6728,6 @@ packages: ...@@ -6735,7 +6728,6 @@ packages:
cpu: [arm64] cpu: [arm64]
os: [win32] os: [win32]
requiresBuild: true requiresBuild: true
dev: true
optional: true optional: true
/@swc/core-win32-ia32-msvc/1.3.55: /@swc/core-win32-ia32-msvc/1.3.55:
...@@ -6744,7 +6736,6 @@ packages: ...@@ -6744,7 +6736,6 @@ packages:
cpu: [ia32] cpu: [ia32]
os: [win32] os: [win32]
requiresBuild: true requiresBuild: true
dev: true
optional: true optional: true
/@swc/core-win32-x64-msvc/1.3.55: /@swc/core-win32-x64-msvc/1.3.55:
...@@ -6753,7 +6744,6 @@ packages: ...@@ -6753,7 +6744,6 @@ packages:
cpu: [x64] cpu: [x64]
os: [win32] os: [win32]
requiresBuild: true requiresBuild: true
dev: true
optional: true optional: true
/@swc/core/1.3.55_@swc+helpers@0.5.1: /@swc/core/1.3.55_@swc+helpers@0.5.1:
...@@ -6778,7 +6768,6 @@ packages: ...@@ -6778,7 +6768,6 @@ packages:
'@swc/core-win32-arm64-msvc': 1.3.55 '@swc/core-win32-arm64-msvc': 1.3.55
'@swc/core-win32-ia32-msvc': 1.3.55 '@swc/core-win32-ia32-msvc': 1.3.55
'@swc/core-win32-x64-msvc': 1.3.55 '@swc/core-win32-x64-msvc': 1.3.55
dev: true
/@swc/helpers/0.4.14: /@swc/helpers/0.4.14:
resolution: {integrity: sha512-4C7nX/dvpzB7za4Ql9K81xK3HPxCpHMgwTZVyf+9JQ6VUbn9jjZVN7/Nkdz/Ugzs2CSjqnL/UPXroiVBVHUWUw==} resolution: {integrity: sha512-4C7nX/dvpzB7za4Ql9K81xK3HPxCpHMgwTZVyf+9JQ6VUbn9jjZVN7/Nkdz/Ugzs2CSjqnL/UPXroiVBVHUWUw==}
...@@ -23775,7 +23764,6 @@ packages: ...@@ -23775,7 +23764,6 @@ packages:
source-map: 0.6.1 source-map: 0.6.1
terser: 5.14.1 terser: 5.14.1
webpack: 5.74.0_@swc+core@1.3.55 webpack: 5.74.0_@swc+core@1.3.55
dev: true
/terser-webpack-plugin/5.2.4_webpack@5.74.0: /terser-webpack-plugin/5.2.4_webpack@5.74.0:
resolution: {integrity: sha512-E2CkNMN+1cho04YpdANyRrn8CyN4yMy+WdFKZIySFZrGXZxJwJP6PMNGGc/Mcr6qygQHUUqRxnAPmi0M9f00XA==} resolution: {integrity: sha512-E2CkNMN+1cho04YpdANyRrn8CyN4yMy+WdFKZIySFZrGXZxJwJP6PMNGGc/Mcr6qygQHUUqRxnAPmi0M9f00XA==}
...@@ -25181,7 +25169,6 @@ packages: ...@@ -25181,7 +25169,6 @@ packages:
- '@swc/core' - '@swc/core'
- esbuild - esbuild
- uglify-js - uglify-js
dev: true
/websocket-driver/0.7.3: /websocket-driver/0.7.3:
resolution: {integrity: sha512-bpxWlvbbB459Mlipc5GBzzZwhoZgGEZLuqPaR0INBGnPAY1vdBX6hPnoFXiw+3yWxDuHyQjO2oXTMyS8A5haFg==} resolution: {integrity: sha512-bpxWlvbbB459Mlipc5GBzzZwhoZgGEZLuqPaR0INBGnPAY1vdBX6hPnoFXiw+3yWxDuHyQjO2oXTMyS8A5haFg==}
...@@ -25587,9 +25574,9 @@ packages: ...@@ -25587,9 +25574,9 @@ packages:
/zwitch/2.0.4: /zwitch/2.0.4:
resolution: {integrity: sha512-bXE4cR/kVZhKZX/RjPEflHaKVhUVl85noU3v6b8apfQEc1x4A+zBxjZ4lN8LqGd6WZ3dl98pY4o717VFmoPp+A==} resolution: {integrity: sha512-bXE4cR/kVZhKZX/RjPEflHaKVhUVl85noU3v6b8apfQEc1x4A+zBxjZ4lN8LqGd6WZ3dl98pY4o717VFmoPp+A==}
'@gitpkg.vercel.app/vercel/turbo/crates/turbopack-dev/js?turbopack-230427.3_react-refresh@0.12.0': '@gitpkg.vercel.app/vercel/turbo/crates/turbopack-dev/js?turbopack-230428.3_react-refresh@0.12.0':
resolution: {tarball: https://gitpkg.vercel.app/vercel/turbo/crates/turbopack-dev/js?turbopack-230427.3} resolution: {tarball: https://gitpkg.vercel.app/vercel/turbo/crates/turbopack-dev/js?turbopack-230428.3}
id: '@gitpkg.vercel.app/vercel/turbo/crates/turbopack-dev/js?turbopack-230427.3' id: '@gitpkg.vercel.app/vercel/turbo/crates/turbopack-dev/js?turbopack-230428.3'
name: '@vercel/turbopack-dev' name: '@vercel/turbopack-dev'
version: 0.0.0 version: 0.0.0
dependencies: dependencies:
...@@ -25599,8 +25586,8 @@ packages: ...@@ -25599,8 +25586,8 @@ packages:
- webpack - webpack
dev: false dev: false
'@gitpkg.vercel.app/vercel/turbo/crates/turbopack-node/js?turbopack-230427.3': '@gitpkg.vercel.app/vercel/turbo/crates/turbopack-node/js?turbopack-230428.3':
resolution: {tarball: https://gitpkg.vercel.app/vercel/turbo/crates/turbopack-node/js?turbopack-230427.3} resolution: {tarball: https://gitpkg.vercel.app/vercel/turbo/crates/turbopack-node/js?turbopack-230428.3}
name: '@vercel/turbopack-node' name: '@vercel/turbopack-node'
version: 0.0.0 version: 0.0.0
dependencies: dependencies:
......
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