@@ -2,7 +2,7 @@ import React, { Children, createElement, isValidElement, PropsWithChildren, Reac
2
2
import type { AlephRuntime } from './types.ts'
3
3
import util , { hashShort } from './util.ts'
4
4
5
- const serverHeadElements : Array < { type : string , props : Record < string , any > } > = [ ]
5
+ const serverHeadElements : Map < string , { type : string , props : Record < string , any > } > = new Map ( )
6
6
const serverStyles : Map < string , { css : string , asLink : boolean } > = new Map ( )
7
7
8
8
export async function renderHead ( styles ?: { url : string , hash : string , async ?: boolean } [ ] ) {
@@ -42,7 +42,7 @@ export async function renderHead(styles?: { url: string, hash: string, async?: b
42
42
}
43
43
}
44
44
} )
45
- serverHeadElements . splice ( 0 , serverHeadElements . length )
45
+ serverHeadElements . clear ( )
46
46
return tags
47
47
}
48
48
@@ -78,7 +78,7 @@ export function applyCSS(id: string, css: string, asLink: boolean = false) {
78
78
79
79
export function Head ( { children } : PropsWithChildren < { } > ) {
80
80
if ( window . Deno ) {
81
- parse ( children ) . forEach ( ( { type, props } ) => serverHeadElements . push ( { type, props } ) )
81
+ parse ( children ) . forEach ( ( { type, props } , key ) => serverHeadElements . set ( key , { type, props } ) )
82
82
}
83
83
84
84
useEffect ( ( ) => {
@@ -182,11 +182,7 @@ export function Viewport(props: ViewportProps) {
182
182
)
183
183
}
184
184
185
- function parse ( node : ReactNode , els ?: Map < string , { type : string , props : Record < string , any > } > ) {
186
- if ( els === undefined ) {
187
- els = new Map ( )
188
- }
189
-
185
+ function parse ( node : ReactNode , els : Map < string , { type : string , props : Record < string , any > } > = new Map ( ) ) {
190
186
Children . forEach ( node , child => {
191
187
if ( ! isValidElement ( child ) ) {
192
188
return
@@ -225,20 +221,20 @@ function parse(node: ReactNode, els?: Map<string, { type: string, props: Record<
225
221
key += Object . keys ( props ) . filter ( k => ! ( / ^ c o n t e n t | c h i l d r e n $ / i. test ( k ) ) ) . map ( k => `[${ k . toLowerCase ( ) } =${ JSON . stringify ( props [ k ] ) } ]` ) . join ( '' )
226
222
}
227
223
} else if ( type !== 'title' ) {
228
- key += '-' + ( els ! . size + 1 )
224
+ key += '-' + ( els . size + 1 )
229
225
}
230
226
// remove the children prop of base/meta/link
231
- if ( / ^ b a s e | m e t a | l i n k $ / . test ( type ) && 'children' in props ) {
227
+ if ( [ ' base' , ' meta' , ' link' ] . includes ( type ) && 'children' in props ) {
232
228
const { children, ...rest } = props
233
- els ! . set ( key , { type, props : rest } )
229
+ els . set ( key , { type, props : rest } )
234
230
} else {
235
- els ! . set ( key , { type, props } )
231
+ els . set ( key , { type, props } )
236
232
}
237
233
}
238
234
break
239
235
}
240
236
} )
241
237
242
- return els !
238
+ return els
243
239
}
244
240
0 commit comments