@@ -120,8 +120,11 @@ export type ServerIslandRegistry = Map<ComponentType, Island>;
120120export const internals : unique symbol = Symbol ( "fresh_internal" ) ;
121121
122122export interface UiTree < Data , State > {
123- app : AnyComponent < PageProps < Data , State > > | null ;
124- layouts : ComponentDef < Data , State > [ ] ;
123+ app : {
124+ component : AnyComponent < PageProps < Data , State > > ;
125+ css : string [ ] | null ;
126+ } | null ;
127+ layouts : ( ComponentDef < Data , State > & { css : string [ ] | null } ) [ ] ;
125128}
126129
127130/**
@@ -131,7 +134,10 @@ export type FreshContext<State = unknown> = Context<State>;
131134
132135export let getBuildCache : < T > ( ctx : Context < T > ) => BuildCache < T > ;
133136export let getInternals : < T > ( ctx : Context < T > ) => UiTree < unknown , T > ;
134- export let setAdditionalStyles : < T > ( ctx : Context < T > , css : string [ ] ) => void ;
137+ export let setAdditionalStyles : < T > (
138+ ctx : Context < T > ,
139+ css : string [ ] | null | undefined ,
140+ ) => void ;
135141
136142/**
137143 * The context passed to every middleware. It is unique for every request.
@@ -204,8 +210,23 @@ export class Context<State> {
204210 // deno-lint-ignore no-explicit-any
205211 getInternals = < T > ( ctx : Context < T > ) => ctx . #internal as any ;
206212 getBuildCache = < T > ( ctx : Context < T > ) => ctx . #buildCache;
207- setAdditionalStyles = < T > ( ctx : Context < T > , css : string [ ] ) =>
208- ctx . #additionalStyles = css ;
213+ setAdditionalStyles = < T > (
214+ ctx : Context < T > ,
215+ css : string [ ] | null | undefined ,
216+ ) => {
217+ if ( css == null ) return ;
218+
219+ if ( ctx . #additionalStyles === null ) {
220+ ctx . #additionalStyles = css . slice ( ) ;
221+ return ;
222+ }
223+
224+ for ( const href of css ) {
225+ if ( ! ctx . #additionalStyles. includes ( href ) ) {
226+ ctx . #additionalStyles. push ( href ) ;
227+ }
228+ }
229+ } ;
209230 }
210231
211232 constructor (
@@ -305,6 +326,7 @@ export class Context<State> {
305326 props . Component = ( ) => child ;
306327
307328 const def = defs [ i ] ;
329+ setAdditionalStyles ( this , def . css ) ;
308330
309331 const result = await renderRouteComponent ( this , def , ( ) => child ) ;
310332 if ( result instanceof Response ) {
@@ -320,16 +342,20 @@ export class Context<State> {
320342
321343 let hasApp = true ;
322344
323- if ( isAsyncAnyComponent ( appDef ) ) {
345+ if ( appDef !== null ) {
346+ setAdditionalStyles ( this , appDef . css ) ;
347+ }
348+
349+ if ( appDef !== null && isAsyncAnyComponent ( appDef . component ) ) {
324350 props . Component = ( ) => appChild ;
325- const result = await renderAsyncAnyComponent ( appDef , props ) ;
351+ const result = await renderAsyncAnyComponent ( appDef . component , props ) ;
326352 if ( result instanceof Response ) {
327353 return result ;
328354 }
329355
330356 appVNode = result ;
331357 } else if ( appDef !== null ) {
332- appVNode = h ( appDef , {
358+ appVNode = h ( appDef . component , {
333359 Component : ( ) => appChild ,
334360 config : this . config ,
335361 data : null ,
0 commit comments