Skip to content
This repository was archived by the owner on Jul 6, 2025. It is now read-only.

Commit 330e2ff

Browse files
author
Je
committed
refactor: improve server head render
1 parent 64ba039 commit 330e2ff

File tree

1 file changed

+9
-13
lines changed

1 file changed

+9
-13
lines changed

head.ts

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import React, { Children, createElement, isValidElement, PropsWithChildren, Reac
22
import type { AlephRuntime } from './types.ts'
33
import util, { hashShort } from './util.ts'
44

5-
const serverHeadElements: Array<{ type: string, props: Record<string, any> }> = []
5+
const serverHeadElements: Map<string, { type: string, props: Record<string, any> }> = new Map()
66
const serverStyles: Map<string, { css: string, asLink: boolean }> = new Map()
77

88
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
4242
}
4343
}
4444
})
45-
serverHeadElements.splice(0, serverHeadElements.length)
45+
serverHeadElements.clear()
4646
return tags
4747
}
4848

@@ -78,7 +78,7 @@ export function applyCSS(id: string, css: string, asLink: boolean = false) {
7878

7979
export function Head({ children }: PropsWithChildren<{}>) {
8080
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 }))
8282
}
8383

8484
useEffect(() => {
@@ -182,11 +182,7 @@ export function Viewport(props: ViewportProps) {
182182
)
183183
}
184184

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()) {
190186
Children.forEach(node, child => {
191187
if (!isValidElement(child)) {
192188
return
@@ -225,20 +221,20 @@ function parse(node: ReactNode, els?: Map<string, { type: string, props: Record<
225221
key += Object.keys(props).filter(k => !(/^content|children$/i.test(k))).map(k => `[${k.toLowerCase()}=${JSON.stringify(props[k])}]`).join('')
226222
}
227223
} else if (type !== 'title') {
228-
key += '-' + (els!.size + 1)
224+
key += '-' + (els.size + 1)
229225
}
230226
// remove the children prop of base/meta/link
231-
if (/^base|meta|link$/.test(type) && 'children' in props) {
227+
if (['base', 'meta', 'link'].includes(type) && 'children' in props) {
232228
const { children, ...rest } = props
233-
els!.set(key, { type, props: rest })
229+
els.set(key, { type, props: rest })
234230
} else {
235-
els!.set(key, { type, props })
231+
els.set(key, { type, props })
236232
}
237233
}
238234
break
239235
}
240236
})
241237

242-
return els!
238+
return els
243239
}
244240

0 commit comments

Comments
 (0)