Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[feature] allow template to be function in vue-server-renderer #9324

Merged
merged 9 commits into from
Jan 18, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 19 additions & 5 deletions src/server/create-renderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export type RenderOptions = {
directives?: Object;
isUnaryTag?: Function;
cache?: RenderCache;
template?: string;
template?: string | (content: string, context: any) => string;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wouldn't (context, content) be a better API, consistent with, for example, vuex actions, which have context as their first argument?

inject?: boolean;
basedir?: string;
shouldPreload?: Function;
Expand Down Expand Up @@ -82,14 +82,26 @@ export function createRenderer ({
}, cb)
try {
render(component, write, context, err => {
if (err) {
return cb(err)
}
if (context && context.rendered) {
context.rendered(context)
}
if (template) {
result = templateRenderer.renderSync(result, context)
}
if (err) {
cb(err)
try {
const res = templateRenderer.render(result, context)
if (typeof res !== 'string') {
// function template returning promise
res
.then(html => cb(null, html))
.catch(cb)
} else {
cb(null, res)
}
} catch (e) {
cb(e)
}
} else {
cb(null, result)
}
Expand Down Expand Up @@ -119,6 +131,8 @@ export function createRenderer ({
})
}
return renderStream
} else if (typeof template === 'function') {
throw new Error(`function template is only supported in renderToString.`)
} else {
const templateStream = templateRenderer.createStream(context)
renderStream.on('error', err => {
Expand Down
21 changes: 15 additions & 6 deletions src/server/template-renderer/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import type { ParsedTemplate } from './parse-template'
import type { AsyncFileMapper } from './create-async-file-mapper'

type TemplateRendererOptions = {
template: ?string;
template?: string | (content: string, context: any) => string;
inject?: boolean;
clientManifest?: ClientManifest;
shouldPreload?: (file: string, type: string) => boolean;
Expand Down Expand Up @@ -42,7 +42,7 @@ type Resource = {
export default class TemplateRenderer {
options: TemplateRendererOptions;
inject: boolean;
parsedTemplate: ParsedTemplate | null;
parsedTemplate: ParsedTemplate | Function | null;
publicPath: string;
clientManifest: ClientManifest;
preloadFiles: Array<Resource>;
Expand All @@ -55,8 +55,12 @@ export default class TemplateRenderer {
this.inject = options.inject !== false
// if no template option is provided, the renderer is created
// as a utility object for rendering assets like preload links and scripts.
this.parsedTemplate = options.template
? parseTemplate(options.template)

const { template } = options
this.parsedTemplate = template
? typeof template === 'string'
? parseTemplate(template)
: template
: null

// function used to serialize initial state JSON
Expand Down Expand Up @@ -89,12 +93,17 @@ export default class TemplateRenderer {
}

// render synchronously given rendered app content and render context
renderSync (content: string, context: ?Object) {
render (content: string, context: ?Object): string | Promise<string> {
const template = this.parsedTemplate
if (!template) {
throw new Error('renderSync cannot be called without a template.')
throw new Error('render cannot be called without a template.')
}
context = context || {}

if (typeof template === 'function') {
return template(content, context)
}

if (this.inject) {
return (
template.head(context) +
Expand Down