Skip to content

Commit

Permalink
Turbopack: Implement Server Actions (#53890)
Browse files Browse the repository at this point in the history
### What?

This implements Server Actions inside the new Turbopack next-api bundles.

### How?

Server Actions requires:
1. A `.next/server/server-reference-manifest.json` manifest describing what loader module to import to invoke a server action
2. A "loader" entry point that then imports server actions from our internal chunk items
3. Importing the bundled `react-experimental` module instead of regular `react`
4. A little 🪄 pixie dust
5. A small change in the magic comment generated in modules that export server actions

I had to change the magic `__next_internal_action_entry_do_not_use__` comment generated by the server actions transformer. When I traverse the module graph to find all exported actions _after chunking_ has been performed, we no longer have access to the original file name needed to generate the server action's id hash. Adding the filename to comment allows me to recover this without overcomplicating our output pipeline.

Closes WEB-1279
Depends on vercel/turborepo#5705

Co-authored-by: Tim Neutkens <6324199+timneutkens@users.noreply.github.com>
Co-authored-by: Jiachi Liu <4800338+huozhi@users.noreply.github.com>
  • Loading branch information
3 people authored Oct 4, 2023
1 parent ad42b61 commit feca3ce
Show file tree
Hide file tree
Showing 33 changed files with 805 additions and 180 deletions.
3 changes: 3 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ opt-level = 3
next-api = { path = "packages/next-swc/crates/next-api", default-features = false }
next-build = { path = "packages/next-swc/crates/next-build", default-features = false }
next-core = { path = "packages/next-swc/crates/next-core", default-features = false }
next-swc = { path = "packages/next-swc/crates/core" }
next-transform-font = { path = "packages/next-swc/crates/next-transform-font" }
next-transform-dynamic = { path = "packages/next-swc/crates/next-transform-dynamic" }
next-transform-strip-page-exports = { path = "packages/next-swc/crates/next-transform-strip-page-exports" }
Expand Down
81 changes: 56 additions & 25 deletions packages/next-swc/crates/core/src/server_actions.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
use std::convert::{TryFrom, TryInto};
use std::{
collections::HashMap,
convert::{TryFrom, TryInto},
};

use hex::encode as hex_encode;
use serde::Deserialize;
Expand All @@ -25,6 +28,9 @@ pub struct Config {
pub enabled: bool,
}

/// A mapping of hashed action id to the action's exported function name.
pub type ActionsMap = HashMap<String, String>;

pub fn server_actions<C: Comments>(
file_name: &FileName,
config: Config,
Expand All @@ -33,7 +39,7 @@ pub fn server_actions<C: Comments>(
as_folder(ServerActions {
config,
comments,
file_name: file_name.clone(),
file_name: file_name.to_string(),
start_pos: BytePos(0),
in_action_file: false,
in_export_decl: false,
Expand All @@ -55,10 +61,40 @@ pub fn server_actions<C: Comments>(
})
}

/// Parses the Server Actions comment for all exported action function names.
///
/// Action names are stored in a leading BlockComment prefixed by
/// `__next_internal_action_entry_do_not_use__`.
pub fn parse_server_actions<C: Comments>(program: &Program, comments: C) -> Option<ActionsMap> {
let byte_pos = match program {
Program::Module(m) => m.span.lo,
Program::Script(s) => s.span.lo,
};
comments.get_leading(byte_pos).and_then(|comments| {
comments.iter().find_map(|c| {
c.text
.split_once("__next_internal_action_entry_do_not_use__")
.and_then(|(_, actions)| match serde_json::from_str(actions) {
Ok(v) => Some(v),
Err(_) => None,
})
})
})
}

/// Serializes the Server Actions into a magic comment prefixed by
/// `__next_internal_action_entry_do_not_use__`.
fn generate_server_actions_comment(actions: ActionsMap) -> String {
format!(
" __next_internal_action_entry_do_not_use__ {} ",
serde_json::to_string(&actions).unwrap()
)
}

struct ServerActions<C: Comments> {
#[allow(unused)]
config: Config,
file_name: FileName,
file_name: String,
comments: C,

start_pos: BytePos,
Expand Down Expand Up @@ -216,7 +252,7 @@ impl<C: Comments> ServerActions<C> {
.cloned()
.map(|id| Some(id.as_arg()))
.collect(),
self.file_name.to_string(),
&self.file_name,
export_name.to_string(),
Some(action_ident.clone()),
);
Expand Down Expand Up @@ -317,7 +353,7 @@ impl<C: Comments> ServerActions<C> {
.cloned()
.map(|id| Some(id.as_arg()))
.collect(),
self.file_name.to_string(),
&self.file_name,
export_name.to_string(),
Some(action_ident.clone()),
);
Expand Down Expand Up @@ -923,8 +959,7 @@ impl<C: Comments> VisitMut for ServerActions<C> {
let ident = Ident::new(id.0.clone(), DUMMY_SP.with_ctxt(id.1));

if !self.config.is_server {
let action_id =
generate_action_id(self.file_name.to_string(), export_name.to_string());
let action_id = generate_action_id(&self.file_name, export_name);

if export_name == "default" {
let export_expr = ModuleItem::ModuleDecl(ModuleDecl::ExportDefaultExpr(
Expand Down Expand Up @@ -973,7 +1008,7 @@ impl<C: Comments> VisitMut for ServerActions<C> {
&mut self.annotations,
ident.clone(),
Vec::new(),
self.file_name.to_string(),
&self.file_name,
export_name.to_string(),
None,
);
Expand Down Expand Up @@ -1037,26 +1072,22 @@ impl<C: Comments> VisitMut for ServerActions<C> {
}

if self.has_action {
let actions = if self.in_action_file {
self.exported_idents.iter().map(|e| e.1.clone()).collect()
} else {
self.export_actions.clone()
};
let actions = actions
.into_iter()
.map(|name| (generate_action_id(&self.file_name, &name), name))
.collect::<ActionsMap>();
// Prepend a special comment to the top of the file.
self.comments.add_leading(
self.start_pos,
Comment {
span: DUMMY_SP,
kind: CommentKind::Block,
// Append a list of exported actions.
text: format!(
" __next_internal_action_entry_do_not_use__ {} ",
if self.in_action_file {
self.exported_idents
.iter()
.map(|e| e.1.to_string())
.collect::<Vec<_>>()
.join(",")
} else {
self.export_actions.join(",")
}
)
.into(),
text: generate_server_actions_comment(actions).into(),
},
);

Expand Down Expand Up @@ -1162,7 +1193,7 @@ fn collect_pat_idents(pat: &Pat, closure_idents: &mut Vec<Id>) {
}
}

fn generate_action_id(file_name: String, export_name: String) -> String {
fn generate_action_id(file_name: &str, export_name: &str) -> String {
// Attach a checksum to the action using sha1:
// $$id = sha1('file_name' + ':' + 'export_name');
let mut hasher = Sha1::new();
Expand All @@ -1178,7 +1209,7 @@ fn annotate_ident_as_action(
annotations: &mut Vec<Stmt>,
ident: Ident,
bound: Vec<Option<ExprOrSpread>>,
file_name: String,
file_name: &str,
export_name: String,
maybe_orig_action_ident: Option<Ident>,
) {
Expand All @@ -1188,7 +1219,7 @@ fn annotate_ident_as_action(
// $$id
ExprOrSpread {
spread: None,
expr: Box::new(generate_action_id(file_name, export_name).into()),
expr: Box::new(generate_action_id(file_name, &export_name).into()),
},
// myAction.$$bound = [arg1, arg2, arg3];
// or myAction.$$bound = null; if there are no bound values.
Expand Down
22 changes: 11 additions & 11 deletions packages/next-swc/crates/napi/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,13 @@ default = ["rustls-tls"]
# when build (i.e napi --build --features plugin), same for the wasm as well.
# this is due to some of transitive dependencies have features cannot be enabled at the same time
# (i.e wasmer/default vs wasmer/js-default) while cargo merges all the features at once.
plugin = ["turbopack-binding/__swc_core_binding_napi_plugin", "turbopack-binding/__swc_core_binding_napi_plugin_filesystem_cache", "turbopack-binding/__swc_core_binding_napi_plugin_shared_runtime", "next-swc/plugin", "next-core/plugin"]
plugin = [
"turbopack-binding/__swc_core_binding_napi_plugin",
"turbopack-binding/__swc_core_binding_napi_plugin_filesystem_cache",
"turbopack-binding/__swc_core_binding_napi_plugin_shared_runtime",
"next-swc/plugin",
"next-core/plugin",
]
sentry_native_tls = ["sentry", "sentry/native-tls", "native-tls"]
sentry_rustls = ["sentry", "sentry/rustls", "rustls-tls"]

Expand All @@ -24,9 +30,7 @@ image-avif = ["next-core/image-avif"]
# Enable all the available image codec support.
# Currently this is identical to `image-webp`, as we are not able to build
# other codecs easily yet.
image-extended = [
"image-webp",
]
image-extended = ["image-webp"]

# Enable dhat profiling allocator for heap profiling.
__internal_dhat-heap = ["dhat"]
Expand All @@ -47,7 +51,7 @@ napi = { version = "2", default-features = false, features = [
"error_anyhow",
] }
napi-derive = "2"
next-swc = { version = "0.0.0", path = "../core" }
next-swc = { workspace = true }
next-api = { workspace = true }
next-build = { workspace = true }
next-core = { workspace = true }
Expand All @@ -73,9 +77,7 @@ turbopack-binding = { workspace = true, features = [
] }

[target.'cfg(not(all(target_os = "linux", target_env = "musl", target_arch = "aarch64")))'.dependencies]
turbopack-binding = { workspace = true, features = [
"__turbo_tasks_malloc"
] }
turbopack-binding = { workspace = true, features = ["__turbo_tasks_malloc"] }

# There are few build targets we can't use native-tls which default features rely on,
# allow to specify alternative (rustls) instead via features.
Expand All @@ -94,6 +96,4 @@ serde = "1"
serde_json = "1"
# It is not a mistake this dependency is specified in dep / build-dep both.
shadow-rs = { workspace = true }
turbopack-binding = { workspace = true, features = [
"__turbo_tasks_build"
]}
turbopack-binding = { workspace = true, features = ["__turbo_tasks_build"] }
13 changes: 8 additions & 5 deletions packages/next-swc/crates/next-api/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,17 @@ bench = false

[features]
default = ["custom_allocator"]
custom_allocator = ["turbopack-binding/__turbo_tasks_malloc", "turbopack-binding/__turbo_tasks_malloc_custom_allocator"]
custom_allocator = [
"turbopack-binding/__turbo_tasks_malloc",
"turbopack-binding/__turbo_tasks_malloc_custom_allocator",
]

[dependencies]
anyhow = { workspace = true, features = ["backtrace"] }
futures = { workspace = true }
next-swc = { workspace = true }
indexmap = { workspace = true }
indoc = { workspace = true }
next-core = { workspace = true }
once_cell = { workspace = true }
serde = { workspace = true }
Expand All @@ -35,14 +40,12 @@ turbopack-binding = { workspace = true, features = [
"__turbopack_cli_utils",
"__turbopack_node",
"__turbopack_dev_server",
]}
] }
turbo-tasks = { workspace = true }
tracing = { workspace = true }
tracing-subscriber = { workspace = true, features = ["env-filter", "json"] }

[build-dependencies]
# It is not a mistake this dependency is specified in dep / build-dep both.
shadow-rs = { workspace = true }
turbopack-binding = { workspace = true, features = [
"__turbo_tasks_build"
]}
turbopack-binding = { workspace = true, features = ["__turbo_tasks_build"] }
51 changes: 48 additions & 3 deletions packages/next-swc/crates/next-api/src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ use turbopack_binding::{
use crate::{
project::Project,
route::{Endpoint, Route, Routes, WrittenEndpoint},
server_actions::create_server_actions_manifest,
server_paths::all_server_paths,
};

Expand Down Expand Up @@ -685,6 +686,26 @@ impl AppEndpoint {
bail!("Entry module must be evaluatable");
};
evaluatable_assets.push(evaluatable);

let (loader, manifest) = create_server_actions_manifest(
app_entry.rsc_entry,
node_root,
&app_entry.pathname,
&app_entry.original_name,
NextRuntime::Edge,
Vc::upcast(this.app_project.edge_rsc_module_context()),
Vc::upcast(chunking_context),
this.app_project
.project()
.next_config()
.enable_server_actions(),
)
.await?;
server_assets.push(manifest);
if let Some(loader) = loader {
evaluatable_assets.push(loader);
}

let files = chunking_context.evaluated_chunk_group(
app_entry
.rsc_entry
Expand Down Expand Up @@ -783,6 +804,28 @@ impl AppEndpoint {
}
}
NextRuntime::NodeJs => {
let mut evaluatable_assets =
this.app_project.rsc_runtime_entries().await?.clone_value();

let (loader, manifest) = create_server_actions_manifest(
app_entry.rsc_entry,
node_root,
&app_entry.pathname,
&app_entry.original_name,
NextRuntime::NodeJs,
Vc::upcast(this.app_project.rsc_module_context()),
Vc::upcast(this.app_project.project().rsc_chunking_context()),
this.app_project
.project()
.next_config()
.enable_server_actions(),
)
.await?;
server_assets.push(manifest);
if let Some(loader) = loader {
evaluatable_assets.push(loader);
}

let rsc_chunk = this
.app_project
.project()
Expand All @@ -793,7 +836,7 @@ impl AppEndpoint {
original_name = app_entry.original_name
)),
app_entry.rsc_entry,
this.app_project.rsc_runtime_entries(),
Vc::cell(evaluatable_assets),
);
server_assets.push(rsc_chunk);

Expand All @@ -816,8 +859,10 @@ impl AppEndpoint {
client_assets: Vc::cell(client_assets),
}
}
};
Ok(endpoint_output.cell())
}
.cell();

Ok(endpoint_output)
}
}

Expand Down
1 change: 1 addition & 0 deletions packages/next-swc/crates/next-api/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ mod middleware;
mod pages;
pub mod project;
pub mod route;
mod server_actions;
pub mod server_paths;
mod versioned_content_map;

Expand Down
1 change: 1 addition & 0 deletions packages/next-swc/crates/next-api/src/project.rs
Original file line number Diff line number Diff line change
Expand Up @@ -476,6 +476,7 @@ impl Project {
self.env(),
self.server_addr(),
this.define_env.nodejs(),
self.next_config(),
))
}

Expand Down
Loading

0 comments on commit feca3ce

Please sign in to comment.