Unverified Commit df0fb70c authored by Justin Ridgewell's avatar Justin Ridgewell Committed by GitHub

turbopack: Support Actions in both RSC and Client layers (#57475)

### What?

Adds support for Server Actions imported by both server and client.

### Why?

If an Action is imported by both the Client and RSC layers, we need to
support them as if they're the same action.

### How?

First, we need to ensure both layers create the same action hash ids,
which we can then use to deduplicate actions imported by both layers. If
a conflict is found, we prefer the RSC layer's action.

Closes WEB-1879

---------
Co-authored-by: 's avatarTobias Koppers <tobias.koppers@googlemail.com>
Co-authored-by: 's avatarkodiakhq[bot] <49736102+kodiakhq[bot]@users.noreply.github.com>
parent 618d6744
use std::{io::Write, iter::once}; use std::{io::Write, iter::once};
use anyhow::{bail, Result}; use anyhow::{bail, Result};
use indexmap::IndexMap; use indexmap::{map::Entry, IndexMap};
use indoc::writedoc;
use next_core::{ use next_core::{
next_manifests::{ActionLayer, ActionManifestWorkerEntry, ServerReferenceManifest}, next_manifests::{ActionLayer, ActionManifestWorkerEntry, ServerReferenceManifest},
util::{get_asset_prefix_from_pathname, NextRuntime}, util::{get_asset_prefix_from_pathname, NextRuntime},
...@@ -10,7 +9,7 @@ use next_core::{ ...@@ -10,7 +9,7 @@ use next_core::{
use next_swc::server_actions::parse_server_actions; use next_swc::server_actions::parse_server_actions;
use turbo_tasks::{ use turbo_tasks::{
graph::{GraphTraversal, NonDeterministic}, graph::{GraphTraversal, NonDeterministic},
TryFlatJoinIterExt, TryJoinIterExt, Value, ValueToString, Vc, TryFlatJoinIterExt, Value, ValueToString, Vc,
}; };
use turbopack_binding::{ use turbopack_binding::{
turbo::tasks_fs::{rope::RopeBuilder, File, FileSystemPath}, turbo::tasks_fs::{rope::RopeBuilder, File, FileSystemPath},
...@@ -78,33 +77,33 @@ pub(crate) async fn create_server_actions_manifest( ...@@ -78,33 +77,33 @@ pub(crate) async fn create_server_actions_manifest(
async fn build_server_actions_loader( async fn build_server_actions_loader(
node_root: Vc<FileSystemPath>, node_root: Vc<FileSystemPath>,
page_name: &str, page_name: &str,
actions: Vc<ModuleActionMap>, actions: Vc<AllActions>,
asset_context: Vc<Box<dyn AssetContext>>, asset_context: Vc<Box<dyn AssetContext>>,
) -> Result<Vc<Box<dyn EcmascriptChunkPlaceable>>> { ) -> Result<Vc<Box<dyn EcmascriptChunkPlaceable>>> {
let actions = actions.await?; let actions = actions.await?;
let mut contents = RopeBuilder::from("__turbopack_export_value__({\n");
let mut import_map = IndexMap::with_capacity(actions.len());
// Every module which exports an action (that is accessible starting from our // Every module which exports an action (that is accessible starting from our
// app page entry point) will be present. We generate a single loader file // app page entry point) will be present. We generate a single loader file
// which lazily imports the respective module's chunk_item id and invokes // which lazily imports the respective module's chunk_item id and invokes
// the exported action function. // the exported action function.
for (i, (module, actions_map)) in actions.iter().enumerate() { let mut contents = RopeBuilder::from("__turbopack_export_value__({\n");
let module_name = format!("ACTIONS_MODULE{i}"); let mut import_map = IndexMap::new();
for (hash_id, name) in &*actions_map.await? { for (hash_id, (_layer, name, module)) in actions.iter() {
writedoc!( let index = import_map.len();
contents, let module_name = import_map
" '{hash_id}': (...args) => (0, require('{module_name}')['{name}'])(...args),", .entry(*module)
)?; .or_insert_with(|| format!("ACTIONS_MODULE{index}"));
} write!(
import_map.insert(module_name, module.1); contents,
" '{hash_id}': (...args) => (0, require('{module_name}')['{name}'])(...args),",
)?;
} }
write!(contents, "}});")?; write!(contents, "}});")?;
let output_path = node_root.join(format!("server/app{page_name}/actions.js")); let output_path = node_root.join(format!("server/app{page_name}/actions.js"));
let file = File::from(contents.build()); let file = File::from(contents.build());
let source = VirtualSource::new(output_path, AssetContent::file(file.into())); let source = VirtualSource::new(output_path, AssetContent::file(file.into()));
let import_map = import_map.into_iter().map(|(k, v)| (v, k)).collect();
let module = asset_context.process( let module = asset_context.process(
Vc::upcast(source), Vc::upcast(source),
Value::new(ReferenceType::Internal(Vc::cell(import_map))), Value::new(ReferenceType::Internal(Vc::cell(import_map))),
...@@ -126,7 +125,7 @@ async fn build_manifest( ...@@ -126,7 +125,7 @@ async fn build_manifest(
pathname: &str, pathname: &str,
page_name: &str, page_name: &str,
runtime: NextRuntime, runtime: NextRuntime,
actions: Vc<ModuleActionMap>, actions: Vc<AllActions>,
loader_id: Vc<String>, loader_id: Vc<String>,
) -> Result<Vc<Box<dyn OutputAsset>>> { ) -> Result<Vc<Box<dyn OutputAsset>>> {
let manifest_path_prefix = get_asset_prefix_from_pathname(pathname); let manifest_path_prefix = get_asset_prefix_from_pathname(pathname);
...@@ -144,16 +143,13 @@ async fn build_manifest( ...@@ -144,16 +143,13 @@ async fn build_manifest(
NextRuntime::NodeJs => &mut manifest.node, NextRuntime::NodeJs => &mut manifest.node,
}; };
for ((layer, _), action_map) in actions_value { for (hash_id, (layer, _name, _module)) in actions_value {
let action_map = action_map.await?; let entry = mapping.entry(hash_id.clone()).or_default();
for hash in action_map.keys() { entry.workers.insert(
let entry = mapping.entry(hash.clone()).or_default(); format!("app{page_name}"),
entry.workers.insert( ActionManifestWorkerEntry::String(loader_id_value.clone_value()),
format!("app{page_name}"), );
ActionManifestWorkerEntry::String(loader_id_value.clone_value()), entry.layer.insert(format!("app{page_name}"), *layer);
);
entry.layer.insert(format!("app{page_name}"), *layer);
}
} }
Ok(Vc::upcast(VirtualOutputAsset::new( Ok(Vc::upcast(VirtualOutputAsset::new(
...@@ -175,8 +171,8 @@ async fn get_actions( ...@@ -175,8 +171,8 @@ async fn get_actions(
rsc_entry: Vc<Box<dyn Module>>, rsc_entry: Vc<Box<dyn Module>>,
server_reference_modules: Vc<Vec<Vc<Box<dyn Module>>>>, server_reference_modules: Vc<Vec<Vc<Box<dyn Module>>>>,
asset_context: Vc<Box<dyn AssetContext>>, asset_context: Vc<Box<dyn AssetContext>>,
) -> Result<Vc<ModuleActionMap>> { ) -> Result<Vc<AllActions>> {
let mut all_actions = NonDeterministic::new() let actions = NonDeterministic::new()
.skip_duplicates() .skip_duplicates()
.visit( .visit(
once((ActionLayer::Rsc, rsc_entry)).chain( once((ActionLayer::Rsc, rsc_entry)).chain(
...@@ -193,44 +189,61 @@ async fn get_actions( ...@@ -193,44 +189,61 @@ async fn get_actions(
.into_iter() .into_iter()
.map(parse_actions_filter_map) .map(parse_actions_filter_map)
.try_flat_join() .try_flat_join()
.await? .await?;
.into_iter()
.map(|((layer, module), actions)| async move { // Actions can be imported by both Client and RSC layers, in which case we need
let module = if layer == ActionLayer::Rsc { // to use the RSC layer's module. We do that by merging the hashes (which match
module // in both layers) and preferring the RSC layer's action.
} else { let mut all_actions: HashToLayerNameModule = IndexMap::new();
// The ActionBrowser layer's module is in the Client context, and we need to for ((layer, module), actions_map) in actions.iter() {
// bring it into the RSC context. let module = if *layer == ActionLayer::Rsc {
let source = VirtualSource::new_with_ident( *module
module.ident().with_modifier(action_modifier()), } else {
module.content(), to_rsc_context(*module, asset_context).await?
); };
let ty = if let Some(module) =
Vc::try_resolve_downcast_type::<EcmascriptModuleAsset>(module).await? for (hash_id, name) in &*actions_map.await? {
{ match all_actions.entry(hash_id.to_owned()) {
if module.await?.ty == EcmascriptModuleAssetType::Ecmascript { Entry::Occupied(e) => {
ReferenceType::EcmaScriptModules( if e.get().0 == ActionLayer::ActionBrowser {
EcmaScriptModulesReferenceSubType::Undefined, *e.into_mut() = (*layer, name.to_string(), module);
)
} else {
ReferenceType::TypeScript(TypeScriptReferenceSubType::Undefined)
} }
} else { }
ReferenceType::TypeScript(TypeScriptReferenceSubType::Undefined) Entry::Vacant(e) => {
}; e.insert((*layer, name.to_string(), module));
asset_context.process(Vc::upcast(source), Value::new(ty)) }
}; }
Ok(((layer, module), actions)) }
}) }
.try_join()
.await?
.into_iter()
.collect::<IndexMap<_, _>>();
all_actions.sort_keys(); all_actions.sort_keys();
Ok(Vc::cell(all_actions)) Ok(Vc::cell(all_actions))
} }
/// The ActionBrowser layer's module is in the Client context, and we need to
/// bring it into the RSC context.
async fn to_rsc_context(
module: Vc<Box<dyn Module>>,
asset_context: Vc<Box<dyn AssetContext>>,
) -> Result<Vc<Box<dyn Module>>> {
let source = VirtualSource::new_with_ident(
module.ident().with_modifier(action_modifier()),
module.content(),
);
let ty = if let Some(module) =
Vc::try_resolve_downcast_type::<EcmascriptModuleAsset>(module).await?
{
if module.await?.ty == EcmascriptModuleAssetType::Ecmascript {
ReferenceType::EcmaScriptModules(EcmaScriptModulesReferenceSubType::Undefined)
} else {
ReferenceType::TypeScript(TypeScriptReferenceSubType::Undefined)
}
} else {
ReferenceType::TypeScript(TypeScriptReferenceSubType::Undefined)
};
Ok(asset_context.process(Vc::upcast(source), Value::new(ty)))
}
/// Our graph traversal visitor, which finds the primary modules directly /// Our graph traversal visitor, which finds the primary modules directly
/// referenced by [parent]. /// referenced by [parent].
async fn get_referenced_modules( async fn get_referenced_modules(
...@@ -280,15 +293,15 @@ async fn parse_actions_filter_map( ...@@ -280,15 +293,15 @@ async fn parse_actions_filter_map(
}) })
} }
type LayerModuleActionMap = IndexMap<(ActionLayer, Vc<Box<dyn Module>>), Vc<ActionMap>>; type HashToLayerNameModule = IndexMap<String, (ActionLayer, String, Vc<Box<dyn Module>>)>;
/// A mapping of every module which exports a Server Action, with the hashed id /// A mapping of every module which exports a Server Action, with the hashed id
/// and exported name of each found action. /// and exported name of each found action.
#[turbo_tasks::value(transparent)] #[turbo_tasks::value(transparent)]
struct ModuleActionMap(LayerModuleActionMap); struct AllActions(HashToLayerNameModule);
#[turbo_tasks::value_impl] #[turbo_tasks::value_impl]
impl ModuleActionMap { impl AllActions {
#[turbo_tasks::function] #[turbo_tasks::function]
pub fn empty() -> Vc<Self> { pub fn empty() -> Vc<Self> {
Vc::cell(IndexMap::new()) Vc::cell(IndexMap::new())
......
...@@ -40,9 +40,7 @@ struct NextServerActions { ...@@ -40,9 +40,7 @@ struct NextServerActions {
impl CustomTransformer for NextServerActions { impl CustomTransformer for NextServerActions {
async fn transform(&self, program: &mut Program, ctx: &TransformContext<'_>) -> Result<()> { async fn transform(&self, program: &mut Program, ctx: &TransformContext<'_>) -> Result<()> {
let mut actions = server_actions( let mut actions = server_actions(
// The same action can be imported by both server and client, and we need to give both &FileName::Real(ctx.file_path_str.into()),
// types a distinct Action hash ID.
&FileName::Real(format!("{}_{:?}", ctx.file_path_str, self.transform).into()),
Config { Config {
is_react_server_layer: matches!(self.transform, ActionsTransform::Server), is_react_server_layer: matches!(self.transform, ActionsTransform::Server),
enabled: true, enabled: true,
......
...@@ -19,7 +19,7 @@ createNextDescribe( ...@@ -19,7 +19,7 @@ createNextDescribe(
'server-only': 'latest', 'server-only': 'latest',
}, },
}, },
({ next, isNextDev, isNextStart, isNextDeploy }) => { ({ next, isNextDev, isNextStart, isNextDeploy, isTurbopack }) => {
it('should handle basic actions correctly', async () => { it('should handle basic actions correctly', async () => {
const browser = await next.browser('/server') const browser = await next.browser('/server')
...@@ -389,7 +389,24 @@ createNextDescribe( ...@@ -389,7 +389,24 @@ createNextDescribe(
const pageBundle = await fs.readFile( const pageBundle = await fs.readFile(
join(next.testDir, '.next', 'server', 'app', 'client', 'page.js') join(next.testDir, '.next', 'server', 'app', 'client', 'page.js')
) )
expect(pageBundle.toString()).toContain('node_modules/nanoid/index.js') if (isTurbopack) {
const chunkPaths = pageBundle
.toString()
.matchAll(/loadChunk\("([^"]*)"\)/g)
// @ts-ignore
const reads = [...chunkPaths].map(async (match) => {
const bundle = await fs.readFile(
join(next.testDir, '.next', ...match[1].split(/[\\/]/g))
)
return bundle.toString().includes('node_modules/nanoid/index.js')
})
expect(await Promise.all(reads)).toContain(true)
} else {
expect(pageBundle.toString()).toContain(
'node_modules/nanoid/index.js'
)
}
}) })
} }
......
...@@ -2206,6 +2206,7 @@ ...@@ -2206,6 +2206,7 @@
"app-dir action handling fetch actions should revalidate when cookies.set is called", "app-dir action handling fetch actions should revalidate when cookies.set is called",
"app-dir action handling fetch actions should revalidate when cookies.set is called in a client action", "app-dir action handling fetch actions should revalidate when cookies.set is called in a client action",
"app-dir action handling fetch actions should store revalidation data in the prefetch cache", "app-dir action handling fetch actions should store revalidation data in the prefetch cache",
"app-dir action handling should bundle external libraries if they are on the action layer",
"app-dir action handling should handle basic actions correctly", "app-dir action handling should handle basic actions correctly",
"app-dir action handling should not block navigation events while a server action is in flight", "app-dir action handling should not block navigation events while a server action is in flight",
"app-dir action handling should only submit action once when resubmitting an action after navigation", "app-dir action handling should only submit action once when resubmitting an action after navigation",
...@@ -2217,16 +2218,14 @@ ...@@ -2217,16 +2218,14 @@
"app-dir action handling should support headers in client imported actions", "app-dir action handling should support headers in client imported actions",
"app-dir action handling should support hoc auth wrappers", "app-dir action handling should support hoc auth wrappers",
"app-dir action handling should support importing actions in client components", "app-dir action handling should support importing actions in client components",
"app-dir action handling should support importing the same action module instance in both server and action layers",
"app-dir action handling should support next/dynamic with ssr: false", "app-dir action handling should support next/dynamic with ssr: false",
"app-dir action handling should support notFound", "app-dir action handling should support notFound",
"app-dir action handling should support notFound (javascript disabled)", "app-dir action handling should support notFound (javascript disabled)",
"app-dir action handling should support setting cookies in route handlers with the correct overrides", "app-dir action handling should support setting cookies in route handlers with the correct overrides",
"app-dir action handling should support uploading files" "app-dir action handling should support uploading files"
], ],
"failed": [ "failed": [],
"app-dir action handling should bundle external libraries if they are on the action layer",
"app-dir action handling should support importing the same action module instance in both server and action layers"
],
"pending": [ "pending": [
"app-dir action handling fetch actions should handle revalidateTag + redirect" "app-dir action handling fetch actions should handle revalidateTag + redirect"
], ],
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