Unverified Commit 9b453930 authored by Janka Uryga's avatar Janka Uryga Committed by GitHub

[dynamicIO] cache tracking for import() (#74152)

This PR introduces some extra tracking which makes us treat
`import(...)` like a call to a cached function. Thanks to this, `await
import(...)` will no longer cause dynamicity errors in `dynamicIO`.
The motivation is the same as allowing `fs.readFileSync` -- if something
is available on the server at prerender time, we don't consider it IO.

Fixes #72589
Closes #75132

### Implementation notes

The tracking is implemented via an SWC transform
(`track_dynamic_imports.ts`) that turns `import(...)` into
`trackDynamicImport(import(...))`.

`trackDynamicImport(promise)` tracks the promise globally, without using
`workUnitStore.cacheSignal`. The prospective render subscribes to
pending modules using `trackPendingModules(cacheSignal)`, which causes
the prospective render to wait for all `import()`s to finish before
proceeding to the final render. The mechanism is analogous to `'use
cache'`, but the "result" of an `import()` is stored *in the module
cache* instead; when we invoke the `import()` again in the actual
prerender, it will resolve at microtask-speed, like we need it to.

The transform is enabled for all modules that run server-side, both RSC
and SSR, because the prerender runs both. This also includes route
handlers, because we also use a `cacheSignal` when prerendering those.
Notably, we also instrument `import()` in `node_modules` to account for
libraries that do lazy initialization.

I've also had to adjust the prospective client render in PPR mode to
wait for `cacheSignal.cacheReady()` - otherwise, it wouldn't wait for
`import()`s to resolve before doing the final client render, which'd
subsequently cause a dynamicity error.
(we didn't need to wait for `cacheSignal` before, because all cached
promises would've already been awaited in the server prerender)

Finally, we no longer need `warmFlightResponse`, because
`trackPendingModules` already makes the `cacheSignal` wait for all
loading chunks to finish, so we don't need to do it ahead of time.
parent 7790f72f
......@@ -28,7 +28,7 @@ pids
coverage
# test output
test/**/out*
test/**/out/*
test/**/next-env.d.ts
.DS_Store
/e2e-tests
......@@ -42,7 +42,7 @@ test/traces
.nvmrc
# examples
examples/**/out
examples/**/out/*
examples/**/.env*.local
pr-stats.md
......
......@@ -965,6 +965,13 @@ async fn insert_next_shared_aliases(
"next/dist/build/webpack/loaders/next-flight-loader/cache-wrapper",
),
);
import_map.insert_exact_alias(
"private-next-rsc-track-dynamic-import",
request_to_import_mapping(
project_path,
"next/dist/build/webpack/loaders/next-flight-loader/track-dynamic-import",
),
);
insert_turbopack_dev_alias(import_map).await?;
insert_package_alias(
......
......@@ -12,8 +12,8 @@ use crate::{
next_shared::transforms::{
get_next_dynamic_transform_rule, get_next_font_transform_rule, get_next_image_rule,
get_next_lint_transform_rule, get_next_modularize_imports_rule,
get_next_pages_transforms_rule, get_server_actions_transform_rule,
next_amp_attributes::get_next_amp_attr_rule,
get_next_pages_transforms_rule, get_next_track_dynamic_imports_transform_rule,
get_server_actions_transform_rule, next_amp_attributes::get_next_amp_attr_rule,
next_cjs_optimizer::get_next_cjs_optimizer_rule,
next_disallow_re_export_all_in_page::get_next_disallow_export_all_in_page_rule,
next_edge_node_api_assert::next_edge_node_api_assert,
......@@ -178,6 +178,10 @@ pub async fn get_next_server_transforms_rules(
ServerContextType::Middleware { .. } | ServerContextType::Instrumentation { .. } => false,
};
if is_app_dir && *next_config.enable_dynamic_io().await? {
rules.push(get_next_track_dynamic_imports_transform_rule(mdx_rs));
}
if !foreign_code {
rules.push(
get_next_dynamic_transform_rule(true, is_server_components, is_app_dir, mode, mdx_rs)
......
......@@ -16,6 +16,7 @@ pub(crate) mod next_pure;
pub(crate) mod next_react_server_components;
pub(crate) mod next_shake_exports;
pub(crate) mod next_strip_page_exports;
pub(crate) mod next_track_dynamic_imports;
pub(crate) mod react_remove_properties;
pub(crate) mod relay;
pub(crate) mod remove_console;
......@@ -30,6 +31,7 @@ pub use next_dynamic::get_next_dynamic_transform_rule;
pub use next_font::get_next_font_transform_rule;
pub use next_lint::get_next_lint_transform_rule;
pub use next_strip_page_exports::get_next_pages_transforms_rule;
pub use next_track_dynamic_imports::get_next_track_dynamic_imports_transform_rule;
pub use server_actions::get_server_actions_transform_rule;
use turbo_tasks::{ReadRef, ResolvedVc, Value};
use turbo_tasks_fs::FileSystemPath;
......
use anyhow::Result;
use async_trait::async_trait;
use next_custom_transforms::transforms::track_dynamic_imports::*;
use swc_core::ecma::ast::Program;
use turbopack::module_options::ModuleRule;
use turbopack_ecmascript::{CustomTransformer, TransformContext};
use super::get_ecma_transform_rule;
pub fn get_next_track_dynamic_imports_transform_rule(mdx_rs: bool) -> ModuleRule {
get_ecma_transform_rule(Box::new(NextTrackDynamicImports {}), mdx_rs, false)
}
#[derive(Debug)]
struct NextTrackDynamicImports {}
#[async_trait]
impl CustomTransformer for NextTrackDynamicImports {
#[tracing::instrument(level = tracing::Level::TRACE, name = "next_track_dynamic_imports", skip_all)]
async fn transform(&self, program: &mut Program, ctx: &TransformContext<'_>) -> Result<()> {
program.mutate(track_dynamic_imports(ctx.unresolved_mark));
Ok(())
}
}
......@@ -121,6 +121,9 @@ pub struct TransformOptions {
#[serde(default)]
pub css_env: Option<swc_core::ecma::preset_env::Config>,
#[serde(default)]
pub track_dynamic_imports: bool,
}
pub fn custom_before_pass<'a, C>(
......@@ -333,6 +336,14 @@ where
)),
None => Either::Right(noop_pass()),
},
match &opts.track_dynamic_imports {
true => Either::Left(
crate::transforms::track_dynamic_imports::track_dynamic_imports(
unresolved_mark,
),
),
false => Either::Right(noop_pass()),
},
match &opts.cjs_require_optimizer {
Some(config) => Either::Left(visit_mut_pass(
crate::transforms::cjs_optimizer::cjs_optimizer(
......
......@@ -18,6 +18,7 @@ pub mod react_server_components;
pub mod server_actions;
pub mod shake_exports;
pub mod strip_page_exports;
pub mod track_dynamic_imports;
pub mod warn_for_edge_runtime;
//[TODO] PACK-1564: need to decide reuse vs. turbopack specific
......
use swc_core::{
common::{source_map::PURE_SP, util::take::Take, Mark, SyntaxContext},
ecma::{
ast::*,
utils::{prepend_stmt, private_ident, quote_ident, quote_str},
visit::{noop_visit_mut_type, visit_mut_pass, VisitMut, VisitMutWith},
},
quote,
};
pub fn track_dynamic_imports(unresolved_mark: Mark) -> impl VisitMut + Pass {
visit_mut_pass(ImportReplacer::new(unresolved_mark))
}
struct ImportReplacer {
unresolved_ctxt: SyntaxContext,
has_dynamic_import: bool,
wrapper_function_local_ident: Ident,
}
impl ImportReplacer {
pub fn new(unresolved_mark: Mark) -> Self {
ImportReplacer {
unresolved_ctxt: SyntaxContext::empty().apply_mark(unresolved_mark),
has_dynamic_import: false,
wrapper_function_local_ident: private_ident!("$$trackDynamicImport__"),
}
}
}
impl VisitMut for ImportReplacer {
noop_visit_mut_type!();
fn visit_mut_program(&mut self, program: &mut Program) {
program.visit_mut_children_with(self);
// if we wrapped a dynamic import while visiting the children, we need to import the wrapper
if self.has_dynamic_import {
let import_args = MakeNamedImportArgs {
original_ident: quote_ident!("trackDynamicImport").into(),
local_ident: self.wrapper_function_local_ident.clone(),
source: "private-next-rsc-track-dynamic-import",
unresolved_ctxt: self.unresolved_ctxt,
};
match program {
Program::Module(module) => {
prepend_stmt(&mut module.body, make_named_import_esm(import_args));
}
Program::Script(script) => {
// CJS modules can still use `import()`. for CJS, we have to inject the helper
// using `require` instead of `import` to avoid accidentally turning them
// into ESM modules.
prepend_stmt(&mut script.body, make_named_import_cjs(import_args));
}
}
}
}
fn visit_mut_expr(&mut self, expr: &mut Expr) {
expr.visit_mut_children_with(self);
// before: `import(...)`
// after: `$$trackDynamicImport__(import(...))`
if let Expr::Call(CallExpr {
callee: Callee::Import(_),
..
}) = expr
{
self.has_dynamic_import = true;
let replacement_expr = quote!(
"$wrapper_fn($expr)" as Expr,
wrapper_fn = self.wrapper_function_local_ident.clone(),
expr: Expr = expr.take()
)
.with_span(PURE_SP);
*expr = replacement_expr
}
}
}
struct MakeNamedImportArgs<'a> {
original_ident: Ident,
local_ident: Ident,
source: &'a str,
unresolved_ctxt: SyntaxContext,
}
fn make_named_import_esm(args: MakeNamedImportArgs) -> ModuleItem {
let MakeNamedImportArgs {
original_ident,
local_ident,
source,
..
} = args;
let mut item = quote!(
"import { $original_ident as $local_ident } from 'dummy'" as ModuleItem,
original_ident = original_ident,
local_ident = local_ident,
);
// the import source cannot be parametrized in `quote!()`, so patch it manually
let decl = item.as_mut_module_decl().unwrap().as_mut_import().unwrap();
decl.src = Box::new(source.into());
item
}
fn make_named_import_cjs(args: MakeNamedImportArgs) -> Stmt {
let MakeNamedImportArgs {
original_ident,
local_ident,
source,
unresolved_ctxt,
} = args;
quote!(
"const { [$original_name]: $local_ident } = $require($source)" as Stmt,
original_name: Expr = quote_str!(original_ident.sym).into(),
local_ident = local_ident,
source: Expr = quote_str!(source).into(),
// the builtin `require` is considered an unresolved identifier.
// we have to match that, or it won't be recognized as
// a proper `require()` call.
require = quote_ident!(unresolved_ctxt, "require")
)
}
......@@ -21,6 +21,7 @@ use next_custom_transforms::transforms::{
server_actions::{self, server_actions, ServerActionsMode},
shake_exports::{shake_exports, Config as ShakeExportsConfig},
strip_page_exports::{next_transform_strip_page_exports, ExportFilter},
track_dynamic_imports::track_dynamic_imports,
warn_for_edge_runtime::warn_for_edge_runtime,
};
use rustc_hash::FxHashSet;
......@@ -930,6 +931,29 @@ fn test_source_maps(input: PathBuf) {
);
}
#[fixture("tests/fixture/track-dynamic-imports/**/input.js")]
fn track_dynamic_imports_fixture(input: PathBuf) {
let output = input.parent().unwrap().join("output.js");
test_fixture(
syntax(),
&|_tr| {
let unresolved_mark = Mark::new();
let top_level_mark = Mark::new();
(
resolver(unresolved_mark, top_level_mark, false),
track_dynamic_imports(unresolved_mark),
)
},
&input,
&output,
FixtureTestConfig {
// auto detect script/module to test CJS handling
module: None,
..Default::default()
},
);
}
fn lint_to_fold<R>(r: R) -> impl Pass
where
R: Visit,
......
export default async function Page() {
const { foo } = await import('some-module')
return foo()
}
import { trackDynamicImport as $$trackDynamicImport__ } from "private-next-rsc-track-dynamic-import";
export default async function Page() {
const { foo } = await /*#__PURE__*/ $$trackDynamicImport__(import('some-module'));
return foo();
}
export default async function Page() {
await import((await import('get-name')).default)
return null
}
import { trackDynamicImport as $$trackDynamicImport__ } from "private-next-rsc-track-dynamic-import";
export default async function Page() {
await /*#__PURE__*/ $$trackDynamicImport__(import((await /*#__PURE__*/ $$trackDynamicImport__(import('get-name'))).default));
return null;
}
export default async function Page() {
const { foo } = await import('some-module')
// name conflict
$$trackDynamicImport__()
return foo()
}
export function $$trackDynamicImport__() {}
import { trackDynamicImport as $$trackDynamicImport__ } from "private-next-rsc-track-dynamic-import";
export default async function Page() {
const { foo } = await /*#__PURE__*/ $$trackDynamicImport__(import('some-module'));
// name conflict
$$trackDynamicImport__1();
return foo();
}
function $$trackDynamicImport__1() {}
export { $$trackDynamicImport__1 as $$trackDynamicImport__ };
const promise = import('some-module')
export default async function Page() {
const { foo } = await promise
return foo()
}
import { trackDynamicImport as $$trackDynamicImport__ } from "private-next-rsc-track-dynamic-import";
const promise = /*#__PURE__*/ $$trackDynamicImport__(import('some-module'));
export default async function Page() {
const { foo } = await promise;
return foo();
}
async function foo() {
const { foo } = await import('some-module')
return foo()
}
exports.foo = foo
const { ["trackDynamicImport"]: $$trackDynamicImport__ } = require("private-next-rsc-track-dynamic-import");
async function foo() {
const { foo } = await /*#__PURE__*/ $$trackDynamicImport__(import('some-module'));
return foo();
}
exports.foo = foo;
/// <reference types="./next" />
/// <reference types="./modules" />
declare module 'some-module' {
export function foo(): null
}
declare module 'get-name' {
const name: string
export default name
}
declare module 'private-next-rsc-track-dynamic-import' {
export function trackDynamicImport<T>(promise: Promise<T>): Promise<T>
}
{
"compilerOptions": {
"noEmit": true,
"rootDir": ".",
"allowJs": true,
"checkJs": true,
"lib": ["ESNext", "DOM"],
"skipLibCheck": true,
"strict": true,
"jsx": "preserve",
"target": "ESNext",
"esModuleInterop": true,
"module": "Preserve",
"moduleResolution": "bundler",
"moduleDetection": "force"
},
"files": ["./index.ts"], // loads ambient declarations for modules used in tests
"include": ["./**/*/input.js", "./**/*/output.js"]
}
......@@ -82,6 +82,7 @@ fn test(input: &Path, minify: bool) {
prefer_esm: false,
debug_function_name: false,
css_env: None,
track_dynamic_imports: false,
};
let unresolved_mark = Mark::new();
......
......@@ -674,5 +674,8 @@
"673": "Thrown value was ignored. This is a bug in Next.js.",
"674": "Route \"%s\" has a \\`generateViewport\\` that depends on Request data (\\`cookies()\\`, etc...) or uncached external data (\\`fetch(...)\\`, etc...) without explicitly allowing fully dynamic rendering. See more info here: https://nextjs.org/docs/messages/next-prerender-dynamic-viewport",
"675": "Expected `generateMetadata` not to block the application shell but it did.",
"676": "Invariant: missing internal router-server-methods this is an internal bug"
"676": "Invariant: missing internal router-server-methods this is an internal bug",
"677": "`trackDynamicImport` should always receive a promise. Something went wrong in the dynamic imports transform.",
"678": "CacheSignal got more endRead() calls than beginRead() calls",
"679": "A CacheSignal cannot subscribe to itself"
}
......@@ -11,6 +11,7 @@ import {
RSC_ACTION_ENCRYPTION_ALIAS,
RSC_CACHE_WRAPPER_ALIAS,
type WebpackLayerName,
RSC_DYNAMIC_IMPORT_WRAPPER_ALIAS,
} from '../lib/constants'
import type { NextConfigComplete } from '../server/config-shared'
import { defaultOverrides } from '../server/require-hook'
......@@ -177,6 +178,8 @@ export function createWebpackAliases({
[RSC_CACHE_WRAPPER_ALIAS]:
'next/dist/build/webpack/loaders/next-flight-loader/cache-wrapper',
[RSC_DYNAMIC_IMPORT_WRAPPER_ALIAS]:
'next/dist/build/webpack/loaders/next-flight-loader/track-dynamic-import',
...(isClient || isEdgeServer
? {
......
......@@ -189,7 +189,7 @@ export function makeExternalHandler({
}
const notExternalModules =
/^(?:private-next-pages\/|next\/(?:dist\/pages\/|(?:app|cache|document|link|form|head|image|legacy\/image|constants|dynamic|script|navigation|headers|router|compat\/router|server)$)|string-hash|private-next-rsc-action-validate|private-next-rsc-action-client-wrapper|private-next-rsc-server-reference|private-next-rsc-cache-wrapper$)/
/^(?:private-next-pages\/|next\/(?:dist\/pages\/|(?:app|cache|document|link|form|head|image|legacy\/image|constants|dynamic|script|navigation|headers|router|compat\/router|server)$)|string-hash|private-next-rsc-action-validate|private-next-rsc-action-client-wrapper|private-next-rsc-server-reference|private-next-rsc-cache-wrapper|private-next-rsc-track-dynamic-import$)/
if (notExternalModules.test(request)) {
return
}
......
......@@ -73,6 +73,7 @@ function getBaseSWCOptions({
isDynamicIo,
cacheHandlers,
useCacheEnabled,
trackDynamicImports,
}: {
filename: string
jest?: boolean
......@@ -93,6 +94,7 @@ function getBaseSWCOptions({
isDynamicIo?: boolean
cacheHandlers?: ExperimentalConfig['cacheHandlers']
useCacheEnabled?: boolean
trackDynamicImports?: boolean
}) {
const isReactServerLayer = isWebpackServerOnlyLayer(bundleLayer)
const isAppRouterPagesLayer = isWebpackAppPagesLayer(bundleLayer)
......@@ -235,6 +237,7 @@ function getBaseSWCOptions({
// On server side of pages router we prefer CJS.
preferEsm: esm,
lintCodemodComments: true,
trackDynamicImports: trackDynamicImports,
debugFunctionName: development,
...(supportedBrowsers && supportedBrowsers.length > 0
......@@ -384,6 +387,7 @@ export function getLoaderSWCOptions({
esm,
cacheHandlers,
useCacheEnabled,
trackDynamicImports,
}: {
filename: string
development: boolean
......@@ -410,6 +414,7 @@ export function getLoaderSWCOptions({
bundleLayer?: WebpackLayerName
cacheHandlers: ExperimentalConfig['cacheHandlers']
useCacheEnabled?: boolean
trackDynamicImports?: boolean
}) {
let baseOptions: any = getBaseSWCOptions({
filename,
......@@ -430,6 +435,7 @@ export function getLoaderSWCOptions({
isDynamicIo,
cacheHandlers,
useCacheEnabled,
trackDynamicImports,
})
baseOptions.fontLoaders = {
fontLoaders: ['next/font/local', 'next/font/google'],
......
export { trackDynamicImport } from '../../../../server/app-render/module-loading/track-dynamic-import'
......@@ -27,7 +27,7 @@ DEALINGS IN THE SOFTWARE.
*/
import type { NextConfig } from '../../../types'
import type { WebpackLayerName } from '../../../lib/constants'
import { type WebpackLayerName, WEBPACK_LAYERS } from '../../../lib/constants'
import { isWasm, transform } from '../../swc'
import { getLoaderSWCOptions } from '../../swc/options'
import path, { isAbsolute } from 'path'
......@@ -76,6 +76,11 @@ export interface SWCLoaderOptions {
// for to force transpiling a `node_module`
const FORCE_TRANSPILE_CONDITIONS =
/next\/font|next\/dynamic|use server|use client|use cache/
// same as above, but including `import(...)`.
// (note the optional whitespace: `import (...)` is also syntactically valid)
const FORCE_TRANSPILE_CONDITIONS_WITH_IMPORT = new RegExp(
String.raw`(?:${FORCE_TRANSPILE_CONDITIONS.source})|import\s*\(`
)
async function loaderTransform(
this: LoaderContext<SWCLoaderOptions> & TelemetryLoaderContext,
......@@ -96,12 +101,18 @@ async function loaderTransform(
loaderOptions.transpilePackages || []
)
const trackDynamicImports = shouldTrackDynamicImports(loaderOptions)
if (shouldMaybeExclude) {
if (!source) {
throw new Error(`Invariant might be excluded but missing source`)
}
if (!FORCE_TRANSPILE_CONDITIONS.test(source)) {
const forceTranspileConditions = trackDynamicImports
? FORCE_TRANSPILE_CONDITIONS_WITH_IMPORT
: FORCE_TRANSPILE_CONDITIONS
if (!forceTranspileConditions.test(source)) {
return [source, inputSourceMap]
}
}
......@@ -150,6 +161,7 @@ async function loaderTransform(
esm,
cacheHandlers: nextConfig.experimental?.cacheHandlers,
useCacheEnabled: nextConfig.experimental?.useCache,
trackDynamicImports,
})
const programmaticOptions = {
......@@ -199,6 +211,18 @@ async function loaderTransform(
)
}
function shouldTrackDynamicImports(loaderOptions: SWCLoaderOptions): boolean {
// we only need to track `import()` 1. in dynamicIO, 2. on the server (RSC and SSR)
// (Note: logic duplicated in crates/next-core/src/next_server/transforms.rs)
const { nextConfig, isServer, bundleLayer } = loaderOptions
return (
!!nextConfig.experimental?.dynamicIO &&
isServer &&
(bundleLayer === WEBPACK_LAYERS.reactServerComponents ||
bundleLayer === WEBPACK_LAYERS.serverSideRendering)
)
}
const EXCLUDED_PATHS =
/[\\/](cache[\\/][^\\/]+\.zip[\\/]node_modules|__virtual__)[\\/]/g
......
......@@ -56,6 +56,8 @@ export const RSC_MOD_REF_PROXY_ALIAS = 'private-next-rsc-mod-ref-proxy'
export const RSC_ACTION_VALIDATE_ALIAS = 'private-next-rsc-action-validate'
export const RSC_ACTION_PROXY_ALIAS = 'private-next-rsc-server-reference'
export const RSC_CACHE_WRAPPER_ALIAS = 'private-next-rsc-cache-wrapper'
export const RSC_DYNAMIC_IMPORT_WRAPPER_ALIAS =
'private-next-rsc-track-dynamic-import'
export const RSC_ACTION_ENCRYPTION_ALIAS = 'private-next-rsc-action-encryption'
export const RSC_ACTION_CLIENT_WRAPPER_ALIAS =
'private-next-rsc-action-client-wrapper'
......
......@@ -7,7 +7,7 @@ export type SchedulerFn<T = void> = (cb: ScheduledFn<T>) => void
*
* @param cb the function to schedule
*/
export const scheduleOnNextTick = <T = void>(cb: ScheduledFn<T>): void => {
export const scheduleOnNextTick = (cb: ScheduledFn<void>) => {
// We use Promise.resolve().then() here so that the operation is scheduled at
// the end of the promise job queue, we then add it to the next process tick
// to ensure it's evaluated afterwards.
......@@ -29,7 +29,7 @@ export const scheduleOnNextTick = <T = void>(cb: ScheduledFn<T>): void => {
*
* @param cb the function to schedule
*/
export const scheduleImmediate = <T = void>(cb: ScheduledFn<T>): void => {
export const scheduleImmediate = (cb: ScheduledFn<void>): void => {
if (process.env.NEXT_RUNTIME === 'edge') {
setTimeout(cb, 0)
} else {
......
......@@ -5,20 +5,16 @@
* and should only be used in codepaths gated with this feature.
*/
import { InvariantError } from '../../shared/lib/invariant-error'
export class CacheSignal {
private count: number
private earlyListeners: Array<() => void>
private listeners: Array<() => void>
private tickPending: boolean
private taskPending: boolean
constructor() {
this.count = 0
this.earlyListeners = []
this.listeners = []
this.tickPending = false
this.taskPending = false
}
private count = 0
private earlyListeners: Array<() => void> = []
private listeners: Array<() => void> = []
private tickPending = false
private taskPending = false
private subscribedSignals: Set<CacheSignal> | null = null
private noMorePendingCaches() {
if (!this.tickPending) {
......@@ -76,9 +72,21 @@ export class CacheSignal {
beginRead() {
this.count++
if (this.subscribedSignals !== null) {
for (const subscriber of this.subscribedSignals) {
subscriber.beginRead()
}
}
}
endRead() {
if (this.count === 0) {
throw new InvariantError(
'CacheSignal got more endRead() calls than beginRead() calls'
)
}
// If this is the last read we need to wait a task before we can claim the cache is settled.
// The cache read will likely ping a Server Component which can read from the cache again and this
// will play out in a microtask so we need to only resolve pending listeners if we're still at 0
......@@ -89,5 +97,46 @@ export class CacheSignal {
if (this.count === 0) {
this.noMorePendingCaches()
}
if (this.subscribedSignals !== null) {
for (const subscriber of this.subscribedSignals) {
subscriber.endRead()
}
}
}
trackRead<T>(promise: Promise<T>) {
this.beginRead()
promise.finally(this.endRead.bind(this))
return promise
}
subscribeToReads(subscriber: CacheSignal): () => void {
if (subscriber === this) {
throw new InvariantError('A CacheSignal cannot subscribe to itself')
}
if (this.subscribedSignals === null) {
this.subscribedSignals = new Set()
}
this.subscribedSignals.add(subscriber)
// we'll notify the subscriber of each endRead() on this signal,
// so we need to give it a corresponding beginRead() for each read we have in flight now.
for (let i = 0; i < this.count; i++) {
subscriber.beginRead()
}
return this.unsubscribeFromReads.bind(this, subscriber)
}
unsubscribeFromReads(subscriber: CacheSignal) {
if (!this.subscribedSignals) {
return
}
this.subscribedSignals.delete(subscriber)
// we don't need to set the set back to `null` if it's empty --
// if other signals are subscribing to this one, it'll likely get more subscriptions later,
// so we'd have to allocate a fresh set again when that happens.
}
}
import { InvariantError } from '../../../shared/lib/invariant-error'
import { isThenable } from '../../../shared/lib/is-thenable'
import { trackPendingImport } from './track-module-loading.external'
/**
* in DynamicIO, `import(...)` will be transformed into `trackDynamicImport(import(...))`.
* A dynamic import is essentially a cached async function, except it's cached by the module system.
*
* The promises are tracked globally regardless of if the `import()` happens inside a render or outside of it.
* When rendering, we can make the `cacheSignal` wait for all pending promises via `trackPendingModules`.
* */
export function trackDynamicImport<TExports extends Record<string, any>>(
modulePromise: Promise<TExports>
): Promise<TExports> {
if (!isThenable(modulePromise)) {
// We're expecting `import()` to always return a promise. If it's not, something's very wrong.
throw new InvariantError(
'`trackDynamicImport` should always receive a promise. Something went wrong in the dynamic imports transform.'
)
}
// Even if we're inside a prerender and have `workUnitStore.cacheSignal`, we always track the promise globally.
// (i.e. via the global `moduleLoadingSignal` that `trackPendingImport` uses internally).
//
// We do this because the `import()` promise might be cached in userspace:
// (which is quite common for e.g. lazy initialization in libraries)
//
// let promise;
// function doDynamicImportOnce() {
// if (!promise) {
// promise = import("...");
// // transformed into:
// // promise = trackDynamicImport(import("..."));
// }
// return promise;
// }
//
// If multiple prerenders (e.g. multiple pages) depend on `doDynamicImportOnce`,
// we have to wait for the import *in all of them*.
// If we only tracked it using `workUnitStore.cacheSignal.trackRead()`,
// then only the first prerender to call `doDynamicImportOnce` would wait --
// Subsequent prerenders would re-use the existing `promise`,
// and `trackDynamicImport` wouldn't be called again in their scope,
// so their respective CacheSignals wouldn't wait for the promise.
trackPendingImport(modulePromise)
return modulePromise
}
// NOTE: this is marked as shared/external because it's stateful
// and the state needs to be shared between app-render (which waits for pending imports)
// and helpers used in transformed page code (which register pending imports)
import {
trackPendingChunkLoad,
trackPendingImport,
trackPendingModules,
} from './track-module-loading.instance' with { 'turbopack-transition': 'next-shared' }
export { trackPendingChunkLoad, trackPendingImport, trackPendingModules }
import { CacheSignal } from '../cache-signal'
import { isThenable } from '../../../shared/lib/is-thenable'
/** Tracks all in-flight async imports and chunk loads. */
const moduleLoadingSignal = new CacheSignal()
export function trackPendingChunkLoad(promise: Promise<unknown>) {
moduleLoadingSignal.trackRead(promise)
}
export function trackPendingImport(exportsOrPromise: unknown) {
// requiring an async module returns a promise.
// if it's sync, there's nothing to track.
if (isThenable(exportsOrPromise)) {
// A client reference proxy might look like a promise, but we can only call `.then()` on it, not e.g. `.finally()`.
// Turn it into a real promise to avoid issues elsewhere.
const promise = Promise.resolve(exportsOrPromise)
moduleLoadingSignal.trackRead(promise)
}
}
/**
* A top-level dynamic import (or chunk load):
*
* 1. delays a prerender (potentially for a task or longer)
* 2. may reveal more caches that need be filled
*
* So if we see one, we want to extend the duration of `cacheSignal` at least until the import/chunk-load is done.
*/
export function trackPendingModules(cacheSignal: CacheSignal): void {
// We can't just use `cacheSignal.trackRead(moduleLoadingSignal.cacheReady())`,
// because we might start and finish multiple batches of module loads while waiting for caches,
// and `moduleLoadingSignal.cacheReady()` would resolve after the first batch.
// Instead, we'll keep notifying `cacheSignal` of each import/chunk-load.
const unsubscribe = moduleLoadingSignal.subscribeToReads(cacheSignal)
// Later, when `cacheSignal` is no longer waiting for any caches (or imports that we've notified it of),
// we can unsubscribe it.
cacheSignal.cacheReady().then(unsubscribe)
}
......@@ -83,6 +83,7 @@ import {
import { RedirectStatusCode } from '../../../client/components/redirect-status-code'
import { INFINITE_CACHE } from '../../../lib/constants'
import { executeRevalidates } from '../../revalidation-utils'
import { trackPendingModules } from '../../app-render/module-loading/track-module-loading.external'
export class WrappedNextRouterError {
constructor(
......@@ -439,6 +440,8 @@ export class AppRouteRouteModule extends RouteModule<
}
)
}
trackPendingModules(cacheSignal)
await cacheSignal.cacheReady()
if (prospectiveRenderIsDynamic) {
......
console.log('[inside-component] async-messages :: imported, sleeping')
await new Promise<void>((resolve) => setTimeout(resolve, 500))
console.log('[inside-component] async-messages :: ready')
export default { title: 'hello' }
'use client'
import * as React from 'react'
import { once } from '../once'
const doImport = once(() => import('./async-messages'))
export function Client() {
const messages = React.use(doImport()).default
return (
<main>
<p>{messages.title}</p>
</main>
)
}
import * as React from 'react'
import { Client } from './client'
export default function Page() {
return <Client />
}
/** Memoize a callback to only invoke it once. */
export function once<T>(cb: () => T) {
let cache: null | { value: T } = null
return () => {
if (!cache) {
cache = { value: cb() }
}
return cache.value
}
}
'use client'
import * as React from 'react'
import { once } from '../once'
const doImport = once(() => import('./messages'))
export function Client() {
const messages = React.use(doImport()).default
return (
<main>
<p>{messages.title}</p>
</main>
)
}
import * as React from 'react'
import { Client } from './client'
export default function Page() {
return <Client />
}
console.log('[inside-component] async-messages :: imported, sleeping')
await new Promise<void>((resolve) => setTimeout(resolve, 500))
console.log('[inside-component] async-messages :: ready')
export default { title: 'hello' }
export async function GET(_request: Request) {
const messages = (await import('./async-messages')).default
return new Response(messages.title)
}
export async function GET(_request: Request) {
const messages = (await import('./messages')).default
return new Response(messages.title)
}
console.log('[inside-component] async-messages :: imported, sleeping')
await new Promise<void>((resolve) => setTimeout(resolve, 500))
console.log('[inside-component] async-messages :: ready')
export default { title: 'hello' }
import * as React from 'react'
export default async function Page() {
const messages = (await import('./async-messages')).default
return (
<main>
<p>{messages.title}</p>
</main>
)
}
import * as React from 'react'
import { getMessages } from 'cjs-pkg-with-async-import'
export default async function Page() {
const messages = await getMessages()
return (
<main>
<p>{messages.title}</p>
</main>
)
}
import * as React from 'react'
import { getMessagesAsync } from 'esm-pkg-with-async-import'
export default async function Page() {
const messages = await getMessagesAsync()
return (
<main>
<p>{messages.title}</p>
</main>
)
}
import * as React from 'react'
import { getMessages } from 'esm-pkg-with-async-import'
export default async function Page() {
const messages = await getMessages()
return (
<main>
<p>{messages.title}</p>
</main>
)
}
import * as React from 'react'
export default async function Page() {
const messages = (await import('./messages')).default
return (
<main>
<p>{messages.title}</p>
</main>
)
}
import * as React from 'react'
export default function Root({ children }: { children: React.ReactNode }) {
return (
<html>
<body>{children}</body>
</html>
)
}
console.log('[top-level] async-messages :: imported, sleeping')
await new Promise<void>((resolve) => setTimeout(resolve, 500))
console.log('[top-level] async-messages :: ready')
export default { title: 'hello' }
'use client'
import * as React from 'react'
const modulePromise = import('./async-messages')
export function Client() {
const messages = React.use(modulePromise).default
return (
<main>
<p>{messages.title}</p>
</main>
)
}
import * as React from 'react'
import { Client } from './client'
export default async function Page() {
return <Client />
}
'use client'
import * as React from 'react'
const modulePromise = import('./messages')
export function Client() {
const messages = React.use(modulePromise).default
return (
<main>
<p>{messages.title}</p>
</main>
)
}
import * as React from 'react'
import { Client } from './client'
export default async function Page() {
return <Client />
}
console.log('[top-level] async-messages :: imported, sleeping')
await new Promise<void>((resolve) => setTimeout(resolve, 500))
console.log('[top-level] async-messages :: ready')
export default { title: 'hello' }
import * as React from 'react'
const modulePromise = import('./async-messages')
export default async function Page() {
const messages = (await modulePromise).default
return (
<main>
<p>{messages.title}</p>
</main>
)
}
import * as React from 'react'
const modulePromise = import('./messages')
export default async function Page() {
const messages = (await modulePromise).default
return (
<main>
<p>{messages.title}</p>
</main>
)
}
/**
* @type {import('next').NextConfig}
*/
const nextConfig = {
experimental: {
ppr: process.env.__NEXT_EXPERIMENTAL_PPR === 'true',
dynamicIO: true,
prerenderEarlyExit: false,
},
}
module.exports = nextConfig
// @ts-check
exports.getMessages = async function getMessages() {
const { default: messages } = await import('./messages.js')
return messages
}
{
"name": "cjs-pkg-with-async-import",
"version": "0.1.0",
"type": "commonjs",
"main": "./index.js"
}
// @ts-check
console.log('[node_modules] async-messages :: imported, sleeping')
await new Promise((resolve) => setTimeout(resolve, 500))
console.log('[node_modules] async-messages :: ready')
export default { title: 'hello' }
// @ts-check
export async function getMessages() {
const { default: messages } = await import('./messages.js')
return messages
}
export async function getMessagesAsync() {
const { default: messages } = await import('./indirect-async.js')
return messages
}
// @ts-check
console.log('[node_modules] indirect-async :: imported, sleeping')
await new Promise((resolve) => setTimeout(resolve, 500))
console.log('[node_modules] indirect-async :: importing messages')
const { default: messages } = await import('./async-messages.js')
console.log('[node_modules] indirect-async :: ready')
export default messages
{
"name": "esm-pkg-with-async-import",
"version": "0.1.0",
"type": "module",
"exports": {
".": "./index.js"
}
}
import * as path from 'path'
import { nextTestSetup } from 'e2e-utils'
import {
assertNoRedbox,
getRouteTypeFromDevToolsIndicator,
retry,
} from 'next-test-utils'
describe('async imports in dynamicIO', () => {
const { next, isNextStart, isNextDev } = nextTestSetup({
files: path.join(__dirname, 'bundled'),
})
if (isNextStart) {
it('does not cause any routes to become (partially) dynamic', async () => {
const prerenderManifest = JSON.parse(
await next.readFile('.next/prerender-manifest.json')
)
let prerenderedRoutes = Object.keys(prerenderManifest.routes).sort()
if (process.env.__NEXT_EXPERIMENTAL_PPR === 'true') {
// For the purpose of this test we don't consider an incomplete shell.
prerenderedRoutes = prerenderedRoutes.filter((route) => {
const filename = route.replace(/^\//, '').replace(/^$/, 'index')
try {
return next
.readFileSync(`.next/server/app/${filename}.html`)
.endsWith('</html>')
} catch (err) {
if ('code' in err && err.code === 'ENOENT') {
// the route was prerendered, but we didn't find a HTML file for it.
// this means it must be a GET route handler, not a page
return true
} else {
throw err
}
}
})
}
expect(prerenderedRoutes).toMatchInlineSnapshot(`
[
"/inside-render/client/async-module",
"/inside-render/client/sync-module",
"/inside-render/route-handler/async-module",
"/inside-render/route-handler/sync-module",
"/inside-render/server/async-module",
"/inside-render/server/from-node-modules/cjs/sync-module",
"/inside-render/server/from-node-modules/esm/async-module",
"/inside-render/server/from-node-modules/esm/sync-module",
"/inside-render/server/sync-module",
"/outside-of-render/client/async-module",
"/outside-of-render/client/sync-module",
"/outside-of-render/server/async-module",
"/outside-of-render/server/sync-module",
]
`)
})
}
const testPage = async (href: string) => {
const browser = await next.browser(href)
expect(await browser.elementByCss('body').text()).toBe('hello')
if (isNextDev) {
await retry(async () => {
// the page should be static
expect(await getRouteTypeFromDevToolsIndicator(browser)).toBe('Static')
// we shouldn't get any errors from `spawnDynamicValidationInDev`
await assertNoRedbox(browser)
})
}
}
describe('inside a server component', () => {
it('import of a sync module', async () => {
await testPage('/inside-render/server/sync-module')
})
it('import of module with top-level-await', async () => {
await testPage('/inside-render/server/async-module')
})
describe('dynamic import in node_modules', () => {
describe('in an ESM package', () => {
it('import of a sync module', async () => {
await testPage(
'/inside-render/server/from-node-modules/esm/sync-module'
)
})
it('import of module with top-level-await', async () => {
await testPage(
'/inside-render/server/from-node-modules/esm/async-module'
)
})
})
describe('in a CJS package', () => {
// CJS can't do top-level-await, so we're only testing sync modules
it('import of a sync module', async () => {
await testPage(
'/inside-render/server/from-node-modules/cjs/sync-module'
)
})
})
})
})
describe('inside a client component', () => {
it('import of a sync module', async () => {
await testPage('/inside-render/client/sync-module')
})
it('import of module with top-level-await', async () => {
await testPage('/inside-render/client/async-module')
})
})
describe('inside a GET route handler', () => {
it('import of a sync module', async () => {
const result = await next
.fetch('/inside-render/route-handler/sync-module')
.then((res) => res.text())
expect(result).toBe('hello')
})
it('import of module with top-level-await', async () => {
const result = await next
.fetch('/inside-render/route-handler/async-module')
.then((res) => res.text())
expect(result).toBe('hello')
})
})
describe('outside of render', () => {
describe('server', () => {
it('import of a sync module', async () => {
await testPage('/outside-of-render/server/sync-module')
})
it('import of module with top-level-await', async () => {
await testPage('/outside-of-render/server/async-module')
})
})
describe('client', () => {
it('import of a sync module', async () => {
await testPage('/outside-of-render/client/sync-module')
})
it('import of module with top-level-await', async () => {
await testPage('/outside-of-render/client/async-module')
})
})
})
})
describe('async imports in dynamicIO - external packages', () => {
const { next, isNextStart, skipped } = nextTestSetup({
files: path.join(__dirname, 'external'),
skipDeployment: true,
skipStart: true,
})
if (skipped) return
// This is currently expected to fail because we can only track `import()` in bundled code,
// and packages marked as external aren't bundled.
it('does not instrument import() in external packages', async () => {
const expectedError = `Error: Route "/": A component accessed data, headers, params, searchParams, or a short-lived cache without a Suspense boundary nor a "use cache" above it.`
if (isNextStart) {
// in prod, we fail during the build
await expect(() => next.start()).rejects.toThrow()
expect(next.cliOutput).toContain(expectedError)
} else {
// in dev, we fail when visiting the page
await next.start()
await next.browser('/')
await retry(() => expect(next.cliOutput).toContain(expectedError))
}
})
})
import * as React from 'react'
export default function Root({ children }: { children: React.ReactNode }) {
return (
<html>
<body>{children}</body>
</html>
)
}
import * as React from 'react'
// NOTE: this is not actually external by default, only in one test where we patch `next.config.js`
import { getMessagesAsync } from 'external-esm-pkg-with-async-import'
export default async function Page() {
const messages = await getMessagesAsync()
return (
<main>
<p>{messages.title}</p>
</main>
)
}
/**
* @type {import('next').NextConfig}
*/
const nextConfig = {
experimental: {
ppr: process.env.__NEXT_EXPERIMENTAL_PPR === 'true',
dynamicIO: true,
},
serverExternalPackages: ['external-esm-pkg-with-async-import'],
}
module.exports = nextConfig
// @ts-check
console.log('[node_modules, external] async-messages :: imported, sleeping')
await new Promise((resolve) => setTimeout(resolve, 500))
console.log('[node_modules, external] async-messages :: ready')
export default { title: 'hello' }
// @ts-check
export async function getMessagesAsync() {
const { default: messages } = await import('./async-messages.js')
return messages
}
{
"name": "external-esm-pkg-with-async-import",
"version": "0.1.0",
"type": "module",
"exports": {
".": "./index.js"
}
}
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