Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/fuzzy-eyes-pump.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@opennextjs/aws": patch
---

fix dangling promise on set for the fetch cache
2 changes: 2 additions & 0 deletions packages/open-next/src/build/createServerBundle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import { generateEdgeBundle } from "./edge/createEdgeBundle.js";
import * as buildHelper from "./helper.js";
import { installDependencies } from "./installDeps.js";
import { type CodePatcher, applyCodePatches } from "./patch/codePatcher.js";
import { patchFetchCacheSetMissingWaitUntil } from "./patch/patchFetchCacheWaitUntil.js";

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

await applyCodePatches(options, tracedFiles, manifests, [
patchFetchCacheSetMissingWaitUntil,
...additionalCodePatches,
]);

Expand Down
42 changes: 42 additions & 0 deletions packages/open-next/src/build/patch/patchFetchCacheWaitUntil.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import { getCrossPlatformPathRegex } from "utils/regex.js";
import { createPatchCode } from "./astCodePatcher.js";
import type { CodePatcher } from "./codePatcher";

export const rule = `
rule:
kind: call_expression
pattern: $PROMISE
all:
- has: { pattern: $_.arrayBuffer().then, stopBy: end }
- has: { pattern: "Buffer.from", stopBy: end }
- any:
- inside:
kind: sequence_expression
inside:
kind: return_statement
- inside:
kind: expression_statement
precedes:
kind: return_statement
- has: { pattern: $_.FETCH, stopBy: end }

fix: |
globalThis.__openNextAls?.getStore()?.pendingPromiseRunner.add($PROMISE)
`;

export const patchFetchCacheSetMissingWaitUntil: CodePatcher = {
name: "patch-fetch-cache-set-missing-wait-until",
patches: [
{
versions: ">=15.0.0",
field: {
pathFilter: getCrossPlatformPathRegex(
String.raw`(server/chunks/.*\.js|.*\.runtime\..*\.js|patch-fetch\.js)$`,
{ escape: false },
),
contentFilter: /arrayBuffer\(\)\s*\.then/,
patchCode: createPatchCode(rule),
},
},
],
};
Loading