Skip to content

Commit 82b02a0

Browse files
committed
progress
1 parent e7d26be commit 82b02a0

File tree

12 files changed

+117
-8
lines changed

12 files changed

+117
-8
lines changed

en/README.md

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,31 @@
1-
# vue-server-renderer
1+
# Vue.js Server-Side Rendering Guide
22

3-
Hello baby
3+
## What is SSR?
4+
5+
Vue.js is a framework for building client-side applications. By default, Vue components produce and manipulate DOM in the browser as output. However, it is also possible to render the same components into HTML strings on the server, send them directly to the browser, and finally "hydrate" the static markup into a fully interactive app on the client.
6+
7+
A server-rendered Vue.js app can also be considered "isomorphic" or "universal", in the sense that the majority of your app's code runs on both the server **and** the client.
8+
9+
## Why SSR?
10+
11+
Compared to a traditional SPA (Single-Page Application), the advantage of SSR primarily lies in:
12+
13+
- Better SEO, as the search engine crawlers will directly see the fully rendered page.
14+
15+
Note that as of now, Google and Bing can index synchronous JavaScript applications just fine. Synchronous being the key word there. If your app starts with a loading spinner, then fetches content via Ajax, the crawler will not wait for you to finish. This means if you have content fetched asynchronously on pages where SEO is important, SSR might be necessary.
16+
17+
- Faster time-to-content, especially on slow internet or slow devices. Server-rendered markup doesn't need to wait until all JavaScript has been downloaded and executed to be displayed, so your user will see a fully-rendered page sooner. This generally results in better user experience, and can be critical for applications where time-to-content is directly associated with conversion rate.
18+
19+
There are also some trade-offs to consider when using SSR:
20+
21+
- Development constraints. Browser-specific code can only be used inside certain lifecycle hooks; some external libraries may need special treatment to be able to run in a server-rendered app.
22+
23+
- More involved build setup and deployment requirements. Unlike a fully static SPA that can be deployed on any static file server, a server-rendered app requires an environment where a Node.js server can run.
24+
25+
- More server-side load. Rendering a full app in Node.js is obviously going to be more CPU-intensive than just serving static files, so if you expect high traffic, be prepared for corresponding server load and wisely employ caching strategies.
26+
27+
Before using SSR for your app, the first question you should ask it whether you actually need it. It mostly depends on how important time-to-content is for your app. For example, if you are building an internal dashboard where an extra few hundred milliseconds on initial load doesn't matter that much, SSR would be an overkill. However, in cases where time-to-content is absolutely critical, SSR can help you achieve the best possible initial load performance.
28+
29+
We realize it could be quite challenging to build a server-rendered Vue app if you are not already familiar with webpack, Node.js and Vue itself. If you prefer a higher-level solution that provides a smoother on-boarding experience, you should probably give [Nuxt.js](http://nuxtjs.org/) a try. It's built upon the same Vue stack but abstracts away a lot of the complexities, and provides some extra features such as static site generation. However, it may not suit your use case if you need more direct control of your app's structure. And it would still be beneficial to read through this guide to better understand how things work together.
30+
31+
If you believe your use case justifies the use of SSR, then let's dive in!

en/SUMMARY.md

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,14 @@
1-
# vue-server-renderer
2-
31
- [Basic Usage](basic.md)
4-
- [Why Use Bundle Renderer?](why-bundle-renderer.md)
2+
- [Writing Universal Code](universal.md)
3+
- [Using a Page Template](template.md)
4+
- [Using the Bundle Renderer](bundle-renderer.md)
55
- [Creating the Server Bundle](server-bundle.md)
66
- [Creating the Client Manifest](client-manifest.md)
7+
- [Routing](routing.md)
8+
- [Data Pre-fetching and State](data.md)
9+
- [Head Management](head.md)
10+
- [Handling CSS](css.md)
711
- [Caching](caching.md)
12+
- [Streaming](streaming.md)
813
- [Client Side Hydration](hydration.md)
914
- [API Reference](api.md)

en/basic.md

Lines changed: 51 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,53 @@
1-
## rendering a vm
1+
# Basic Usage
22

3-
## rendering to stream
3+
## Installation
44

5-
## template
5+
``` bash
6+
npm install vue-server-renderer --save
7+
```
8+
9+
#### Notes
10+
11+
- It's recommended to use Node.js version 6+.
12+
- `vue-server-renderer` and `vue` must have matching versions.
13+
- `vue-server-renderer` relies on some Node.js native modules and therefore can only be used in Node.js. We may provide a simpler build that can be run in other JavaScript runtimes in the future.
14+
15+
## Rendering a Vue Instance
16+
17+
``` js
18+
// Step 1: Create a Vue instance
19+
const Vue = require('vue')
20+
const app = new Vue({
21+
template: `<div>Hello World</div>`
22+
})
23+
24+
// Step 2: Create a renderer
25+
const renderer = require('vue-server-renderer').createRenderer()
26+
27+
// Step 3: Render the Vue instance to HTML
28+
renderer.renderToString(app, (err, html) => {
29+
if (err) throw err
30+
console.log(html)
31+
// => <p server-rendered="true">hello world</p>
32+
})
33+
```
34+
35+
It is pretty straightforward when used inside a Node.js server, for example [Express](https://expressjs.com/):
36+
37+
``` js
38+
app.get('*', (req, res) => {
39+
const app = new Vue({
40+
data: {
41+
url: req.url
42+
},
43+
template: `<div>Hello {{ url }}</div>`
44+
})
45+
46+
renderer.renderToString(app, (err, html) => {
47+
if (err) throw err
48+
res.end(html)
49+
})
50+
})
51+
```
52+
53+
This is the most basic API to render a Vue app on the server. However, this is far from sufficient for a real world server-rendered app. In the following chapters we will cover the common issues encountered and how to deal with them.

en/why-bundle-renderer.md renamed to en/bundle-renderer.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
1+
# Using the BundleRenderer
2+
13
## Problems with Basic SSR
24

5+
In our basic usage example, we directly required Vue and created an app instance in our Node.js server code. This is straightforward, however has quite a few issues in practice:
6+
37
- webpack
48
- development and deployment
59
- source map

en/code-splitting.md

Whitespace-only changes.

en/css.md

Whitespace-only changes.

en/data.md

Whitespace-only changes.

en/head.md

Whitespace-only changes.

en/routing.md

Whitespace-only changes.

en/streaming.md

Whitespace-only changes.

en/template.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Using a Page Template
2+
3+
When you render a Vue app, the renderer only generates the markup of the app. We also need to wrap the output with an extra HTML page shell.

en/universal.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Writing Universal Code
2+
3+
In a server-rendered app, most of our code will be "universal" - that is, it runs on both the server and the client. However, the two environments are quite different, so the behavior of our code will not be exactly the same. Here we will go over some differences that you need to be aware of.
4+
5+
## Data Reactivity on the Server
6+
7+
In a client-only app, every user will be using a fresh instance of the app in their browser. For server-side rendering we want the same: each request should have a fresh, isolated app instance so that there is no cross-request state pollution. Since each app is rendered only once, this means data reactivity is unnecessary on the server, so it is disabled by default. Disabling data reactivity also avoids the performance cost of converting data into reactive objects.
8+
9+
## Component Lifecycle Hooks
10+
11+
Since there are no dynamic updates, of all the lifecycle hooks, only `beforeCreate` and `created` will be called during SSR. This means any code inside other lifecycle hooks such as `beforeMount` or `mounted` will only be executed on the client.
12+
13+
## Access to Environment-specific APIs
14+
15+
Universal code cannot assume access to environment-specific APIs, so if your code directly uses browser-only globals like `window` or `document`, they will throw errors when executed in Node.js, and vice-versa.
16+
17+
For tasks shared between server and client but use different platform APIs, it's recommended to wrap the platform-specific implementations inside a universal API, or use libraries that do this for you. For example, [axios](https://github.com/mzabriskie/axios) is an HTTP client that exposes the same API for both server and client.
18+
19+
For browser-only APIs, the common approach is to lazily access them inside client-only lifecycle hooks.
20+
21+
Note that if a 3rd party library is not written with universal code in mind, it could be tricky to use it in an server-rendered app. You *might* be able to get it working by mocking some of the globals, but it would be hacky and may interfere with the environment detection code of other libraries.

0 commit comments

Comments
 (0)