Skip to content

Commit a6da6ac

Browse files
authored
Fix: fetch cache set missing wait until (opennextjs#782)
* patch fetch cache waitUntil * add unit test * changeset * fix rebase * fix changeset
1 parent 0ca267a commit a6da6ac

File tree

4 files changed

+509
-0
lines changed

4 files changed

+509
-0
lines changed

.changeset/fuzzy-eyes-pump.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@opennextjs/aws": patch
3+
---
4+
5+
fix dangling promise on set for the fetch cache

packages/open-next/src/build/createServerBundle.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import { generateEdgeBundle } from "./edge/createEdgeBundle.js";
1717
import * as buildHelper from "./helper.js";
1818
import { installDependencies } from "./installDeps.js";
1919
import { type CodePatcher, applyCodePatches } from "./patch/codePatcher.js";
20+
import { patchFetchCacheSetMissingWaitUntil } from "./patch/patchFetchCacheWaitUntil.js";
2021

2122
interface CodeCustomization {
2223
// These patches are meant to apply on user and next generated code
@@ -179,6 +180,7 @@ async function generateBundle(
179180
const additionalCodePatches = codeCustomization?.additionalCodePatches ?? [];
180181

181182
await applyCodePatches(options, tracedFiles, manifests, [
183+
patchFetchCacheSetMissingWaitUntil,
182184
...additionalCodePatches,
183185
]);
184186

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import { getCrossPlatformPathRegex } from "utils/regex.js";
2+
import { createPatchCode } from "./astCodePatcher.js";
3+
import type { CodePatcher } from "./codePatcher";
4+
5+
export const rule = `
6+
rule:
7+
kind: call_expression
8+
pattern: $PROMISE
9+
all:
10+
- has: { pattern: $_.arrayBuffer().then, stopBy: end }
11+
- has: { pattern: "Buffer.from", stopBy: end }
12+
- any:
13+
- inside:
14+
kind: sequence_expression
15+
inside:
16+
kind: return_statement
17+
- inside:
18+
kind: expression_statement
19+
precedes:
20+
kind: return_statement
21+
- has: { pattern: $_.FETCH, stopBy: end }
22+
23+
fix: |
24+
globalThis.__openNextAls?.getStore()?.pendingPromiseRunner.add($PROMISE)
25+
`;
26+
27+
export const patchFetchCacheSetMissingWaitUntil: CodePatcher = {
28+
name: "patch-fetch-cache-set-missing-wait-until",
29+
patches: [
30+
{
31+
versions: ">=15.0.0",
32+
field: {
33+
pathFilter: getCrossPlatformPathRegex(
34+
String.raw`(server/chunks/.*\.js|.*\.runtime\..*\.js|patch-fetch\.js)$`,
35+
{ escape: false },
36+
),
37+
contentFilter: /arrayBuffer\(\)\s*\.then/,
38+
patchCode: createPatchCode(rule),
39+
},
40+
},
41+
],
42+
};

0 commit comments

Comments
 (0)