This article will continue on from my previous post, where we implemented basic server side rendering. Now we will add hydration. If the application relies on an external resource, for example data retreived from an external endpoint, the data needs to be fetched and resolved before we call renderer.renderToString
.
The source code is available here.
For this example, we will fetch a post from JSONPlaceholder. The data looks like this:
https://gist.github.com/e9db7f28bc98775c522c20f86b7bd42d
The strategy will go like this:
Client Side Rendering:
- in the App's
mounted
lifecycle hook,dispatch
a Vuexaction
commit
the response- render as usual
Server Side Rendering:
- check for a static
asyncData
function we will make - pass the store to
asyncData
, and calldispatch(action)
- commit the result
- now we have the required data, call
renderer.renderToString
We need some new modules. Namely:
axios
- a HTTP Client that works in a browser and node environmentvuex
- to store the data
Install them with:
https://gist.github.com/a01b4387914b15c0400fafda96a61391
Let's make a store, and get it working on the dev server first. Create a store by running touch src/store.js
. Inside it, add the following:
https://gist.github.com/936382925477a495151dacd393e1efac
Standard Vuex, nothing special, so I won't go into any detail.
We need to use the store now. Update create-app
:
https://gist.github.com/c9684e2503caaa2abfb0d9a618c44bf4
We are now returning { app, store, App }
. This is because we will need access to both App
and store
in src/server.js
later on.
If you run npm run dev
, and visit localhost:8080
, everything should still be working. Update src/Hello.vue
, to dispatch the action in mounted
, and retreive it using a computed
property:
https://gist.github.com/b56d945aa1e908471370dc8d27ee1b5d
localhost:8080
should now display the title
as well as Hello
.
Run npm run build && node src/server.js
, then visit localhost:8000
. You will notice Hello
is rendered, but the post.title
is not. This is because mounted
only runs in a browser. There are no dynamic updated when using SSR, only created
and beforeCreate
execute. See here for more information. We need another way to dispatch the action.
In Hello.vue
, add a asyncData
function. This is not part of Vue, just a regular JavaScript function.
https://gist.github.com/73d21b5c9ef1f04846c10d7d8646d4eb
We have to pass store
as an argument. This is because asyncData
is not part of Vue, so it doesn't have access to this
, so we cannot access the store - in fact, because we will call this function before calling renderer.renderToString
, this
doesn't even exist yet.
Now update src/server.js
to call asyncData
:
https://gist.github.com/4cc0c726a6455fc3326e765c286da078
Now we when render app
, store.state
should already contain post
! Let's try it out:
https://gist.github.com/ef3afe4ed6a90522ad53eb72cd39d989
Visting localhost:8000
causes a error to be shown in the terminal:
https://gist.github.com/ea188ba1b775c60e31565cc199e59d93
XMLHttpRequest
is Web API, and does not exist in a Node environment. But why is this happening? axios
is meant to work on both the client and server, right?
Let's take a look at axios
:
https://gist.github.com/24b1bd4fdd9dfb4858a760d7b92116d4
There is a bunch of stuff. The fields are interested in are browser
and main
:
https://gist.github.com/d173ff66a452bea821df4373690585f4
browser
is the source of the problem. See more about browser on npm. Basically, if there is a browser
field, and the target
of the webpack build is web
, it will use the browser
field instead of main
. Let's review our config/server.js
:
https://gist.github.com/1122000b48e2d665659b3ce3c31ba317
We did not specify target
. If we check the documentation here, we can see that the default value for target
is web. This means we are using the axios
build intended for the client instead of the Node.js build. Update config.server.js
:
https://gist.github.com/64f41d897d021f8955cd924237494fc0
Now run
https://gist.github.com/60cccf138b2533ecec888173a4dd96cd
and visit localhost:8000
. The title
is rendered! Compare it to localhost:8080
using the dev server - you can see that when we do the client side fetching, the title is blank briefly, until the request finished. Visiting localhost:8000
doesn't have this problem, since the data is fetched before the app is even rendered.
We saw how to write code that runs both on the server and client. This configuration is by no means robust and is not meant for use in a serious app, but illustrates how to set up different webpack configs for the client and server.
In this post we learned:
- about
package.json
, specifically thebrowser
property - webpack's
target
property - how to execute an ajax request on both the client and server
Many improvements remain:
- use Vue Router (both server and client side)
- more robust data fetching
- add some unit tests
The source code is available here.