|
1 |
| -import unescape from 'https://esm.sh/lodash/unescape?no-check' |
2 | 1 | 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' |
5 | 3 |
|
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() |
68 | 7 |
|
69 | 8 | export default function Head({ children }: PropsWithChildren<{}>) {
|
70 | 9 | if (window.Deno) {
|
|
0 commit comments