Skip to content

Commit

Permalink
Fixup server actions
Browse files Browse the repository at this point in the history
  • Loading branch information
mischnic committed Nov 25, 2024
1 parent 9e6cae8 commit 09b2ecf
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 39 deletions.
44 changes: 21 additions & 23 deletions crates/next-api/src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1136,29 +1136,27 @@ impl AppEndpoint {
(None, None, None, None)
};

let server_action_manifest_loader =
if let Some(app_server_reference_modules) = app_server_reference_modules {
let server_action_manifest = create_server_actions_manifest(
*ResolvedVc::upcast(app_entry.rsc_entry),
app_server_reference_modules,
this.app_project.project().project_path(),
node_root,
app_entry.original_name.clone(),
runtime,
match runtime {
NextRuntime::Edge => Vc::upcast(this.app_project.edge_rsc_module_context()),
NextRuntime::NodeJs => Vc::upcast(this.app_project.rsc_module_context()),
},
this.app_project
.project()
.runtime_chunking_context(process_client_assets, runtime),
)
.await?;
server_assets.insert(server_action_manifest.manifest);
Some(server_action_manifest.loader)
} else {
None
};
let server_action_manifest_loader = if app_server_reference_modules.is_some() {
let server_action_manifest = create_server_actions_manifest(
*ResolvedVc::upcast(app_entry.rsc_entry),
this.app_project.project().project_path(),
node_root,
app_entry.original_name.clone(),
runtime,
match runtime {
NextRuntime::Edge => Vc::upcast(this.app_project.edge_rsc_module_context()),
NextRuntime::NodeJs => Vc::upcast(this.app_project.rsc_module_context()),
},
this.app_project
.project()
.runtime_chunking_context(process_client_assets, runtime),
)
.await?;
server_assets.insert(server_action_manifest.manifest);
Some(server_action_manifest.loader)
} else {
None
};

let (app_entry_chunks, app_entry_chunks_availability) = &*self
.app_entry_chunks(
Expand Down
43 changes: 27 additions & 16 deletions crates/next-api/src/server_actions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use std::{collections::BTreeMap, future::Future, io::Write, iter::once};
use anyhow::{bail, Context, Result};
use indexmap::map::Entry;
use next_core::{
next_client_reference::EcmascriptClientReferenceModule,
next_manifests::{
ActionLayer, ActionManifestModuleId, ActionManifestWorkerEntry, ServerReferenceManifest,
},
Expand Down Expand Up @@ -31,7 +32,7 @@ use turbopack_core::{
chunk::{ChunkItem, ChunkItemExt, ChunkableModule, ChunkingContext, EvaluatableAsset},
context::AssetContext,
file_source::FileSource,
module::{Module, Modules},
module::Module,
output::OutputAsset,
reference::primary_referenced_modules,
reference_type::{EcmaScriptModulesReferenceSubType, ReferenceType},
Expand Down Expand Up @@ -60,15 +61,15 @@ pub(crate) struct ServerActionsManifest {
#[turbo_tasks::function]
pub(crate) async fn create_server_actions_manifest(
rsc_entry: Vc<Box<dyn Module>>,
server_reference_modules: Vc<Modules>,
project_path: Vc<FileSystemPath>,
node_root: Vc<FileSystemPath>,
page_name: RcStr,
runtime: NextRuntime,
asset_context: Vc<Box<dyn AssetContext>>,
chunking_context: Vc<Box<dyn ChunkingContext>>,
) -> Result<Vc<ServerActionsManifest>> {
let actions = find_actions(rsc_entry, server_reference_modules, asset_context);
let actions = find_actions(rsc_entry, asset_context);

let loader =
build_server_actions_loader(project_path, page_name.clone(), actions, asset_context);
let evaluable = Vc::try_resolve_sidecast::<Box<dyn EvaluatableAsset>>(loader)
Expand Down Expand Up @@ -190,28 +191,20 @@ async fn build_manifest(
#[turbo_tasks::function]
async fn find_actions(
rsc_entry: ResolvedVc<Box<dyn Module>>,
server_reference_modules: Vc<Modules>,
asset_context: Vc<Box<dyn AssetContext>>,
) -> Result<Vc<AllActions>> {
async move {
let actions = NonDeterministic::new()
.skip_duplicates()
.visit(
// First visit the actions (for ActionLayer::ActionBrowser), then later the rsc
// entry (which may revisit the client references but with ActionLayer::Rsc).
// This will be refactored later anyway.
once((
ActionLayer::Rsc,
rsc_entry,
rsc_entry.ident().to_string().await?,
))
.chain(
server_reference_modules
.await?
.iter()
.map(|m| async move {
Ok((ActionLayer::ActionBrowser, *m, m.ident().to_string().await?))
})
.try_join()
.await?,
),
)),
FindActionsVisit {},
)
.await
Expand All @@ -227,6 +220,11 @@ async fn find_actions(
// in both layers) and preferring the RSC layer's action.
let mut all_actions: HashToLayerNameModule = FxIndexMap::default();
for ((layer, module, _), actions_map) in actions.iter() {
println!(
"all actions {:?} {}",
layer,
module.ident().to_string().await?
);
let module = if *layer == ActionLayer::Rsc {
*module
} else {
Expand Down Expand Up @@ -284,11 +282,24 @@ impl turbo_tasks::graph::Visit<FindActionsNode> for FindActionsVisit {
async fn get_referenced_modules(
(layer, module, _): FindActionsNode,
) -> Result<impl Iterator<Item = FindActionsNode> + Send> {
if let Some(module) =
ResolvedVc::try_downcast_type::<EcmascriptClientReferenceModule>(module).await?
{
let module: ReadRef<EcmascriptClientReferenceModule> = module.await?;
return Ok(vec![(
ActionLayer::ActionBrowser,
ResolvedVc::upcast(module.client_module),
module.client_module.ident().to_string().await?,
)]
.into_iter());
}

let modules = primary_referenced_modules(*module).await?;

Ok(modules
.into_iter()
.map(move |&m| async move { Ok((layer, m, m.ident().to_string().await?)) })
.copied()
.map(move |m| async move { Ok((layer, m, m.ident().to_string().await?)) })
.try_join()
.await?
.into_iter())
Expand Down

0 comments on commit 09b2ecf

Please sign in to comment.