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

Commit 306b77b

Browse files
committed
refactor: move cache to renderer module
1 parent 3bce4c7 commit 306b77b

File tree

2 files changed

+47
-33
lines changed

2 files changed

+47
-33
lines changed

server/app.ts

Lines changed: 12 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,6 @@ export class Application implements ServerApplication {
6868
#fsWatchListeners: Array<EventEmitter> = []
6969
#bundler: Bundler = new Bundler(this)
7070
#renderer: Renderer = new Renderer(this)
71-
#renderCache: Map<string, Map<string, [string, any]>> = new Map()
7271
#injects: Map<'compilation' | 'hmr' | 'ssr', TransformFn[]> = new Map()
7372
#reloading = false
7473

@@ -270,9 +269,9 @@ export class Application implements ServerApplication {
270269
const hmrable = this.isHMRable(mod.url)
271270
const update = ({ url }: Module) => {
272271
if (trimModuleExt(url) === '/app') {
273-
this.#renderCache.clear()
272+
this.#renderer.clearCache()
274273
} else if (url.startsWith('/pages/')) {
275-
this.#renderCache.delete(toPagePath(url))
274+
this.#renderer.clearCache(url)
276275
this.#pageRouting.update(this.createRouteModule(url))
277276
} else if (url.startsWith('/api/')) {
278277
this.#apiRouting.update(this.createRouteModule(url))
@@ -297,9 +296,9 @@ export class Application implements ServerApplication {
297296
})
298297
} else if (this.#modules.has(url)) {
299298
if (trimModuleExt(url) === '/app') {
300-
this.#renderCache.clear()
299+
this.#renderer.clearCache()
301300
} else if (url.startsWith('/pages/')) {
302-
this.#renderCache.delete(toPagePath(url))
301+
this.#renderer.clearCache(toPagePath(url))
303302
this.#pageRouting.removeRoute(toPagePath(url))
304303
} else if (url.startsWith('/api/')) {
305304
this.#apiRouting.removeRoute(toPagePath(url))
@@ -426,7 +425,7 @@ export class Application implements ServerApplication {
426425
}
427426

428427
const cacheKey = router.pathname + router.query.toString()
429-
const ret = await this.useRenderCache(pagePath, cacheKey, async () => {
428+
const ret = await this.#renderer.useCache(pagePath, cacheKey, async () => {
430429
return await this.#renderer.renderPage(router, nestedModules)
431430
})
432431
return ret[1]
@@ -440,26 +439,30 @@ export class Application implements ServerApplication {
440439
const path = router.pathname + router.query.toString()
441440

442441
if (!this.isSSRable(loc.pathname)) {
443-
const [html] = await this.useRenderCache('-', 'spa-index', async () => {
442+
const [html] = await this.#renderer.useCache('-', 'spa-index', async () => {
444443
return [await this.#renderer.renderSPAIndexPage(), null]
445444
})
446445
return [status, html]
447446
}
448447

449448
if (pagePath === '') {
450-
const [html] = await this.useRenderCache('404', path, async () => {
449+
const [html] = await this.#renderer.useCache('404', path, async () => {
451450
return [await this.#renderer.render404Page(router), null]
452451
})
453452
return [status, html]
454453
}
455454

456-
const [html] = await this.useRenderCache(pagePath, path, async () => {
455+
const [html] = await this.#renderer.useCache(pagePath, path, async () => {
457456
let [html, data] = await this.#renderer.renderPage(router, nestedModules)
458457
return [html, data]
459458
})
460459
return [status, html]
461460
}
462461

462+
getCodeInjects(phase: 'compilation' | 'hmr' | 'ssr') {
463+
return this.#injects.get(phase)
464+
}
465+
463466
createFSWatcher(): EventEmitter {
464467
const e = new EventEmitter()
465468
this.#fsWatchListeners.push(e)
@@ -1204,30 +1207,6 @@ export class Application implements ServerApplication {
12041207
}
12051208
}
12061209

1207-
private async useRenderCache(
1208-
namespace: string,
1209-
key: string,
1210-
render: () => Promise<[string, any]>
1211-
): Promise<[string, any]> {
1212-
let cache = this.#renderCache.get(namespace)
1213-
if (cache === undefined) {
1214-
cache = new Map()
1215-
this.#renderCache.set(namespace, cache)
1216-
}
1217-
const cached = cache.get(key)
1218-
if (cached !== undefined) {
1219-
return cached
1220-
}
1221-
const ret = await render()
1222-
if (namespace !== '-') {
1223-
this.#injects.get('ssr')?.forEach(transform => {
1224-
ret[0] = transform(key, ret[0])
1225-
})
1226-
}
1227-
cache.set(key, ret)
1228-
return ret
1229-
}
1230-
12311210
/** check a page whether is able to SSR. */
12321211
private isSSRable(pathname: string): boolean {
12331212
const { ssr } = this.config

server/ssr.ts

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { toPagePath } from '../framework/core/module.ts'
12
import { createBlankRouterURL, RouteModule } from '../framework/core/routing.ts'
23
import log from '../shared/log.ts'
34
import util from '../shared/util.ts'
@@ -25,16 +26,50 @@ export type FrameworkRenderer = {
2526
export class Renderer {
2627
#app: Application
2728
#renderer: FrameworkRenderer
29+
#cache: Map<string, Map<string, [string, any]>>
2830

2931
constructor(app: Application) {
3032
this.#app = app
3133
this.#renderer = { render: async () => { throw new Error("framework renderer is undefined") } }
34+
this.#cache = new Map()
3235
}
3336

3437
setFrameworkRenderer(renderer: FrameworkRenderer) {
3538
this.#renderer = renderer
3639
}
3740

41+
async useCache(
42+
namespace: string,
43+
key: string,
44+
render: () => Promise<[string, any]>
45+
): Promise<[string, any]> {
46+
let cache = this.#cache.get(namespace)
47+
if (cache === undefined) {
48+
cache = new Map()
49+
this.#cache.set(namespace, cache)
50+
}
51+
const cached = cache.get(key)
52+
if (cached !== undefined) {
53+
return cached
54+
}
55+
const ret = await render()
56+
if (namespace !== '-') {
57+
this.#app.getCodeInjects('ssr')?.forEach(transform => {
58+
ret[0] = transform(key, ret[0])
59+
})
60+
}
61+
cache.set(key, ret)
62+
return ret
63+
}
64+
65+
clearCache(url?: string) {
66+
if (url) {
67+
this.#cache.delete(toPagePath(url))
68+
} else {
69+
this.#cache.clear()
70+
}
71+
}
72+
3873
/** render page base the given location. */
3974
async renderPage(url: RouterURL, nestedModules: RouteModule[]): Promise<[string, any]> {
4075
const start = performance.now()

0 commit comments

Comments
 (0)