Skip to content

Commit

Permalink
refactor(packages/tuono): ssr - improve resources management
Browse files Browse the repository at this point in the history
  • Loading branch information
marcalexiei committed Jan 2, 2025
1 parent a008646 commit f6c9431
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 52 deletions.
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import type { ReactNode } from 'react'
import type { JSX } from 'react'

const TUONO_DEV_SERVER_PORT = 3000
const VITE_PROXY_PATH = '/vite-server'
const SCRIPT_BASE_URL = `http://localhost:${TUONO_DEV_SERVER_PORT}${VITE_PROXY_PATH}`

export const ViteScripts = (): ReactNode => (
export const DevResources = (): JSX.Element => (
<>
<script type="module">
{[
Expand Down
26 changes: 26 additions & 0 deletions packages/tuono/src/ssr/components/ProdResources.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import type { JSX } from 'react'

interface ProdResourcesProps {
cssBundles: Array<string>
jsBundles: Array<string>
}

export const ProdResources = ({
cssBundles,
jsBundles,
}: ProdResourcesProps): JSX.Element => (
<>
{cssBundles.map((cssHref) => (
<link
key={cssHref}
rel="stylesheet"
type="text/css"
href={`/${cssHref}`}
/>
))}

{jsBundles.map((scriptSrc) => (
<script key={scriptSrc} type="module" src={`/${scriptSrc}`}></script>
))}
</>
)
41 changes: 0 additions & 41 deletions packages/tuono/src/ssr/components/ProductionTags.tsx

This file was deleted.

17 changes: 8 additions & 9 deletions packages/tuono/src/ssr/server.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,8 @@ import { renderToReadableStream } from 'react-dom/server'
import { RouterProvider, createRouter } from 'tuono-router'
import type { createRoute } from 'tuono-router'

import {
ProductionScriptLinks,
ProductionCssLinks,
} from './components/ProductionTags'
import { ViteScripts } from './components/ViteScripts'

import { DevResources } from './components/DevResources'
import { ProdResources } from './components/ProdResources'
import type { Mode } from './types'
import { streamToString } from './utils'

Expand All @@ -69,10 +65,13 @@ export function serverSideRendering(routeTree: RouteTree) {

const stream = await renderToReadableStream(
<>
<ProductionCssLinks mode={mode} bundles={cssBundles} />
<ProductionScriptLinks mode={mode} bundles={jsBundles} />
<RouterProvider router={router} serverProps={serverProps as never} />
{mode === 'Dev' && <ViteScripts />}

{mode === 'Dev' && <DevResources />}
{mode === 'Prod' && (
<ProdResources cssBundles={cssBundles} jsBundles={jsBundles} />
)}

<script>{`window.__TUONO_SSR_PROPS__=${payload as string}`}</script>
</>,
)
Expand Down

0 comments on commit f6c9431

Please sign in to comment.