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

Commit 60783f4

Browse files
author
Je
committed
refactor: move renderHead and renderScripts functions to renderer.ts
1 parent b5a95d2 commit 60783f4

File tree

2 files changed

+65
-68
lines changed

2 files changed

+65
-68
lines changed

head.ts

Lines changed: 4 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -1,70 +1,9 @@
1-
import unescape from 'https://esm.sh/lodash/unescape?no-check'
21
import React, { Children, createElement, isValidElement, PropsWithChildren, ReactElement, ReactNode, useEffect } from 'https://esm.sh/react'
3-
import type { AlephEnv } from './types.ts'
4-
import util, { hashShort } from './util.ts'
2+
import util from './util.ts'
53

6-
const serverHeadElements: Map<string, { type: string, props: Record<string, any> }> = new Map()
7-
const serverScriptsElements: Map<string, { type: string, props: Record<string, any> }> = new Map()
8-
const serverStyles: Map<string, { css: string, asLink: boolean }> = new Map()
9-
10-
export async function renderHead(styles?: { url: string, hash: string, async?: boolean }[]) {
11-
const { __buildMode, __buildTarget } = (window as any).ALEPH.ENV as AlephEnv
12-
const tags: string[] = []
13-
serverHeadElements.forEach(({ type, props }) => {
14-
if (type === 'title') {
15-
if (util.isNEString(props.children)) {
16-
tags.push(`<title ssr>${props.children}</title>`)
17-
} else if (util.isNEArray(props.children)) {
18-
tags.push(`<title ssr>${props.children.join('')}</title>`)
19-
}
20-
} else {
21-
const attrs = Object.keys(props)
22-
.filter(key => key !== 'children')
23-
.map(key => ` ${key}=${JSON.stringify(props[key])}`)
24-
.join('')
25-
if (util.isNEString(props.children)) {
26-
tags.push(`<${type}${attrs} ssr>${props.children}</${type}>`)
27-
} else if (util.isNEArray(props.children)) {
28-
tags.push(`<${type}${attrs} ssr>${props.children.join('')}</${type}>`)
29-
} else {
30-
tags.push(`<${type}${attrs} ssr />`)
31-
}
32-
}
33-
})
34-
await Promise.all(styles?.filter(({ async }) => !!async).map(({ url, hash }) => {
35-
return import('file://' + util.cleanPath(`${Deno.cwd()}/.aleph/${__buildMode}.${__buildTarget}/${url}.${hash.slice(0, hashShort)}.js`))
36-
}) || [])
37-
styles?.forEach(({ url }) => {
38-
if (serverStyles.has(url)) {
39-
const { css, asLink } = serverStyles.get(url)!
40-
if (asLink) {
41-
tags.push(`<link rel="stylesheet" href="${css}" data-module-id=${JSON.stringify(url)} />`)
42-
} else {
43-
tags.push(`<style type="text/css" data-module-id=${JSON.stringify(url)}>${css}</style>`)
44-
}
45-
}
46-
})
47-
serverHeadElements.clear()
48-
return tags
49-
}
50-
51-
export function renderScripts() {
52-
const scripts: Record<string, any>[] = []
53-
serverScriptsElements.forEach(({ props }) => {
54-
const { children, dangerouslySetInnerHTML, ...attrs } = props
55-
if (dangerouslySetInnerHTML && util.isNEString(dangerouslySetInnerHTML.__html)) {
56-
scripts.push({ ...attrs, innerText: dangerouslySetInnerHTML.__html })
57-
} if (util.isNEString(children)) {
58-
scripts.push({ ...attrs, innerText: unescape(children) })
59-
} else if (util.isNEArray(children)) {
60-
scripts.push({ ...attrs, innerText: unescape(children.join('')) })
61-
} else {
62-
scripts.push(props)
63-
}
64-
})
65-
serverScriptsElements.clear()
66-
return scripts
67-
}
4+
export const serverHeadElements: Map<string, { type: string, props: Record<string, any> }> = new Map()
5+
export const serverScriptsElements: Map<string, { type: string, props: Record<string, any> }> = new Map()
6+
export const serverStyles: Map<string, { css: string, asLink: boolean }> = new Map()
687

