-
Notifications
You must be signed in to change notification settings - Fork 73
Fix fetch inside use cache
in ISR
#666
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
Merged
Merged
Changes from 4 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"@opennextjs/cloudflare": patch | ||
--- | ||
|
||
fix fetch inside `use cache` in ISR |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
22 changes: 22 additions & 0 deletions
22
examples/e2e/experimental/src/app/use-cache/fetch/page.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import { ISRComponent } from "@/components/cached"; | ||
import { Suspense } from "react"; | ||
|
||
async function getFromFetch() { | ||
"use cache"; | ||
// This is a simple fetch to ensure that the cache is working with IO inside | ||
const res = await fetch("https://opennext.js.org"); | ||
return res.headers.get("Date"); | ||
} | ||
|
||
export default async function Page() { | ||
const date = await getFromFetch(); | ||
return ( | ||
<div> | ||
<h1>Cache</h1> | ||
<p data-testid="date">{date}</p> | ||
<Suspense fallback={<p>Loading...</p>}> | ||
<ISRComponent /> | ||
</Suspense> | ||
</div> | ||
); | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
159 changes: 159 additions & 0 deletions
159
packages/cloudflare/src/cli/build/patches/plugins/use-cache.spec.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,159 @@ | ||
import { patchCode } from "@opennextjs/aws/build/patch/astCodePatcher.js"; | ||
import { expect, test } from "vitest"; | ||
|
||
import { rule } from "./use-cache.js"; | ||
|
||
const codeToPatch = `"use strict"; | ||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
0 && (module.exports = { | ||
bindSnapshot: null, | ||
createAsyncLocalStorage: null, | ||
createSnapshot: null | ||
}); | ||
function _export(target, all) { | ||
for(var name in all)Object.defineProperty(target, name, { | ||
enumerable: true, | ||
get: all[name] | ||
}); | ||
} | ||
_export(exports, { | ||
bindSnapshot: function() { | ||
return bindSnapshot; | ||
}, | ||
createAsyncLocalStorage: function() { | ||
return createAsyncLocalStorage; | ||
}, | ||
createSnapshot: function() { | ||
return createSnapshot; | ||
} | ||
}); | ||
const sharedAsyncLocalStorageNotAvailableError = Object.defineProperty(new Error('Invariant: AsyncLocalStorage accessed in runtime where it is not available'), "__NEXT_ERROR_CODE", { | ||
value: "E504", | ||
enumerable: false, | ||
configurable: true | ||
}); | ||
class FakeAsyncLocalStorage { | ||
disable() { | ||
throw sharedAsyncLocalStorageNotAvailableError; | ||
} | ||
getStore() { | ||
// This fake implementation of AsyncLocalStorage always returns \`undefined\`. | ||
return undefined; | ||
} | ||
run() { | ||
throw sharedAsyncLocalStorageNotAvailableError; | ||
} | ||
exit() { | ||
throw sharedAsyncLocalStorageNotAvailableError; | ||
} | ||
enterWith() { | ||
throw sharedAsyncLocalStorageNotAvailableError; | ||
} | ||
static bind(fn) { | ||
return fn; | ||
} | ||
} | ||
const maybeGlobalAsyncLocalStorage = typeof globalThis !== 'undefined' && globalThis.AsyncLocalStorage; | ||
function createAsyncLocalStorage() { | ||
if (maybeGlobalAsyncLocalStorage) { | ||
return new maybeGlobalAsyncLocalStorage(); | ||
} | ||
return new FakeAsyncLocalStorage(); | ||
} | ||
function bindSnapshot(fn) { | ||
if (maybeGlobalAsyncLocalStorage) { | ||
return maybeGlobalAsyncLocalStorage.bind(fn); | ||
} | ||
return FakeAsyncLocalStorage.bind(fn); | ||
} | ||
function createSnapshot() { | ||
if (maybeGlobalAsyncLocalStorage) { | ||
return maybeGlobalAsyncLocalStorage.snapshot(); | ||
} | ||
return function(fn, ...args) { | ||
return fn(...args); | ||
}; | ||
} | ||
|
||
//# sourceMappingURL=async-local-storage.js.map | ||
`; | ||
|
||
test("patch the createSnapshot function", () => { | ||
const patchedCode = patchCode(codeToPatch, rule); | ||
expect(patchedCode).toMatchInlineSnapshot(`""use strict"; | ||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
0 && (module.exports = { | ||
bindSnapshot: null, | ||
createAsyncLocalStorage: null, | ||
createSnapshot: null | ||
}); | ||
function _export(target, all) { | ||
for(var name in all)Object.defineProperty(target, name, { | ||
enumerable: true, | ||
get: all[name] | ||
}); | ||
} | ||
_export(exports, { | ||
bindSnapshot: function() { | ||
return bindSnapshot; | ||
}, | ||
createAsyncLocalStorage: function() { | ||
return createAsyncLocalStorage; | ||
}, | ||
createSnapshot: function() { | ||
return createSnapshot; | ||
} | ||
}); | ||
const sharedAsyncLocalStorageNotAvailableError = Object.defineProperty(new Error('Invariant: AsyncLocalStorage accessed in runtime where it is not available'), "__NEXT_ERROR_CODE", { | ||
value: "E504", | ||
enumerable: false, | ||
configurable: true | ||
}); | ||
class FakeAsyncLocalStorage { | ||
disable() { | ||
throw sharedAsyncLocalStorageNotAvailableError; | ||
} | ||
getStore() { | ||
// This fake implementation of AsyncLocalStorage always returns \`undefined\`. | ||
return undefined; | ||
} | ||
run() { | ||
throw sharedAsyncLocalStorageNotAvailableError; | ||
} | ||
exit() { | ||
throw sharedAsyncLocalStorageNotAvailableError; | ||
} | ||
enterWith() { | ||
throw sharedAsyncLocalStorageNotAvailableError; | ||
} | ||
static bind(fn) { | ||
return fn; | ||
} | ||
} | ||
const maybeGlobalAsyncLocalStorage = typeof globalThis !== 'undefined' && globalThis.AsyncLocalStorage; | ||
function createAsyncLocalStorage() { | ||
if (maybeGlobalAsyncLocalStorage) { | ||
return new maybeGlobalAsyncLocalStorage(); | ||
} | ||
return new FakeAsyncLocalStorage(); | ||
} | ||
function bindSnapshot(fn) { | ||
if (maybeGlobalAsyncLocalStorage) { | ||
return maybeGlobalAsyncLocalStorage.bind(fn); | ||
} | ||
return FakeAsyncLocalStorage.bind(fn); | ||
} | ||
function createSnapshot() { | ||
// Ignored snapshot | ||
return function(fn, ...args) { | ||
return fn(...args); | ||
}; | ||
} | ||
|
||
//# sourceMappingURL=async-local-storage.js.map | ||
"`); | ||
}); |
39 changes: 39 additions & 0 deletions
39
packages/cloudflare/src/cli/build/patches/plugins/use-cache.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
/** | ||
* This patch will replace the createSnapshot function in the | ||
* server/app-render/async-local-storage.js file to an empty string. | ||
* This is necessary because the createSnapshot function is causing I/O issues for | ||
* ISR/SSG revalidation in Cloudflare Workers. | ||
* TODO: Find a better fix for this issue. | ||
*/ | ||
import { patchCode } from "@opennextjs/aws/build/patch/astCodePatcher.js"; | ||
import type { CodePatcher } from "@opennextjs/aws/build/patch/codePatcher.js"; | ||
import { getCrossPlatformPathRegex } from "@opennextjs/aws/utils/regex.js"; | ||
|
||
export const rule = ` | ||
rule: | ||
kind: if_statement | ||
inside: | ||
kind: function_declaration | ||
stopBy: end | ||
has: | ||
kind: identifier | ||
pattern: createSnapshot | ||
fix: | ||
'// Ignored snapshot' | ||
`; | ||
|
||
export const patchUseCacheIO: CodePatcher = { | ||
name: "patch-use-cache", | ||
patches: [ | ||
{ | ||
versions: ">=15.3.1", | ||
field: { | ||
pathFilter: getCrossPlatformPathRegex(String.raw`server/app-render/async-local-storage\.js$`, { | ||
escape: false, | ||
}), | ||
contentFilter: /createSnapshot/, | ||
patchCode: async ({ code }) => patchCode(code, rule), | ||
}, | ||
}, | ||
], | ||
}; |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you please add more details from the PR discussion:
https://github.com/vercel/next.js/blob/62de494f/packages/next/src/server/app-render/async-local-storage.ts#L55-L65