Skip to content

Commit 8f1d2b4

Browse files
authored
Fix: await cache set (#446)
* await cache set * add new lint rules * fix all linting issues * Refactor detached promise handling * fix e2e remove lint from next build * fix example and docs * Create eight-parrots-peel.md
1 parent c50f2c8 commit 8f1d2b4

File tree

23 files changed

+132
-43
lines changed

23 files changed

+132
-43
lines changed

.changeset/eight-parrots-peel.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
---
2+
"open-next-docs": patch
3+
"open-next-benchmark": patch
4+
"app-pages-router": patch
5+
"app-router": patch
6+
"pages-router": patch
7+
"open-next": patch
8+
"tests-e2e": patch
9+
---
10+
11+
Fix: dangling promises

.eslintrc.cjs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,5 +16,24 @@ module.exports = {
1616
"sonarjs/elseif-without-else": "warn",
1717
"sonarjs/no-duplicate-string": "warn",
1818
"sonarjs/cognitive-complexity": "warn",
19+
20+
// We add some typescript rules - The recommended rules breaks too much stuff
21+
// TODO: We should add more rules, especially around typescript types
22+
23+
// Promises related rules
24+
"@typescript-eslint/await-thenable": "error",
25+
"@typescript-eslint/no-floating-promises": "error",
26+
"@typescript-eslint/no-misused-promises": [
27+
"error",
28+
{ checksVoidReturn: false },
29+
],
30+
31+
"@typescript-eslint/unbound-method": "error",
32+
33+
"@typescript-eslint/no-non-null-assertion": "warn",
34+
},
35+
parserOptions: {
36+
project: ["./tsconfig.eslint.json", "./**/tsconfig.json"],
1937
},
38+
ignorePatterns: ["**/node_modules/**", "**/dist/**", "**/out/**"],
2039
};

docs/next.config.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,9 @@ const withNextra = require("nextra")({
3434

3535
module.exports = withNextra({
3636
swcMinify: true,
37+
eslint: {
38+
ignoreDuringBuilds: true,
39+
},
3740
images: {
3841
unoptimized: true,
3942
},

example/next.config.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@ const nextConfig = {
33
reactStrictMode: true,
44
cleanDistDir: true,
55
swcMinify: true,
6+
eslint: {
7+
ignoreDuringBuilds: true,
8+
},
69
images: {
710
remotePatterns: [
811
{

examples/app-pages-router/next.config.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ const nextConfig = {
88
experimental: {
99
serverActions: true,
1010
},
11+
eslint: {
12+
ignoreDuringBuilds: true,
13+
},
1114
trailingSlash: true,
1215
skipTrailingSlashRedirect: true,
1316
};

examples/app-router/app/api/sse/route.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,15 @@ export async function GET(request: NextRequest) {
1616
});
1717

1818
setTimeout(async () => {
19-
writer.write(
19+
await writer.write(
2020
`data: ${JSON.stringify({
2121
message: "open",
2222
time: new Date().toISOString(),
2323
})}\n\n`,
2424
);
2525
for (let i = 1; i <= 4; i++) {
2626
await wait(2000);
27-
writer.write(
27+
await writer.write(
2828
`data: ${JSON.stringify({
2929
message: "hello:" + i,
3030
time: new Date().toISOString(),
@@ -33,7 +33,7 @@ export async function GET(request: NextRequest) {
3333
}
3434

3535
await wait(2000); // Wait for 4 seconds
36-
writer.write(
36+
await writer.write(
3737
`data: ${JSON.stringify({
3838
message: "close",
3939
time: new Date().toISOString(),

examples/app-router/next.config.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ const nextConfig = {
88
experimental: {
99
serverActions: true,
1010
},
11+
eslint: {
12+
ignoreDuringBuilds: true,
13+
},
1114
images: {
1215
remotePatterns: [
1316
{

examples/pages-router/next.config.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@ const nextConfig = {
55
reactStrictMode: true,
66
output: "standalone",
77
outputFileTracing: "../sst",
8+
eslint: {
9+
ignoreDuringBuilds: true,
10+
},
811
rewrites: () => [
912
{ source: "/rewrite", destination: "/" },
1013
{

packages/open-next/src/adapters/cache.ts

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
import { DetachedPromise } from "utils/promise.js";
2-
31
import { IncrementalCache } from "../cache/incremental/types.js";
42
import { TagCache } from "../cache/tag/types.js";
53
import { isBinaryContentType } from "./binary.js";
@@ -225,8 +223,11 @@ export default class S3Cache {
225223
if (globalThis.disableIncrementalCache) {
226224
return;
227225
}
228-
const detachedPromise = new DetachedPromise<void>();
229-
globalThis.__als.getStore()?.pendingPromises.push(detachedPromise);
226+
// This one might not even be necessary anymore
227+
// Better be safe than sorry
228+
const detachedPromise = globalThis.__als
229+
.getStore()
230+
?.pendingPromiseRunner.withResolvers<void>();
230231
try {
231232
if (data?.kind === "ROUTE") {
232233
const { body, status, headers } = data;
@@ -250,7 +251,7 @@ export default class S3Cache {
250251
const { html, pageData } = data;
251252
const isAppPath = typeof pageData === "string";
252253
if (isAppPath) {
253-
globalThis.incrementalCache.set(
254+
await globalThis.incrementalCache.set(
254255
key,
255256
{
256257
type: "app",
@@ -260,7 +261,7 @@ export default class S3Cache {
260261
false,
261262
);
262263
} else {
263-
globalThis.incrementalCache.set(
264+
await globalThis.incrementalCache.set(
264265
key,
265266
{
266267
type: "page",
@@ -312,7 +313,7 @@ export default class S3Cache {
312313
error("Failed to set cache", e);
313314
} finally {
314315
// We need to resolve the promise even if there was an error
315-
detachedPromise.resolve();
316+
detachedPromise?.resolve();
316317
}
317318
}
318319

packages/open-next/src/build.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ export async function build(
7676
// Build Next.js app
7777
printHeader("Building Next.js app");
7878
setStandaloneBuildMode(monorepoRoot);
79-
await buildNextjsApp(packager);
79+
buildNextjsApp(packager);
8080

8181
// Generate deployable bundle
8282
printHeader("Generating bundle");
@@ -280,7 +280,7 @@ async function createRevalidationBundle(config: OpenNextConfig) {
280280
copyOpenNextConfig(options.tempDir, outputPath);
281281

282282
// Build Lambda code
283-
esbuildAsync(
283+
await esbuildAsync(
284284
{
285285
external: ["next", "styled-jsx", "react"],
286286
entryPoints: [path.join(__dirname, "adapters", "revalidate.js")],

0 commit comments

Comments
 (0)