Server Side Hydration — SSR with Vue and webpack from scratch (3/3)
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:
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
Setup
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:
Create the store
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:
Standard Vuex, nothing special, so I won’t go into any detail.
We need to use the store now. Update create-app
:
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:
computed: {
post() {
return this.$store.state.post
}
}
localhost:8080
should now display the title
as well as Hello
.
Fetching the resources on the server
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.
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
:
Now we when render app
, store.state
should already contain post
! Let's try it out:
Visting localhost:8000
causes a error to be shown in the terminal:
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
:
There is a bunch of stuff. The fields are interested in are browser
and main
:
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
:
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
:
Now run
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.
Conclusion
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
Improvements
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.
Originally published at Lachlan’s blog.