|
1 |
| -import { Transform } from 'stream'; |
| 1 | +import { PassThrough, Transform } from 'stream'; |
2 | 2 | import { createReadableStreamFromReadable } from '@modern-js/runtime-utils/node';
|
3 | 3 | import checkIsBot from 'isbot';
|
4 | 4 | import { ServerStyleSheet } from 'styled-components';
|
@@ -30,12 +30,10 @@ export const createReadableStreamFromElement: CreateReadableStreamFromElement =
|
30 | 30 |
|
31 | 31 | const chunkVec: string[] = [];
|
32 | 32 |
|
33 |
| - const root = forceStream2String |
34 |
| - ? sheet.collectStyles(rootElement) |
35 |
| - : rootElement; |
| 33 | + const root = sheet.collectStyles(rootElement); |
36 | 34 |
|
37 | 35 | return new Promise(resolve => {
|
38 |
| - const { pipe } = renderToPipeableStream(root, { |
| 36 | + const { pipe: reactStreamingPipe } = renderToPipeableStream(root, { |
39 | 37 | nonce: config.nonce,
|
40 | 38 | [onReady]() {
|
41 | 39 | const styledComponentsStyleTags = forceStream2String
|
@@ -87,11 +85,21 @@ export const createReadableStreamFromElement: CreateReadableStreamFromElement =
|
87 | 85 | },
|
88 | 86 | });
|
89 | 87 |
|
| 88 | + // Transform the Node.js readable stream to a Web ReadableStream |
| 89 | + // For modern.js depend on hono.js, and we use Web standard |
90 | 90 | const stream = createReadableStreamFromReadable(body);
|
91 |
| - |
92 | 91 | resolve(stream);
|
93 | 92 |
|
94 |
| - pipe(body); |
| 93 | + // Transform the react pipe to a readable stream |
| 94 | + // Actually it's for type check, we even can execute `sheet.interleaveWithNodeStream({ pipe })` |
| 95 | + // Source code https://github.com/styled-components/styled-components/blob/main/packages/styled-components/src/models/ServerStyleSheet.tsx#L80 |
| 96 | + const passThrough = new PassThrough(); |
| 97 | + const styledStream = sheet.interleaveWithNodeStream(passThrough); |
| 98 | + reactStreamingPipe(passThrough); |
| 99 | + |
| 100 | + // pipe the styled stream to the body stream |
| 101 | + // now only use styled stream, if there is multiple stream, we can abstract it to a function |
| 102 | + styledStream.pipe(body); |
95 | 103 | });
|
96 | 104 | },
|
97 | 105 |
|
|
0 commit comments