Skip to content

Commit

Permalink
fix Next.js calling a non-bound waitUntil method (#917)
Browse files Browse the repository at this point in the history
  • Loading branch information
dario-piotrowicz authored Dec 8, 2024
1 parent edd4fea commit e1d65f4
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 function
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]=$2?{kind:"external",function:globalThis[Symbol.for('__cloudflare-request-context__')].ctx.waitUntil.bind(globalThis[Symbol.for('__cloudflare-request-context__')].ctx)}:{kind:"internal",promises:`,
);

return contents;
}

Expand Down

0 comments on commit e1d65f4

Please sign in to comment.