+35
-3
lines changedFilter options
+35
-3
lines changed Original file line number Diff line number Diff line change
@@ -29,6 +29,7 @@ export type RenderOptions = {
29
29
shouldPreload?: Function;
30
30
shouldPrefetch?: Function;
31
31
clientManifest?: ClientManifest;
32
+
serializer?: Function;
32
33
runInNewContext?: boolean | 'once';
33
34
};
34
35
@@ -41,15 +42,17 @@ export function createRenderer ({
41
42
cache,
42
43
shouldPreload,
43
44
shouldPrefetch,
44
-
clientManifest
45
+
clientManifest,
46
+
serializer
45
47
}: RenderOptions = {}): Renderer {
46
48
const render = createRenderFunction(modules, directives, isUnaryTag, cache)
47
49
const templateRenderer = new TemplateRenderer({
48
50
template,
49
51
inject,
50
52
shouldPreload,
51
53
shouldPrefetch,
52
-
clientManifest
54
+
clientManifest,
55
+
serializer
53
56
})
54
57
55
58
return {
Original file line number Diff line number Diff line change
@@ -16,6 +16,7 @@ type TemplateRendererOptions = {
16
16
clientManifest?: ClientManifest;
17
17
shouldPreload?: (file: string, type: string) => boolean;
18
18
shouldPrefetch?: (file: string, type: string) => boolean;
19
+
serializer?: Function;
19
20
};
20
21
21
22
export type ClientManifest = {
@@ -47,6 +48,7 @@ export default class TemplateRenderer {
47
48
preloadFiles: Array<Resource>;
48
49
prefetchFiles: Array<Resource>;
49
50
mapFiles: AsyncFileMapper;
51
+
serialize: Function;
50
52
51
53
constructor (options: TemplateRendererOptions) {
52
54
this.options = options
@@ -57,6 +59,11 @@ export default class TemplateRenderer {
57
59
? parseTemplate(options.template)
58
60
: null
59
61
62
+
// function used to serialize initial state JSON
63
+
this.serialize = options.serializer || (state => {
64
+
return serialize(state, { isJSON: true })
65
+
})
66
+
60
67
// extra functionality with client manifest
61
68
if (options.clientManifest) {
62
69
const clientManifest = this.clientManifest = options.clientManifest
@@ -194,7 +201,7 @@ export default class TemplateRenderer {
194
201
contextKey = 'state',
195
202
windowKey = '__INITIAL_STATE__'
196
203
} = options || {}
197
-
const state = serialize(context[contextKey], { isJSON: true })
204
+
const state = this.serialize(context[contextKey])
198
205
const autoRemove = process.env.NODE_ENV === 'production'
199
206
? ';(function(){var s;(s=document.currentScript||document.scripts[document.scripts.length-1]).parentNode.removeChild(s);}());'
200
207
: ''
Original file line number Diff line number Diff line change
@@ -489,5 +489,27 @@ describe('SSR: template option', () => {
489
489
done()
490
490
})
491
491
})
492
+
493
+
it('renderToString + custom serializer', done => {
494
+
const expected = `{"foo":123}`
495
+
const renderer = createRenderer({
496
+
template: defaultTemplate,
497
+
serializer: () => expected
498
+
})
499
+
500
+
const context = {
501
+
state: { a: 1 }
502
+
}
503
+
504
+
renderer.renderToString(new Vue({
505
+
template: '<div>hi</div>'
506
+
}), context, (err, res) => {
507
+
expect(err).toBeNull()
508
+
expect(res).toContain(
509
+
`<script>window.__INITIAL_STATE__=${expected}</script>`
510
+
)
511
+
done()
512
+
})
513
+
})
492
514
}
493
515
})
You can’t perform that action at this time.
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4