Skip to content

Commit cf97e74

Browse files
committed
don't call onClose in static generation
1 parent 7e80ced commit cf97e74

File tree

1 file changed

+12
-3
lines changed

1 file changed

+12
-3
lines changed

packages/next/src/server/after/after.ts

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -135,9 +135,18 @@ export function createAfterContext({
135135
: callback())
136136

137137
// NOTE: if the callback returns a stream, there may still be components that'll execute later,
138-
// which means that more callbacks can be added.
139-
// TODO: can we call onClose lazily?
140-
onClose(() => runCallbacks(requestStore))
138+
// which means that more callbacks can be added after this point.
139+
140+
// `onClose` is not available in static generation.
141+
// after() calls will throw and bail out anyway, but streaming can make them happen later,
142+
// so we don't want to crash here.
143+
const hasOnClose =
144+
!staticGenerationAsyncStorage.getStore()?.isStaticGeneration
145+
146+
if (hasOnClose) {
147+
// TODO: can we call onClose lazily somehow?
148+
onClose(() => runCallbacks(requestStore))
149+
}
141150
return res
142151
} finally {
143152
// if something failed, make sure the request doesn't stay open forever.

0 commit comments

Comments
 (0)