Unverified Commit 3e5e6dba authored by Tim Neutkens's avatar Tim Neutkens Committed by GitHub

Turbopack: Implement deploymentId (#76904)

Builds on #76814

Closes PACK-4071 (implementing config.deploymentId)

This uses the new `__turbopack_load_by_url__` to load chunks through
client references in react-dom-turbopack.

After this lands I'm going to send a PR to react-dom-turbopack to switch
the original source to use `__turbopack_load_by_url__` too, landing this
PR first to make sure it passes all the Next.js tests.

<!-- Thanks for opening a PR! Your contribution is much appreciated.
To make sure your PR is handled as smoothly as possible we request that
you follow the checklist sections below.
Choose the right checklist for the change(s) that you're making:

## For Contributors

### Improving Documentation

- Run `pnpm prettier-fix` to fix formatting issues before opening the
PR.
- Read the Docs Contribution Guide to ensure your contribution follows
the docs guidelines:
https://nextjs.org/docs/community/contribution-guide

### Adding or Updating Examples

- The "examples guidelines" are followed from our contributing doc
https://github.com/vercel/next.js/blob/canary/contributing/examples/adding-examples.md
- Make sure the linting passes by running `pnpm build && pnpm lint`. See
https://github.com/vercel/next.js/blob/canary/contributing/repository/linting.md

### Fixing a bug

- Related issues linked using `fixes #number`
- Tests added. See:
https://github.com/vercel/next.js/blob/canary/contributing/core/testing.md#writing-tests-for-nextjs
- Errors have a helpful link attached, see
https://github.com/vercel/next.js/blob/canary/contributing.md

### Adding a feature

- Implements an existing feature request or RFC. Make sure the feature
request has been accepted for implementation before opening a PR. (A
discussion must be opened, see
https://github.com/vercel/next.js/discussions/new?category=ideas)
- Related issues/discussions are linked using `fixes #number`
- e2e tests added
(https://github.com/vercel/next.js/blob/canary/contributing/core/testing.md#writing-tests-for-nextjs)
- Documentation added
- Telemetry added. In case of a feature if it's used or not.
- Errors have a helpful link attached, see
https://github.com/vercel/next.js/blob/canary/contributing.md


## For Maintainers

- Minimal description (aim for explaining to someone not on the team to
understand the PR)
- When linking to a Slack thread, you might want to share details of the
conclusion
- Link both the Linear (Fixes NEXT-xxx) and the GitHub issues
- Add review comments if necessary to explain to the reviewer the logic
behind a change

### What?

### Why?

### How?

Closes NEXT-
Fixes #

-->
parent 823484a7
...@@ -4482,12 +4482,14 @@ dependencies = [ ...@@ -4482,12 +4482,14 @@ dependencies = [
"futures", "futures",
"indexmap 2.7.1", "indexmap 2.7.1",
"indoc", "indoc",
"itertools 0.10.5",
"lazy-regex", "lazy-regex",
"lazy_static", "lazy_static",
"mime_guess", "mime_guess",
"modularize_imports", "modularize_imports",
"next-custom-transforms", "next-custom-transforms",
"once_cell", "once_cell",
"percent-encoding",
"qstring", "qstring",
"react_remove_properties", "react_remove_properties",
"regex", "regex",
......
...@@ -34,8 +34,9 @@ tracing = { workspace = true } ...@@ -34,8 +34,9 @@ tracing = { workspace = true }
rustc-hash = { workspace = true } rustc-hash = { workspace = true }
react_remove_properties = "0.33.0" react_remove_properties = "0.33.0"
remove_console = "0.34.0" remove_console = "0.34.0"
itertools = { workspace = true }
auto-hash-map = { workspace = true } auto-hash-map = { workspace = true }
percent-encoding = "2.3.1"
swc_core = { workspace = true, features = [ swc_core = { workspace = true, features = [
"base", "base",
......
use anyhow::Result; use anyhow::Result;
use either::Either; use either::Either;
use indoc::formatdoc; use indoc::formatdoc;
use itertools::Itertools;
use rustc_hash::FxHashMap; use rustc_hash::FxHashMap;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use tracing::Instrument; use tracing::Instrument;
...@@ -25,6 +26,7 @@ use crate::{ ...@@ -25,6 +26,7 @@ use crate::{
next_app::ClientReferencesChunks, next_app::ClientReferencesChunks,
next_client_reference::{ClientReferenceGraphResult, ClientReferenceType}, next_client_reference::{ClientReferenceGraphResult, ClientReferenceType},
next_config::NextConfig, next_config::NextConfig,
next_manifests::encode_uri_component::encode_uri_component,
util::NextRuntime, util::NextRuntime,
}; };
...@@ -83,7 +85,8 @@ impl ClientReferenceManifest { ...@@ -83,7 +85,8 @@ impl ClientReferenceManifest {
.map(|p| p.to_string()) .map(|p| p.to_string())
.unwrap_or("".into()); .unwrap_or("".into());
entry_manifest.module_loading.prefix = prefix_path; // TODO: Add `suffix` to the manifest for React to use.
// entry_manifest.module_loading.prefix = prefix_path;
entry_manifest.module_loading.cross_origin = next_config entry_manifest.module_loading.cross_origin = next_config
.await? .await?
...@@ -200,9 +203,16 @@ impl ClientReferenceManifest { ...@@ -200,9 +203,16 @@ impl ClientReferenceManifest {
.map(ToString::to_string) .map(ToString::to_string)
}) })
// It's possible that a chunk also emits CSS files, that will // It's possible that a chunk also emits CSS files, that will
// be handled separatedly. // be handled separately.
.filter(|path| path.ends_with(".js")) .filter(|path| path.ends_with(".js"))
.map(|path| format!("{}{}", path, suffix_path)) .map(|path| {
format!(
"{}{}{}",
prefix_path,
path.split('/').map(encode_uri_component).format("/"),
suffix_path
)
})
.map(RcStr::from) .map(RcStr::from)
.collect::<Vec<_>>(); .collect::<Vec<_>>();
......
use percent_encoding::{utf8_percent_encode, AsciiSet, CONTROLS};
/// An `AsciiSet` that matches the behavior of JavaScript's `encodeURIComponent`.
/// - It leaves `A-Z a-z 0-9 - _ . ~` unescaped.
/// - It percent-encodes all other ASCII characters (and of course non-ASCII).
/// - The `CONTROLS` set covers `\0`-`\x1F` and `\x7F`.
const ENCODE_URI_COMPONENT_SET: &AsciiSet = &CONTROLS
// Add everything else JS `encodeURIComponent` would encode:
.add(b' ')
// .add(b'!')
.add(b'"')
.add(b'#')
.add(b'$')
.add(b'%')
.add(b'&')
.add(b'\'')
// .add(b'(')
// .add(b')')
.add(b'*')
.add(b'+')
.add(b',')
.add(b'/')
.add(b':')
.add(b';')
.add(b'<')
.add(b'=')
.add(b'>')
.add(b'?')
.add(b'@')
.add(b'[')
.add(b'\\')
.add(b']')
.add(b'^')
.add(b'`')
.add(b'{')
.add(b'|')
// .add(b'~');
.add(b'}');
pub fn encode_uri_component(input: &str) -> String {
utf8_percent_encode(input, ENCODE_URI_COMPONENT_SET).to_string()
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_encode_uri_component() {
// Each (input, expected_output)
let test_cases = vec![
("Hello", "Hello"),
("Hello World", "Hello%20World"),
(
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_.~",
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_.~",
),
("This is 100% test!", "This%20is%20100%25%20test!"),
("你好", "%E4%BD%A0%E5%A5%BD"),
(
"こんにちは世界",
"%E3%81%93%E3%82%93%E3%81%AB%E3%81%A1%E3%81%AF%E4%B8%96%E7%95%8C",
),
("foo bar@baz.com", "foo%20bar%40baz.com"),
("#$&+,/:;=?@", "%23%24%26%2B%2C%2F%3A%3B%3D%3F%40"),
("éléphant", "%C3%A9l%C3%A9phant"),
("𐍈", "%F0%90%8D%88"),
("A B C", "A%20%20%20B%20%20%20%20C"),
("foo\nbar", "foo%0Abar"),
("foo\rbar", "foo%0Dbar"),
("foo\r\nbar", "foo%0D%0Abar"),
("Hello\u{200B}World", "Hello%E2%80%8BWorld"),
("a\u{0301}", "a%CC%81"),
("🏳️‍🌈", "%F0%9F%8F%B3%EF%B8%8F%E2%80%8D%F0%9F%8C%88"),
(
"👩‍❤️‍💋‍👩",
"%F0%9F%91%A9%E2%80%8D%E2%9D%A4%EF%B8%8F%E2%80%8D%F0%9F%92%8B%E2%80%8D%F0%9F%91%A9",
),
(
"Cafe\u{0301} / résumé & co.",
"Cafe%CC%81%20%2F%20r%C3%A9sum%C3%A9%20%26%20co.",
),
(
"السلام عليكم!?",
"%D8%A7%D9%84%D8%B3%D9%84%D8%A7%D9%85%20%D8%B9%D9%84%D9%8A%D9%83%D9%85!%3F",
),
(
"你好 world! ~测试~ (mixed)",
"%E4%BD%A0%E5%A5%BD%20world!%20~%E6%B5%8B%E8%AF%95~%20(mixed)",
),
(
"a\u{0301} 🇺🇳 e\u{0301} 🍏",
"a%CC%81%20%F0%9F%87%BA%F0%9F%87%B3%20e%CC%81%20%F0%9F%8D%8F",
),
("𐍈𐍇", "%F0%90%8D%88%F0%90%8D%87"),
(
"<<<>>>%%%$$$###@@@",
"%3C%3C%3C%3E%3E%3E%25%25%25%24%24%24%23%23%23%40%40%40",
),
("[some]{text}(here)<>", "%5Bsome%5D%7Btext%7D(here)%3C%3E"),
(
"Test\r\ntest 🐱\n\rSecond line \u{200B} end",
"Test%0D%0Atest%20%F0%9F%90%B1%0A%0DSecond%20line%20%E2%80%8B%20end",
),
];
for (input, expected) in test_cases {
let actual = encode_uri_component(input);
assert_eq!(
actual, expected,
"Failed on input='{input}': expected='{expected}', got='{actual}'",
);
}
}
}
//! Type definitions for the Next.js manifest formats. //! Type definitions for the Next.js manifest formats.
pub mod client_reference_manifest; pub mod client_reference_manifest;
mod encode_uri_component;
use anyhow::{Context, Result}; use anyhow::{Context, Result};
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
......
...@@ -87,7 +87,7 @@ ...@@ -87,7 +87,7 @@
var chunkFilename = chunks[i], var chunkFilename = chunks[i],
entry = chunkCache.get(chunkFilename); entry = chunkCache.get(chunkFilename);
if (void 0 === entry) { if (void 0 === entry) {
entry = __turbopack_load__(chunkFilename); entry = __turbopack_load_by_url__(chunkFilename);
promises.push(entry); promises.push(entry);
var resolve = chunkCache.set.bind(chunkCache, chunkFilename, null); var resolve = chunkCache.set.bind(chunkCache, chunkFilename, null);
entry.then(resolve, ignoreReject); entry.then(resolve, ignoreReject);
......
...@@ -73,7 +73,7 @@ function preloadModule(metadata) { ...@@ -73,7 +73,7 @@ function preloadModule(metadata) {
var chunkFilename = chunks[i], var chunkFilename = chunks[i],
entry = chunkCache.get(chunkFilename); entry = chunkCache.get(chunkFilename);
if (void 0 === entry) { if (void 0 === entry) {
entry = __turbopack_load__(chunkFilename); entry = __turbopack_load_by_url__(chunkFilename);
promises.push(entry); promises.push(entry);
var resolve = chunkCache.set.bind(chunkCache, chunkFilename, null); var resolve = chunkCache.set.bind(chunkCache, chunkFilename, null);
entry.then(resolve, ignoreReject); entry.then(resolve, ignoreReject);
......
...@@ -87,7 +87,7 @@ ...@@ -87,7 +87,7 @@
var chunkFilename = chunks[i], var chunkFilename = chunks[i],
entry = chunkCache.get(chunkFilename); entry = chunkCache.get(chunkFilename);
if (void 0 === entry) { if (void 0 === entry) {
entry = __turbopack_load__(chunkFilename); entry = __turbopack_load_by_url__(chunkFilename);
promises.push(entry); promises.push(entry);
var resolve = chunkCache.set.bind(chunkCache, chunkFilename, null); var resolve = chunkCache.set.bind(chunkCache, chunkFilename, null);
entry.then(resolve, ignoreReject); entry.then(resolve, ignoreReject);
......
...@@ -73,7 +73,7 @@ function preloadModule(metadata) { ...@@ -73,7 +73,7 @@ function preloadModule(metadata) {
var chunkFilename = chunks[i], var chunkFilename = chunks[i],
entry = chunkCache.get(chunkFilename); entry = chunkCache.get(chunkFilename);
if (void 0 === entry) { if (void 0 === entry) {
entry = __turbopack_load__(chunkFilename); entry = __turbopack_load_by_url__(chunkFilename);
promises.push(entry); promises.push(entry);
var resolve = chunkCache.set.bind(chunkCache, chunkFilename, null); var resolve = chunkCache.set.bind(chunkCache, chunkFilename, null);
entry.then(resolve, ignoreReject); entry.then(resolve, ignoreReject);
......
...@@ -1792,7 +1792,7 @@ export async function copy_vendor_react(task_) { ...@@ -1792,7 +1792,7 @@ export async function copy_vendor_react(task_) {
const source = file.data.toString() const source = file.data.toString()
let newSource = source.replace( let newSource = source.replace(
/__turbopack_load__/g, /__turbopack_load__/g,
'__turbopack_load__' '__turbopack_load_by_url__'
) )
file.data = newSource file.data = newSource
......
...@@ -17873,8 +17873,7 @@ ...@@ -17873,8 +17873,7 @@
"runtimeError": false "runtimeError": false
}, },
"test/production/deployment-id-handling/deployment-id-handling.test.ts": { "test/production/deployment-id-handling/deployment-id-handling.test.ts": {
"passed": [], "passed": [
"failed": [
"deployment-id-handling disabled should not append dpl query to all assets for /", "deployment-id-handling disabled should not append dpl query to all assets for /",
"deployment-id-handling disabled should not append dpl query to all assets for /from-app", "deployment-id-handling disabled should not append dpl query to all assets for /from-app",
"deployment-id-handling disabled should not append dpl query to all assets for /from-app/edge", "deployment-id-handling disabled should not append dpl query to all assets for /from-app/edge",
...@@ -17894,6 +17893,7 @@ ...@@ -17894,6 +17893,7 @@
"deployment-id-handling enabled with NEXT_DEPLOYMENT_ID should append dpl query to all assets correctly for /from-app/edge", "deployment-id-handling enabled with NEXT_DEPLOYMENT_ID should append dpl query to all assets correctly for /from-app/edge",
"deployment-id-handling enabled with NEXT_DEPLOYMENT_ID should append dpl query to all assets correctly for /pages-edge" "deployment-id-handling enabled with NEXT_DEPLOYMENT_ID should append dpl query to all assets correctly for /pages-edge"
], ],
"failed": [],
"pending": [], "pending": [],
"flakey": [], "flakey": [],
"runtimeError": false "runtimeError": false
...@@ -294,7 +294,6 @@ function instantiateRuntimeModule( ...@@ -294,7 +294,6 @@ function instantiateRuntimeModule(
): Module { ): Module {
return instantiateModule(moduleId, { type: SourceType.Runtime, chunkPath }); return instantiateModule(moduleId, { type: SourceType.Runtime, chunkPath });
} }
/** /**
* Returns the URL relative to the origin where a chunk can be fetched from. * Returns the URL relative to the origin where a chunk can be fetched from.
*/ */
......
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