-
-
Notifications
You must be signed in to change notification settings - Fork 33.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
* add types for vue-server-renderer * add a new line to the end of files * make RenderCallback's err to be nullable * fix the server side directive types * make stricter the types of the server bundle and the client manifest
- Loading branch information
Showing
4 changed files
with
150 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import Vue = require('vue'); | ||
import { Readable } from 'stream'; | ||
|
||
export declare function createRenderer(options?: RendererOptions): Renderer; | ||
|
||
export declare function createBundleRenderer(bundle: string | object, options?: BundleRendererOptions): BundleRenderer; | ||
|
||
type RenderCallback = (err: Error | null, html: string) => void; | ||
|
||
interface Renderer { | ||
renderToString(vm: Vue, callback: RenderCallback): void; | ||
renderToString(vm: Vue, context: object, callback: RenderCallback): void; | ||
|
||
renderToStream(vm: Vue, context?: object): Readable; | ||
} | ||
|
||
interface BundleRenderer { | ||
renderToString(callback: RenderCallback): void; | ||
renderToString(context: object, callback: RenderCallback): void; | ||
|
||
renderToStream(context?: object): Readable; | ||
} | ||
|
||
interface RendererOptions { | ||
template?: string; | ||
inject?: boolean; | ||
shouldPreload?: (file: string, type: string) => boolean; | ||
cache?: RenderCache; | ||
directives?: { | ||
[key: string]: (vnode: Vue.VNode, dir: Vue.VNodeDirective) => void | ||
}; | ||
} | ||
|
||
interface BundleRendererOptions extends RendererOptions { | ||
clientManifest?: object; | ||
runInNewContext?: boolean | 'once'; | ||
basedir?: string; | ||
} | ||
|
||
interface RenderCache { | ||
get: (key: string, cb?: (res: string) => void) => string | void; | ||
set: (key: string, val: string) => void; | ||
has?: (key: string, cb?: (hit: boolean) => void) => boolean | void; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
import Vue = require('vue'); | ||
import { readFileSync } from 'fs'; | ||
import { createRenderer, createBundleRenderer } from '../'; | ||
|
||
function createApp (context: any) { | ||
return new Vue({ | ||
data: { | ||
url: context.url | ||
}, | ||
template: `<div>The visited URL is: {{ url }}</div>` | ||
}); | ||
} | ||
|
||
// Renderer test | ||
const app = createApp({ url: 'http://localhost:8000/' }); | ||
|
||
const renderer = createRenderer({ | ||
template: readFileSync('./index.template.html', 'utf-8') | ||
}); | ||
|
||
const context = { | ||
title: 'Hello', | ||
meta: ` | ||
<meta name="description" content="Vue.js SSR Example"> | ||
` | ||
}; | ||
|
||
renderer.renderToString(app, context, (err, html) => { | ||
if (err) throw err; | ||
const res: string = html; | ||
}); | ||
|
||
renderer.renderToStream(app, context).on('data', chunk => { | ||
const html = chunk.toString(); | ||
}); | ||
|
||
// Bundle renderer test | ||
declare const cacheClient: { [key: string]: string }; | ||
|
||
const bundleRenderer = createBundleRenderer('/path/to/vue-ssr-server-bundle.json', { | ||
inject: false, | ||
runInNewContext: 'once', | ||
basedir: '/path/to/base', | ||
|
||
shouldPreload: (file, type) => { | ||
if (type === 'script' || type === 'style') { | ||
return true; | ||
} | ||
if (type === 'font') { | ||
return /\.woff2$/.test(file); | ||
} | ||
if (type === 'image') { | ||
return file === 'hero.jpg'; | ||
} | ||
return false; | ||
}, | ||
|
||
cache: { | ||
get: key => { | ||
return cacheClient[key]; | ||
}, | ||
set: (key, val) => { | ||
cacheClient[key] = val; | ||
}, | ||
has: key => { | ||
return !!cacheClient[key]; | ||
} | ||
}, | ||
|
||
directives: { | ||
example (vnode: Vue.VNode, directiveMeta: Vue.VNodeDirective) { | ||
// transform vnode based on directive binding metadata | ||
} | ||
} | ||
}); | ||
|
||
bundleRenderer.renderToString(context, (err, html) => { | ||
if (err) throw err; | ||
const res: string = html; | ||
}); | ||
|
||
bundleRenderer.renderToStream(context).on('data', chunk => { | ||
const html = chunk.toString(); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
{ | ||
"compilerOptions": { | ||
"target": "es5", | ||
"module": "commonjs", | ||
"moduleResolution": "node", | ||
"strict": true, | ||
"noEmit": true | ||
}, | ||
"compileOnSave": false, | ||
"include": [ | ||
"**/*.ts" | ||
] | ||
} |