Skip to content

Commit 651263b

Browse files
committed
even better ssr caching
1 parent 9344bd7 commit 651263b

File tree

3 files changed

+41
-29
lines changed

3 files changed

+41
-29
lines changed

src/server/create-renderer.js

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,12 @@ export function createRenderer ({
3636
): void {
3737
let result = ''
3838
let stackDepth = 0
39-
const write = (str: string, next: Function) => {
40-
result += str
39+
40+
const write = (text: string, next: Function) => {
41+
if (write.caching && text) {
42+
write.buffer += text
43+
}
44+
result += text
4145
if (stackDepth >= MAX_STACK_DEPTH) {
4246
process.nextTick(() => {
4347
try { next() } catch (e) {
@@ -50,6 +54,9 @@ export function createRenderer ({
5054
stackDepth--
5155
}
5256
}
57+
write.caching = false
58+
write.buffer = ''
59+
5360
try {
5461
render(component, write, () => {
5562
done(null, result)

src/server/render-stream.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,10 @@ export default class RenderStream extends stream.Readable {
2525
this.expectedSize = 0
2626
this.stackDepth = 0
2727

28-
this.write = (text: string, next: Function) => {
28+
const write = this.write = (text: string, next: Function) => {
29+
if (write.caching && text) {
30+
write.buffer += text
31+
}
2932
const n = this.expectedSize
3033
this.buffer += text
3134
if (this.buffer.length >= n) {
@@ -47,6 +50,8 @@ export default class RenderStream extends stream.Readable {
4750
}
4851
}
4952
}
53+
write.caching = false
54+
write.buffer = ''
5055

5156
this.end = () => {
5257
// the rendering is finished; we should push out the last of the buffer.

src/server/render.js

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,28 @@ export function createRenderFunction (
2525
isRoot: boolean
2626
) {
2727
if (node.componentOptions) {
28-
const child =
29-
getCachedComponent(node) ||
30-
createComponentInstanceForVnode(node)._render()
28+
// check cache hit
29+
const Ctor = node.componentOptions.Ctor
30+
const getKey = Ctor.options.server && Ctor.options.server.getCacheKey
31+
if (getKey) {
32+
const key = Ctor.cid + '::' + getKey(node.componentOptions.propsData)
33+
if (cache.has(key)) {
34+
return write(cache.get(key), next)
35+
} else {
36+
if (!write.caching) {
37+
// initialize if not already caching
38+
write.caching = true
39+
const _next = next
40+
next = () => {
41+
cache.set(key, write.buffer)
42+
write.caching = false
43+
write.buffer = ''
44+
_next()
45+
}
46+
}
47+
}
48+
}
49+
const child = createComponentInstanceForVnode(node)._render()
3150
child.parent = node
3251
renderNode(child, write, next, isRoot)
3352
} else {
@@ -39,21 +58,6 @@ export function createRenderFunction (
3958
}
4059
}
4160

42-
function getCachedComponent (node) {
43-
const Ctor = node.componentOptions.Ctor
44-
const getKey = Ctor.options.server && Ctor.options.server.getCacheKey
45-
if (getKey) {
46-
const key = Ctor.cid + '::' + getKey(node.componentOptions.propsData)
47-
if (cache.has(key)) {
48-
return cache.get(key)
49-
} else {
50-
const res = createComponentInstanceForVnode(node)._render()
51-
cache.set(key, res)
52-
return res
53-
}
54-
}
55-
}
56-
5761
function renderElement (
5862
el: VNode,
5963
write: Function,
@@ -94,9 +98,6 @@ export function createRenderFunction (
9498
}
9599

96100
function renderStartingTag (node: VNode) {
97-
if (node._rendered) {
98-
return node._rendered
99-
}
100101
let markup = `<${node.tag}`
101102
if (node.data) {
102103
// check directives
@@ -124,14 +125,13 @@ export function createRenderFunction (
124125
if (node.host && (scopeId = node.host.$options._scopeId)) {
125126
markup += ` ${scopeId}`
126127
}
127-
let _node = node
128-
while (_node) {
129-
if ((scopeId = _node.context.$options._scopeId)) {
128+
while (node) {
129+
if ((scopeId = node.context.$options._scopeId)) {
130130
markup += ` ${scopeId}`
131131
}
132-
_node = _node.parent
132+
node = node.parent
133133
}
134-
return (node._rendered = markup + '>')
134+
return markup + '>'
135135
}
136136

137137
return function render (

0 commit comments

Comments
 (0)