Unverified Commit 7c0e66f8 authored by Justin Ridgewell's avatar Justin Ridgewell Committed by GitHub

turbopack: Fix Server Actions in Edge runtime (#57462)

### 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
parent 66882010
......@@ -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(
......
......@@ -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",
......@@ -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"
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