Skip to content

Commit

Permalink
Change edge wrapper to proxy exports
Browse files Browse the repository at this point in the history
  • Loading branch information
mischnic committed Nov 8, 2024
1 parent e21b810 commit c61780f
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 6 deletions.
11 changes: 10 additions & 1 deletion crates/next-core/src/next_edge/entry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,19 @@ pub async fn wrap_edge_entry(
// The wrapped module could be an async module, we handle that with the proxy
// here. The comma expression makes sure we don't call the function with the
// module as the "this" arg.
// Turn exports into functions that are also a thenable. This way you can await exports (e.g.
// for Components) or call them directly as though they are async function (e.g. edge
// functions/middleware, this is what the Edge Runtime does).
let source = formatdoc!(
r#"
self._ENTRIES ||= {{}}
self._ENTRIES[{}] = import('MODULE')
self._ENTRIES[{}] = new Proxy(import('MODULE'), {{
get(modProm, name) {{
let result = (...args) => modProm.then((mod) => (0, mod[name])(...args));
result.then = (v) => v(modProm.then((mod) => (0, mod[name])));
return result;
}}
}})
"#,
StringifyJs(&format_args!("middleware_{}", pathname))
);
Expand Down
4 changes: 2 additions & 2 deletions packages/next/src/build/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1563,9 +1563,9 @@ export async function isPageStatic({
useCache: true,
distDir,
})
const mod = (
const mod =
await runtime.context._ENTRIES[`middleware_${edgeInfo.name}`]
).ComponentMod
.ComponentMod

// This is not needed during require.
const buildManifest = {} as BuildManifest
Expand Down
5 changes: 2 additions & 3 deletions packages/next/src/server/web/sandbox/sandbox.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,9 +115,8 @@ export const run = withTaggedErrors(async function runWithTaggedErrors(params) {

const edgeFunction: (args: {
request: RequestData
}) => Promise<FetchEventResult> = (
await runtime.context._ENTRIES[`middleware_${params.name}`]
).default
}) => Promise<FetchEventResult> =
await runtime.context._ENTRIES[`middleware_${params.name}`].default

const cloned = !['HEAD', 'GET'].includes(params.request.method)
? params.request.body?.cloneBodyStream()
Expand Down

0 comments on commit c61780f

Please sign in to comment.