From 62926c17cf78b0f5f1cb560f497ca8160f489bc5 Mon Sep 17 00:00:00 2001 From: harpsealjs Date: Thu, 31 Oct 2024 18:15:53 +0800 Subject: [PATCH] feat: support `queueMicroTask` on the server side of SSR (#3873) --- e2e/cases/server/ssr/src/assert.server.ts | 9 +++++++++ e2e/cases/server/ssr/src/index.server.tsx | 4 ++++ packages/core/src/server/runner/cjs.ts | 2 ++ 3 files changed, 15 insertions(+) create mode 100644 e2e/cases/server/ssr/src/assert.server.ts diff --git a/e2e/cases/server/ssr/src/assert.server.ts b/e2e/cases/server/ssr/src/assert.server.ts new file mode 100644 index 0000000000..a35557cb73 --- /dev/null +++ b/e2e/cases/server/ssr/src/assert.server.ts @@ -0,0 +1,9 @@ +function assertQueueMicroTask() { + if (typeof queueMicrotask !== 'function') { + throw Error('not support queueMicrotask in this environment'); + } +} + +export function assert() { + assertQueueMicroTask(); +} diff --git a/e2e/cases/server/ssr/src/index.server.tsx b/e2e/cases/server/ssr/src/index.server.tsx index 9c0b1b1aad..f908d5fa6d 100644 --- a/e2e/cases/server/ssr/src/index.server.tsx +++ b/e2e/cases/server/ssr/src/index.server.tsx @@ -1,10 +1,14 @@ import React from 'react'; import ReactDOMServer from 'react-dom/server'; import App from './App'; +import { assert } from './assert.server'; // test dynamic import import('./test'); +// assert environment +assert(); + export function render() { return ReactDOMServer.renderToString( diff --git a/packages/core/src/server/runner/cjs.ts b/packages/core/src/server/runner/cjs.ts index bcb0abcfed..81628a6d0a 100644 --- a/packages/core/src/server/runner/cjs.ts +++ b/packages/core/src/server/runner/cjs.ts @@ -29,6 +29,7 @@ export class CommonJsRunner extends BasicRunner { return timeout; }) as typeof setTimeout, clearTimeout: clearTimeout, + queueMicrotask, }; } @@ -43,6 +44,7 @@ export class CommonJsRunner extends BasicRunner { }); return m; }, + queueMicrotask, }; return baseModuleScope; }