even better ssr caching

This commit is contained in:
Evan You 2016-06-26 12:25:19 -04:00
parent 9344bd7a52
commit 651263bb78
3 changed files with 41 additions and 29 deletions

View File

@ -36,8 +36,12 @@ export function createRenderer ({
): void {
let result = ''
let stackDepth = 0
const write = (str: string, next: Function) => {
result += str
const write = (text: string, next: Function) => {
if (write.caching && text) {
write.buffer += text
}
result += text
if (stackDepth >= MAX_STACK_DEPTH) {
process.nextTick(() => {
try { next() } catch (e) {
@ -50,6 +54,9 @@ export function createRenderer ({
stackDepth--
}
}
write.caching = false
write.buffer = ''
try {
render(component, write, () => {
done(null, result)

View File

@ -25,7 +25,10 @@ export default class RenderStream extends stream.Readable {
this.expectedSize = 0
this.stackDepth = 0
this.write = (text: string, next: Function) => {
const write = this.write = (text: string, next: Function) => {
if (write.caching && text) {
write.buffer += text
}
const n = this.expectedSize
this.buffer += text
if (this.buffer.length >= n) {
@ -47,6 +50,8 @@ export default class RenderStream extends stream.Readable {
}
}
}
write.caching = false
write.buffer = ''
this.end = () => {
// the rendering is finished; we should push out the last of the buffer.

View File

@ -25,9 +25,28 @@ export function createRenderFunction (
isRoot: boolean
) {
if (node.componentOptions) {
const child =
getCachedComponent(node) ||
createComponentInstanceForVnode(node)._render()
// check cache hit
const Ctor = node.componentOptions.Ctor
const getKey = Ctor.options.server && Ctor.options.server.getCacheKey
if (getKey) {
const key = Ctor.cid + '::' + getKey(node.componentOptions.propsData)
if (cache.has(key)) {
return write(cache.get(key), next)
} else {
if (!write.caching) {
// initialize if not already caching
write.caching = true
const _next = next
next = () => {
cache.set(key, write.buffer)
write.caching = false
write.buffer = ''
_next()
}
}
}
}
const child = createComponentInstanceForVnode(node)._render()
child.parent = node
renderNode(child, write, next, isRoot)
} else {
@ -39,21 +58,6 @@ export function createRenderFunction (
}
}
function getCachedComponent (node) {
const Ctor = node.componentOptions.Ctor
const getKey = Ctor.options.server && Ctor.options.server.getCacheKey
if (getKey) {
const key = Ctor.cid + '::' + getKey(node.componentOptions.propsData)
if (cache.has(key)) {
return cache.get(key)
} else {
const res = createComponentInstanceForVnode(node)._render()
cache.set(key, res)
return res
}
}
}
function renderElement (
el: VNode,
write: Function,
@ -94,9 +98,6 @@ export function createRenderFunction (
}
function renderStartingTag (node: VNode) {
if (node._rendered) {
return node._rendered
}
let markup = `<${node.tag}`
if (node.data) {
// check directives
@ -124,14 +125,13 @@ export function createRenderFunction (
if (node.host && (scopeId = node.host.$options._scopeId)) {
markup += ` ${scopeId}`
}
let _node = node
while (_node) {
if ((scopeId = _node.context.$options._scopeId)) {
while (node) {
if ((scopeId = node.context.$options._scopeId)) {
markup += ` ${scopeId}`
}
_node = _node.parent
node = node.parent
}
return (node._rendered = markup + '>')
return markup + '>'
}
return function render (