Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix Next.js calling a non-bound waitUntil method #917

Merged
merged 3 commits into from
Dec 8, 2024

Conversation

dario-piotrowicz
Copy link
Member

resolves #912

Copy link

changeset-bot bot commented Dec 7, 2024

🦋 Changeset detected

Latest commit: a2d4de8

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 2 packages
Name Type
@cloudflare/next-on-pages Patch
eslint-plugin-next-on-pages Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

Copy link
Contributor

github-actions bot commented Dec 7, 2024

🧪 Prereleases are available for testing 🧪

@cloudflare/next-on-pages

You can install this latest build in your project with:

npm install --save-dev https://prerelease-registry.devprod.cloudflare.dev/next-on-pages/runs/12225063689/npm-package-next-on-pages-917

@cloudflare/eslint-plugin-next-on-pages

You can install this latest build in your project with:

npm install --save-dev https://prerelease-registry.devprod.cloudflare.dev/next-on-pages/runs/12225063689/npm-package-eslint-plugin-next-on-pages-917

Comment on lines 638 to 639
/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]}`,
Copy link
Contributor

@james-elicx james-elicx Dec 8, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Where does e come from? Should this value be from the capture group?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh wow! thanks a lot for spotting this! yeah it should be $2!

I changed this regex a couple of times so I must have messed that up here! 😓 good eye! 🫶

// 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,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we end the regex we're replacing at the end of the promises string instead of trying to capture everything in that array as well maybe? just to be a bit less brittle

Copy link
Member Author

@dario-piotrowicz dario-piotrowicz Dec 8, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes... it could be either way, arguably capturing the array and closing bracket gives us more confidence that we're matching the right thing?

Anyways it's a good callout 👍 I don't mind either version so if you prefer not to match promises: that's fine by me 🙂

@dario-piotrowicz dario-piotrowicz merged commit e1d65f4 into main Dec 8, 2024
9 of 10 checks passed
@dario-piotrowicz dario-piotrowicz deleted the fix-waitUntil-bind branch December 8, 2024 20:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
2 participants