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: make sure that fetch cache sets are properly awaited #364

Merged
merged 15 commits into from
Feb 12, 2025

Conversation

dario-piotrowicz
Copy link
Contributor

Next.js does not await promises that update the incremental cache for fetch requests,
that is needed in our runtime otherwise the cache updates get lost, so this change
makes sure that the promise is properly awaited via waitUntil


resolves #328

Copy link

changeset-bot bot commented Feb 10, 2025

🦋 Changeset detected

Latest commit: 107b285

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

This PR includes changesets to release 1 package
Name Type
@opennextjs/cloudflare 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

pkg-pr-new bot commented Feb 10, 2025

Open in Stackblitz

pnpm add https://pkg.pr.new/@opennextjs/cloudflare@364

commit: 107b285

Copy link
Contributor

@vicb vicb left a comment

Choose a reason for hiding this comment

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

Added some comments.
Testing unminified is the most important

@dario-piotrowicz dario-piotrowicz force-pushed the dario/328/fetch-cache-set-fix branch 2 times, most recently from c0c83b9 to 3f7381b Compare February 11, 2025 00:11
@dario-piotrowicz dario-piotrowicz mentioned this pull request Feb 11, 2025
Next.js does not await promises that update the incremental cache for fetch requests,
that is needed in our runtime otherwise the cache updates get lost, so this change
makes sure that the promise is properly awaited via `waitUntil`

Co-authored-by: Victor Berchet <[email protected]>
@dario-piotrowicz dario-piotrowicz force-pushed the dario/328/fetch-cache-set-fix branch from 0826365 to 61484b6 Compare February 11, 2025 10:16
@vicb
Copy link
Contributor

vicb commented Feb 11, 2025

@dario-piotrowicz maybe you try something like:

rule:
  kind: expression_statement
  all:
    - has: { pattern: "$$$_.then($$$_)", stopBy: end }
    - has: { pattern: "Buffer.from", stopBy: end }
    - has: { pattern: "$_.toString('base64')", stopBy: end }
    - has: { pattern: "$_.CachedRouteKind.FETCH", stopBy: end }

I have only tried on minified code but it should be possible to figure out something that works everywhere

@vicb
Copy link
Contributor

vicb commented Feb 11, 2025

rule:
  kind: expression_statement
  all:
    - has: { pattern: "$$$.then($$$)", stopBy: end }
    - has: { pattern: "Buffer.from", stopBy: end }
    - has: { pattern: "$_.toString('base64')", stopBy: end }
    - any:
        - has: { pattern: "CachedRouteKind.FETCH", stopBy: end }
        - has: { pattern: "$_.CachedRouteKind.FETCH", stopBy: end }
    - has: { pattern: "$$$.finally($$$)", stopBy: end }

@dario-piotrowicz
Copy link
Contributor Author

dario-piotrowicz commented Feb 11, 2025

rule:
  kind: expression_statement
  all:
    - has: { pattern: "$$$.then($$$)", stopBy: end }
    - has: { pattern: "Buffer.from", stopBy: end }
    - has: { pattern: "$_.toString('base64')", stopBy: end }
    - any:
        - has: { pattern: "CachedRouteKind.FETCH", stopBy: end }
        - has: { pattern: "$_.CachedRouteKind.FETCH", stopBy: end }
    - has: { pattern: "$$$.finally($$$)", stopBy: end }

this doesn't work, if you want you can check that in the playground I've been using, here's the link to it: https://pastebin.mozilla.org/eEiLmsaF

with my rule there are three matches, with yours 4 incorrect ones (I've also tried your rule in the adapter and it didn't result in a valid patch)

Copy link
Contributor

@vicb vicb left a comment

Choose a reason for hiding this comment

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

Nice work!

I added a comment inline, but LGTM 🚀

@dario-piotrowicz
Copy link
Contributor Author

One last thing before merging!

The usage of getCrossPlatformPathRegex seems to cause the chunk file not be be patched, I temporarily reverted that (5e97964) I'm looking into understanding why 👀

@conico974
Copy link
Collaborator

@dario-piotrowicz There might be an issue with getCrossPlatformPathRegex, i'm wondering if it has something to do with String.raw ?

@vicb
Copy link
Contributor

vicb commented Feb 11, 2025

@dario-piotrowicz There might be an issue with getCrossPlatformPathRegex, i'm wondering if it has something to do with String.raw ?

Looking at .source might help:

image

@dario-piotrowicz
Copy link
Contributor Author

dario-piotrowicz commented Feb 11, 2025

@dario-piotrowicz There might be an issue with getCrossPlatformPathRegex, i'm wondering if it has something to do with String.raw ?

No I've been debugging it and I'm pretty sure it has to do with the g flag that makes the regex side effectful (see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/lastIndex), that causes it to go out of whack and not match things properly

this fixes the issue:
Screenshot 2025-02-11 at 19 56 29

But ideally I think we should add an option to getCrossPlatformPathRegex to make the g flag opt-in (or opt-out) (or just remove it entirely)

@conico974
Copy link
Collaborator

@vicb
Copy link
Contributor

vicb commented Feb 11, 2025

Maybe we should switch to str.match(regex) instead.

I asked on the PR if we really need to g the regexs and say "it doesn't harm", that was poorly anticipating :)

@dario-piotrowicz
Copy link
Contributor Author

https://github.com/opennextjs/opennextjs-aws/blob/b59027a5899d25dd5263d1a272b33ec23fb683d3/packages/open-next/src/utils/regex.ts#L20 You should be able to change the flags (especially with escape false)

oops, I missed that the flags were already configurable! 😅👍

@dario-piotrowicz
Copy link
Contributor Author

Maybe we should switch to str.match(regex) instead.

good call! let's go with that 😄

@dario-piotrowicz
Copy link
Contributor Author

@vicb, @conico974 the PR is ready to merge please let me know if we're all happy with it 🙂

Copy link
Collaborator

@conico974 conico974 left a comment

Choose a reason for hiding this comment

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

LGTM
Nice work on this one

Copy link
Contributor

@vicb vicb left a comment

Choose a reason for hiding this comment

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

Awesome, thanks for your work on this PR 🎉

@dario-piotrowicz dario-piotrowicz merged commit 2e48d4f into main Feb 12, 2025
7 checks passed
@dario-piotrowicz dario-piotrowicz deleted the dario/328/fetch-cache-set-fix branch February 12, 2025 10:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Error when running the e2e for app-router (Fetch cache)
3 participants