Skip to content

Commit

Permalink
fix Next.js calling a non-bound waitUntil method
Browse files Browse the repository at this point in the history
  • Loading branch information
dario-piotrowicz committed Dec 7, 2024
1 parent 835bec5 commit 2d3afd1
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .changeset/bright-vans-relate.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@cloudflare/next-on-pages': patch
---

fix Next.js calling a non-bound waitUntil method
Original file line number Diff line number Diff line change
Expand Up @@ -631,6 +631,14 @@ function fixFunctionContents(contents: string): string {
'const { originalRequest = $1 } = $1$3',
);

// Next.js calls `waitUntil` without binding is to the context object, triggering the following error: `TypeError: Illegal invocation: function called with incorrect `this` reference`
// We need to update it so that uses a `waitUntil` bound to our `ctx` object
// (source https://github.com/vercel/next.js/blob/b7c271d7/packages/next/src/server/web/spec-extension/fetch-event.ts#L21)
contents = contents.replace(
/this\[(.)\]=(\w)\?{kind:"external",function:\2}:{kind:"internal",promises:\[(.*?)\]}/gm,
`this[$1]=e?{kind:"external",function:globalThis[Symbol.for('__cloudflare-request-context__')].ctx.waitUntil.bind(globalThis[Symbol.for('__cloudflare-request-context__')].ctx)}:{kind:"internal",promises:[$3]}`,
);

return contents;
}

Expand Down

0 comments on commit 2d3afd1

Please sign in to comment.