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

Commit 1201e28

Browse files
committed
refactor(framework): improve style ssr
1 parent cc1063a commit 1201e28

File tree

3 files changed

+22
-25
lines changed

3 files changed

+22
-25
lines changed

framework/core/style.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
import util from '../../shared/util.ts'
22

3-
export const serverStyles: Map<string, string> = new Map()
4-
53
export function removeCSS(id: string) {
64
const { document } = window as any
75
Array.from(document.head.children).forEach((el: any) => {
@@ -13,7 +11,7 @@ export function removeCSS(id: string) {
1311

1412
export function applyCSS(id: string, css: string) {
1513
if (util.inDeno()) {
16-
serverStyles.set(id, css)
14+
return { id, css }
1715
} else {
1816
const { document } = window as any
1917
const ssrStyle = Array.from<any>(document.head.children).find((el: any) => {

framework/react/renderer.ts

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import { renderToString } from 'https://esm.sh/react-dom/server'
44
import util from '../../shared/util.ts'
55
import type { RenderResult, RouterURL } from '../../types.ts'
66
import events from '../core/events.ts'
7-
import { serverStyles } from "../core/style.ts"
87
import { RendererContext, RouterContext } from './context.ts'
98
import { AsyncUseDenoError, E400MissingComponent, E404Page } from './error.ts'
109
import { createPageProps } from './pageprops.ts'
@@ -15,7 +14,7 @@ export async function render(
1514
App: ComponentType<any> | undefined,
1615
E404: ComponentType | undefined,
1716
pageComponentChain: { url: string, Component?: any }[],
18-
styles?: { url: string, hash: string }[]
17+
styles: { url: string, hash: string }[]
1918
) {
2019
const global = globalThis as any
2120
const ret: Omit<RenderResult, 'url' | 'status'> = {
@@ -139,18 +138,18 @@ export async function render(
139138
})
140139
scriptsElements.clear()
141140

142-
// get inline-styles
143-
const rets = await Promise.all(styles?.filter(({ url }) => !url.startsWith('#inline-style-')).map(({ url, hash }) => {
144-
const path = util.isLikelyHttpURL(url) ? '/-/' + url.split('://')[1] : `${url}.${util.shortHash(hash)}`
145-
return import('file://' + util.cleanPath(`${Deno.cwd()}/.aleph/${buildMode}/${path}.js`))
146-
}) || [])
147-
rets.forEach(({ default: def }) => util.isFunction(def) && def())
148-
styles?.forEach(({ url }) => {
149-
if (serverStyles.has(url)) {
150-
const css = serverStyles.get(url)!
151-
ret.head.push(`<style type="text/css" data-module-id=${JSON.stringify(url)} ssr>${css}</style>`)
141+
// apply styles
142+
await Promise.all(styles.map(async ({ url, hash }) => {
143+
if (!url.startsWith('#inline-style-')) {
144+
const pathname = util.isLikelyHttpURL(url) ? '/-/' + url.split('://')[1] : `${url}.${util.shortHash(hash)}`
145+
const importUrl = 'file://' + util.cleanPath(`${Deno.cwd()}/.aleph/${buildMode}/${pathname}.js`)
146+
const { default: applyCSS } = await import(importUrl)
147+
if (util.isFunction(applyCSS)) {
148+
const { css } = applyCSS()
149+
ret.head.push(`<style type="text/css" data-module-id=${JSON.stringify(url)} ssr>${css}</style>`)
150+
}
152151
}
153-
})
152+
}))
154153

155154
defer()
156155
return ret

server/app.ts

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -864,7 +864,7 @@ export class Application {
864864
sourceCode = [
865865
`import { applyCSS } from "${alephModuleUrl}/framework/core/style.ts";`,
866866
`export default function __applyCSS() {`,
867-
` applyCSS(${JSON.stringify(url)}, ${JSON.stringify(css)});`,
867+
` return applyCSS(${JSON.stringify(url)}, ${JSON.stringify(css)});`,
868868
`}`,
869869
bundleMode && `__ALEPH.pack[${JSON.stringify(url)}] = { default: __applyCSS };`
870870
].filter(Boolean).join('\n')
@@ -1566,21 +1566,21 @@ export class Application {
15661566
}
15671567

15681568
/** lookup deps recurively. */
1569-
private lookupDeps(url: string, __deps: DependencyDescriptor[] = [], __tracing: Set<string> = new Set()) {
1569+
private lookupDeps(url: string, deps: DependencyDescriptor[] = [], tracing: Set<string> = new Set()) {
15701570
const mod = this.getModule(url)
15711571
if (!mod) {
1572-
return __deps
1572+
return deps
15731573
}
1574-
if (__tracing.has(url)) {
1575-
return __deps
1574+
if (tracing.has(url)) {
1575+
return deps
15761576
}
1577-
__tracing.add(url)
1578-
__deps.push(...mod.deps.filter(({ url }) => __deps.findIndex(i => i.url === url) === -1))
1577+
tracing.add(url)
1578+
deps.push(...mod.deps.filter(({ url }) => deps.findIndex(i => i.url === url) === -1))
15791579
mod.deps.forEach(({ url }) => {
15801580
if (isModuleURL(url) && !util.isLikelyHttpURL(url)) {
1581-
this.lookupDeps(url, __deps, __tracing)
1581+
this.lookupDeps(url, deps, tracing)
15821582
}
15831583
})
1584-
return __deps
1584+
return deps
15851585
}
15861586
}

0 commit comments

Comments
 (0)