refactor: refine ServerPayload type using a union with mode as discriminant (#701)

This commit is contained in:
Marco Pasqualetti
2025-04-07 08:21:25 +02:00
committed by GitHub
parent c946c43462
commit f3cf9b05ae
4 changed files with 30 additions and 32 deletions
+8 -3
View File
@@ -1,12 +1,17 @@
import type { JSX } from 'react' import type { JSX } from 'react'
import { useTuonoContextServerPayload } from './TuonoContext' import type { TuonoConfigServer } from '../config'
const VITE_PROXY_PATH = '/vite-server' const VITE_PROXY_PATH = '/vite-server'
const DEFAULT_SERVER_CONFIG = { host: 'localhost', origin: null, port: 3000 } const DEFAULT_SERVER_CONFIG = { host: 'localhost', origin: null, port: 3000 }
export const DevResources = (): JSX.Element => { interface DevResourcesProps {
const { devServerConfig } = useTuonoContextServerPayload() devServerConfig?: TuonoConfigServer
}
export const DevResources = ({
devServerConfig,
}: DevResourcesProps): JSX.Element => {
const { host, origin, port } = devServerConfig ?? DEFAULT_SERVER_CONFIG const { host, origin, port } = devServerConfig ?? DEFAULT_SERVER_CONFIG
const viteBaseUrl = const viteBaseUrl =
+8 -4
View File
@@ -1,10 +1,14 @@
import type { JSX } from 'react' import type { JSX } from 'react'
import { useTuonoContextServerPayload } from './TuonoContext' interface ProdResourcesProps {
jsBundles: Array<string> | null
export const ProdResources = (): JSX.Element => { cssBundles: Array<string> | null
const { cssBundles, jsBundles } = useTuonoContextServerPayload() }
export const ProdResources = ({
cssBundles,
jsBundles,
}: ProdResourcesProps): JSX.Element => {
return ( return (
<> <>
{cssBundles?.map((cssHref) => ( {cssBundles?.map((cssHref) => (
+9 -2
View File
@@ -12,8 +12,15 @@ export function TuonoScripts(): JSX.Element {
return ( return (
<> <>
<script>{`window['${SERVER_PAYLOAD_VARIABLE_NAME}']=${JSON.stringify(serverPayload)}`}</script> <script>{`window['${SERVER_PAYLOAD_VARIABLE_NAME}']=${JSON.stringify(serverPayload)}`}</script>
{serverPayload.mode === 'Dev' && <DevResources />} {serverPayload.mode === 'Dev' && (
{serverPayload.mode === 'Prod' && <ProdResources />} <DevResources devServerConfig={serverPayload.devServerConfig} />
)}
{serverPayload.mode === 'Prod' && (
<ProdResources
jsBundles={serverPayload.jsBundles}
cssBundles={serverPayload.cssBundles}
/>
)}
</> </>
) )
} }
+5 -23
View File
@@ -1,5 +1,7 @@
import type { ReactNode } from 'react' import type { ReactNode } from 'react'
import type { TuonoConfigServer } from './config'
/** /**
* Provided by the rust server and used in the ssr env * Provided by the rust server and used in the ssr env
* @see tuono-router {@link ServerInitialLocation} * @see tuono-router {@link ServerInitialLocation}
@@ -13,27 +15,11 @@ export interface ServerPayloadLocation {
/** /**
* @see crates/tuono_lib/src/payload.rs * @see crates/tuono_lib/src/payload.rs
*/ */
export interface ServerPayload<TData = unknown> { export type ServerPayload<TData = unknown> = {
mode: 'Prod' | 'Dev'
location: ServerPayloadLocation location: ServerPayloadLocation
data: TData data: TData
} & (
/** Available only on 'Prod' mode */
jsBundles: Array<string> | null
cssBundles: Array<string> | null
/** Available only on 'Dev' mode */
devServerConfig?: {
port: number
origin: string | null
host: string
}
}
/* the above type could be refined with an union like this
(
| { | {
mode: 'Prod' mode: 'Prod'
jsBundles: Array<string> jsBundles: Array<string>
@@ -41,13 +27,9 @@ export interface ServerPayload<TData = unknown> {
} }
| { | {
mode: 'Dev' mode: 'Dev'
devServerConfig: { devServerConfig?: TuonoConfigServer
port: number
host: string
}
} }
) )
*/
export type TuonoRouteProps<TData> = export type TuonoRouteProps<TData> =
| { | {