Skip to content

Commit 3ab1688

Browse files
committed
[Cache Components] Allow sync IO inside console methods
console methods may be patched to do things like add a timestamp to the log row. While we can't determine the meaning of readin the current time in arbitrary contexts we can know with reasonable certainty that any sync IO inside a console method is not being used in the logical output of the program. To account for this we can make a special affordance for sync IO inside of these methods that allows them to run without interrupting the prerender early. Originall I wanted to make it so that even if you patch these methods in userland we can still exit the context but it turns out this is very complex and difficult because we have many patches layered together and they need to run in the correct order. So essentially this just permits sync IO in the preexisting implementations of the console methods. This is probably sufficient since typically these methods aren't patched in userspace but in the runtime host itself. If we recieve feedback that this is insufficient we can consider alternative later. The original implementation exited the workUnitAsyncStorage but it turns out that some console methods like assert call others from their internal implementation. This means that you lose the context at the top of the patch stack for things like dimming console logs if you do this. Instead we simply set a temporary flag to allow sync IO during the a portion of the prerender. It is important to guard this with a try/finally and always synchronously otherwise you can accidentally and racily allow sync IO in other parts of the program that you did not intend
1 parent 4d7f836 commit 3ab1688

File tree

5 files changed

+802
-0
lines changed

5 files changed

+802
-0
lines changed

packages/next/src/server/app-render/work-unit-async-storage.external.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,14 @@ interface PrerenderStoreModernCommon
192192
*/
193193
readonly hmrRefreshHash: string | undefined
194194

195+
/**
196+
* For certain special case synchronous operations we allow the temporary disabling of
197+
* sync IO aborting. This must always be used in a try/finally block to ensure that the
198+
* original behavior is restored before exiting the sync block. If you aren't sure if you
199+
* should use this then ask first.
200+
*/
201+
allowSyncIO?: boolean
202+
195203
/**
196204
* Only available in dev mode.
197205
*/

0 commit comments

Comments
 (0)