Skip to content

Commit

Permalink
turbopack: Fix Server Actions in Edge runtime (vercel#57462)
Browse files Browse the repository at this point in the history
### What?

Changes Server Actions to use a lazy `require()` statement instead of a
lazy dynamic `import()`, to fix SA in the Edge runtime.

### Why?

The Edge runtime has a restriction that it's not allowed to lazy load
more files. The problem is that dynamic `import()` does exactly that, it
defers importing those files until the call time. `require()` doesn't
have this issue, because the chunks it would load are included instead
of deferred.

### How?

Just needed to modify the actions loader entry point… after hours of
trying to get the action loader to evaluate in the node runtime and then
import the actions in the edge runtime.


Closes WEB-1874
  • Loading branch information
jridgewell authored Oct 26, 2023
1 parent 6688201 commit 7c0e66f
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 12 deletions.
7 changes: 2 additions & 5 deletions packages/next-swc/crates/next-api/src/server_actions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,17 +95,14 @@ async fn build_server_actions_loader(
for (hash_id, name) in &*actions_map.await? {
writedoc!(
contents,
"
\x20 '{hash_id}': (...args) => import('{module_name}')
.then(mod => (0, mod['{name}'])(...args)),\n
",
" '{hash_id}': (...args) => (0, require('{module_name}')['{name}'])(...args),",
)?;
}
import_map.insert(module_name, module.1);
}
write!(contents, "}});")?;

let output_path = node_root.join(format!("server/app{page_name}/actions.ts"));
let output_path = node_root.join(format!("server/app{page_name}/actions.js"));
let file = File::from(contents.build());
let source = VirtualSource::new(output_path, AssetContent::file(file.into()));
let module = asset_context.process(
Expand Down
14 changes: 7 additions & 7 deletions test/turbopack-tests-manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -2192,7 +2192,12 @@
},
"test/e2e/app-dir/actions/app-action.test.ts": {
"passed": [
"app-dir action handling Edge SSR should allow cookie and header async storages",
"app-dir action handling Edge SSR should handle basic actions correctly",
"app-dir action handling Edge SSR should handle redirect to a relative URL in a single pass",
"app-dir action handling Edge SSR should handle regular redirects",
"app-dir action handling Edge SSR should handle unicode search params",
"app-dir action handling Edge SSR should return error response for hoc auth wrappers in edge runtime",
"app-dir action handling HMR should support updating the action",
"app-dir action handling encryption should send encrypted values from the closed over closure",
"app-dir action handling fetch actions should handle a fetch action initiated from a static page",
Expand Down Expand Up @@ -2224,13 +2229,8 @@
"app-dir action handling should support uploading files"
],
"failed": [
"app-dir action handling Edge SSR should allow cookie and header async storages",
"app-dir action handling Edge SSR should handle basic actions correctly",
"app-dir action handling Edge SSR should handle regular redirects",
"app-dir action handling Edge SSR should handle unicode search params",
"app-dir action handling Edge SSR should return error response for hoc auth wrappers in edge runtime",
"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"
"app-dir action handling should support importing the same action module instance in both server and action layers",
"app-dir action handling should bundle external libraries if they are on the action layer"
],
"pending": [
"app-dir action handling fetch actions should handle revalidateTag + redirect"
Expand Down

0 comments on commit 7c0e66f

Please sign in to comment.