698
export default function Head({ children }: PropsWithChildren<{}>) {
709
if (window.Deno) {

renderer.ts

Lines changed: 61 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,69 @@ import { renderToString } from 'https://esm.sh/react-dom/server'
33
import { RouterContext } from './context.ts'
44
import { AsyncUseDenoError, E400MissingDefaultExportAsComponent, E404Page } from './error.ts'
55
import events from './events.ts'
6+
import { serverHeadElements, serverScriptsElements, serverStyles } from './head.ts'
67
import { createPageProps } from './routing.ts'
7-
import type { RouterURL } from './types.ts'
8-
import util from './util.ts'
8+
import type { AlephEnv, RouterURL } from './types.ts'
9+
import util, { hashShort } from './util.ts'
910

10-
export { renderHead, renderScripts } from './head.ts'
11+
export async function renderHead(styles?: { url: string, hash: string, async?: boolean }[]) {
12+
const { __buildMode, __buildTarget } = (window as any).ALEPH.ENV as AlephEnv
13+
const tags: string[] = []
14+
serverHeadElements.forEach(({ type, props }) => {
15+
if (type === 'title') {
16+
if (util.isNEString(props.children)) {
17+
tags.push(`<title ssr>${props.children}</title>`)
18+
} else if (util.isNEArray(props.children)) {
19+
tags.push(`<title ssr>${props.children.join('')}</title>`)
20+
}
21+
} else {
22+
const attrs = Object.keys(props)
23+
.filter(key => key !== 'children')
24+
.map(key => ` ${key}=${JSON.stringify(props[key])}`)
25+
.join('')
26+
if (util.isNEString(props.children)) {
27+
tags.push(`<${type}${attrs} ssr>${props.children}</${type}>`)
28+
} else if (util.isNEArray(props.children)) {
29+
tags.push(`<${type}${attrs} ssr>${props.children.join('')}</${type}>`)
30+
} else {
31+
tags.push(`<${type}${attrs} ssr />`)
32+
}
33+
}
34+
})
35+
await Promise.all(styles?.filter(({ async }) => !!async).map(({ url, hash }) => {
36+
return import('file://' + util.cleanPath(`${Deno.cwd()}/.aleph/${__buildMode}.${__buildTarget}/${url}.${hash.slice(0, hashShort)}.js`))
37+
}) || [])
38+
styles?.forEach(({ url }) => {
39+
if (serverStyles.has(url)) {
40+
const { css, asLink } = serverStyles.get(url)!
41+
if (asLink) {
42+
tags.push(`<link rel="stylesheet" href="${css}" data-module-id=${JSON.stringify(url)} />`)
43+
} else {
44+
tags.push(`<style type="text/css" data-module-id=${JSON.stringify(url)}>${css}</style>`)
45+
}
46+
}
47+
})
48+
serverHeadElements.clear()
49+
return tags
50+
}
51+
52+
export function renderScripts() {
53+
const scripts: Record<string, any>[] = []
54+
serverScriptsElements.forEach(({ props }) => {
55+
const { children, dangerouslySetInnerHTML, ...attrs } = props
56+
if (dangerouslySetInnerHTML && util.isNEString(dangerouslySetInnerHTML.__html)) {
57+
scripts.push({ ...attrs, innerText: dangerouslySetInnerHTML.__html })
58+
} if (util.isNEString(children)) {
59+
scripts.push({ ...attrs, innerText: unescape(children) })
60+
} else if (util.isNEArray(children)) {
61+
scripts.push({ ...attrs, innerText: unescape(children.join('')) })
62+
} else {
63+
scripts.push(props)
64+
}
65+
})
66+
serverScriptsElements.clear()
67+
return scripts
68+
}
1169

1270
export async function renderPage(
1371
url: RouterURL,

0 commit comments

Comments
 (0)