Skip to content

fix: make patchAsyncStorage work with non-mutable node:module modules #730

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

Conversation

dario-piotrowicz
Copy link
Contributor

This change allows patchAsyncStorage to work with the cloudflare adapter allowing us to get rid of the code that comments the function out

The problem with the current patch is that it updates mod._resolveFilename which doesn't work in the cloudflare adapter since the worker gets build by esbuild making the exports of the module (which we polyfill using unenv) non-settable:
Screenshot 2025-02-07 at 17 46 41

Warning

Disclamer: I did test the Cloudflare adapter with this change and this does allow us to use patchAsyncStorage without getting the runtime error anymore, I could however not see any other difference in the app I tested this with (the app-router app), if anyone would have suggestions on how/if I can check some effects that the patch has that would be very much appreciated 🙏

Copy link

changeset-bot bot commented Feb 7, 2025

🦋 Changeset detected

Latest commit: 35be04f

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

This PR includes changesets to release 3 packages
Name Type
@opennextjs/aws Patch
app-pages-router Patch
app-router 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 7, 2025

Open in Stackblitz

pnpm add https://pkg.pr.new/@opennextjs/aws@730

commit: 35be04f

Copy link
Contributor

github-actions bot commented Feb 7, 2025

Coverage Report

Status Category Percentage Covered / Total
🔵 Lines 24.27% 1950 / 8032
🔵 Statements 24.27% 1950 / 8032
🔵 Functions 51.61% 112 / 217
🔵 Branches 71.34% 488 / 684
File Coverage
File Stmts Branches Functions Lines Uncovered Lines
Changed Files
packages/open-next/src/core/patchAsyncStorage.ts 0% 0% 0% 0% 1-36
Generated in workflow #945 for commit 35be04f by the Vitest Coverage Report Action


const resolveFilename = mod._resolveFilename;

export function patchAsyncStorage() {
mod._resolveFilename = ((
const _resolveFilename = ((
Copy link
Contributor

Choose a reason for hiding this comment

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

I don't think this is correct path on workers.

_resolveFilename is not used on workers (it throws) as we do not use dynamic require - requires/imports are resolved at build time.

Copy link
Contributor

@vicb vicb Feb 8, 2025

Choose a reason for hiding this comment

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

Maybe we can use a build time patch instead, replacing the Next version with the patch.

That should be pretty easy to do via the onResolve hook of an ESBuild plugin

Copy link
Contributor Author

Choose a reason for hiding this comment

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

ah ok I see, yes that's a good call thanks 🙂👍

I'm happy to close the PR if we don't see the point in this then (however it is a bit too bad that aws does something that then we need to un-do, I wonder if that could be solved somehow at some point 😕)

Copy link
Contributor

@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.

I'm fine with this change as long as it works (i can't test it today)
FYI this patch is meant to make the fetch cache works when we do ISR revalidation, without it fetch cache will just be ignored.
BTW i just realized that i didn't make any e2e test for this

@dario-piotrowicz
Copy link
Contributor Author

Thanks for having a look @conico974 🫶

But I'm going to close the PR as per @vicb's suggestion, the fix should probably be done in our adapter

PS: if you were to add an e2e test please do let me know as I would be interested to see the effect of this function, if this function is definitely necessary for fetch cache I wonder why my change was working in our adapter (unless our adapter was throwing but swallowing the errors?! 🤔)

@dario-piotrowicz dario-piotrowicz deleted the dario/patchAsyncStorage/reassign-mod-instead-of-updating-it branch February 8, 2025 16:59
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.

3 participants