Skip to content

Commit

Permalink
feat(ssr): add custom state serializer option
Browse files Browse the repository at this point in the history
close #6614
  • Loading branch information
yyx990803 committed Dec 20, 2018
1 parent 7ebabe2 commit 4494012
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 3 deletions.
7 changes: 5 additions & 2 deletions src/server/create-renderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ export type RenderOptions = {
shouldPreload?: Function;
shouldPrefetch?: Function;
clientManifest?: ClientManifest;
serializer?: Function;
runInNewContext?: boolean | 'once';
};

Expand All @@ -41,15 +42,17 @@ export function createRenderer ({
cache,
shouldPreload,
shouldPrefetch,
clientManifest
clientManifest,
serializer
}: RenderOptions = {}): Renderer {
const render = createRenderFunction(modules, directives, isUnaryTag, cache)
const templateRenderer = new TemplateRenderer({
template,
inject,
shouldPreload,
shouldPrefetch,
clientManifest
clientManifest,
serializer
})

return {
Expand Down
9 changes: 8 additions & 1 deletion src/server/template-renderer/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ type TemplateRendererOptions = {
clientManifest?: ClientManifest;
shouldPreload?: (file: string, type: string) => boolean;
shouldPrefetch?: (file: string, type: string) => boolean;
serializer?: Function;
};

export type ClientManifest = {
Expand Down Expand Up @@ -47,6 +48,7 @@ export default class TemplateRenderer {
preloadFiles: Array<Resource>;
prefetchFiles: Array<Resource>;
mapFiles: AsyncFileMapper;
serialize: Function;

constructor (options: TemplateRendererOptions) {
this.options = options
Expand All @@ -57,6 +59,11 @@ export default class TemplateRenderer {
? parseTemplate(options.template)
: null

// function used to serialize initial state JSON
this.serialize = options.serializer || (state => {
return serialize(state, { isJSON: true })
})

// extra functionality with client manifest
if (options.clientManifest) {
const clientManifest = this.clientManifest = options.clientManifest
Expand Down Expand Up @@ -194,7 +201,7 @@ export default class TemplateRenderer {
contextKey = 'state',
windowKey = '__INITIAL_STATE__'
} = options || {}
const state = serialize(context[contextKey], { isJSON: true })
const state = this.serialize(context[contextKey])
const autoRemove = process.env.NODE_ENV === 'production'
? ';(function(){var s;(s=document.currentScript||document.scripts[document.scripts.length-1]).parentNode.removeChild(s);}());'
: ''
Expand Down
22 changes: 22 additions & 0 deletions test/ssr/ssr-template.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -489,5 +489,27 @@ describe('SSR: template option', () => {
done()
})
})

it('renderToString + custom serializer', done => {
const expected = `{"foo":123}`
const renderer = createRenderer({
template: defaultTemplate,
serializer: () => expected
})

const context = {
state: { a: 1 }
}

renderer.renderToString(new Vue({
template: '<div>hi</div>'
}), context, (err, res) => {
expect(err).toBeNull()
expect(res).toContain(
`<script>window.__INITIAL_STATE__=${expected}</script>`
)
done()
})
})
}
})

0 comments on commit 4494012

Please sign in to comment.