Skip to content

Commit

Permalink
feat: support config ssr.renderMode
Browse files Browse the repository at this point in the history
  • Loading branch information
sorrycc committed Nov 25, 2024
1 parent 8841c62 commit c70bc97
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 6 deletions.
5 changes: 5 additions & 0 deletions .changeset/fast-turtles-pump.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@umijs/tnf': patch
---

feat: support config ssr render mode
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ Config is loaded from `.tnfrc.ts` by default.
- `externals: Record<string, string>`: An object that maps package names to their corresponding paths.
- `less: { modifyVars?: Record<string, string>; globalVars?: Record<string, string>; math?: 'always' | 'strict' | 'parens-division' | 'parens' | 'strict-legacy' | number; sourceMap?: any; plugins?: (string | [string, Record<string, any>])[];}`: The configuration passed to lessLoader.
- `router: { defaultPreload?: 'intent' | 'render' | 'viewport'; defaultPreloadDelay?: number; devtool?: { options?: { initialIsOpen?: boolean; position?: 'bottom-left' | 'bottom-right' | 'top-left' | 'top-right' }; } | false; convention?: any }`: The router configuration. Checkout [@tanstack/router-generator](https://github.com/TanStack/router/blob/cc05ad8/packages/router-generator/src/config.ts#L22-L67) for convention config.
- `ssr: {}`: The ssr configuration.
- `ssr: { renderMode?: 'stream' | 'string' }`: The ssr configuration.
- `tailwindcss: boolean`: Turn on/off tailwindcss. Need to be used in conjunction with `src/tailwind.css` and `tailwind.config.js`.

## FAQ
Expand Down
6 changes: 5 additions & 1 deletion src/config/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,11 @@ export const ConfigSchema = z.object({
convention: RouterGeneratorConfig,
})
.optional(),
ssr: z.object({}).optional(),
ssr: z
.object({
renderMode: z.enum(['stream', 'string']).optional(),
})
.optional(),
tailwindcss: z.boolean().optional(),
clickToComponent: z
.union([
Expand Down
18 changes: 14 additions & 4 deletions src/sync/write_server_entry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,19 @@ export function writeServerEntry({ opts }: { opts: SyncOptions }) {
paths: { tmpPath },
} = opts.context;

const renderMode = opts.context.config.ssr?.renderMode || 'stream';
const renderer =
renderMode === 'stream'
? `
const appHtml = ReactDOMServer.renderToPipeableStream(<Server router={router} />, {
bootstrapScripts: ['/client.js'],
});
appHtml.pipe(response);
`
: `
const appHtml = ReactDOMServer.renderToString(<Server router={router} />);
response.end(appHtml + '<script src="/client.js"></script>');
`;
writeFileSync(
path.join(tmpPath, 'server.tsx'),
`
Expand All @@ -26,12 +39,9 @@ export async function render(request: Request, response: any) {
history: memoryHistory,
});
await router.load();
const appHtml = ReactDOMServer.renderToPipeableStream(<Server router={router} />, {
bootstrapScripts: ['/client.js'],
});
response.statusCode = router.hasNotFoundMatch() ? 404 : 200;
response.setHeader('Content-Type', 'text/html');
appHtml.pipe(response);
${renderer}
}
`,
);
Expand Down

0 comments on commit c70bc97

Please sign in to comment.