<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Georgy Perepecho on Medium]]></title>
        <description><![CDATA[Stories by Georgy Perepecho on Medium]]></description>
        <link>https://medium.com/@horprogs?source=rss-8e49886e479d------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/2*EvU1zRlsTQ6M_mISv7IxNg.jpeg</url>
            <title>Stories by Georgy Perepecho on Medium</title>
            <link>https://medium.com/@horprogs?source=rss-8e49886e479d------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Fri, 27 Nov 2026 21:26:16 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@horprogs/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[How I made it easy to develop on Vue.js with server-side rendering]]></title>
            <link>https://medium.com/js-dojo/how-i-made-it-easy-to-develop-on-vue-js-with-server-side-rendering-fdeebdd7e8d8?source=rss-8e49886e479d------2</link>
            <guid isPermaLink="false">https://medium.com/p/fdeebdd7e8d8</guid>
            <category><![CDATA[vuejs]]></category>
            <category><![CDATA[webpack]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[server-side-rendering]]></category>
            <category><![CDATA[nodejs]]></category>
            <dc:creator><![CDATA[Georgy Perepecho]]></dc:creator>
            <pubDate>Fri, 28 Jun 2019 19:48:09 GMT</pubDate>
            <atom:updated>2019-07-03T08:42:22.674Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*F9CZy2sm61uO2A3cWm35rg.jpeg" /></figure><p>Hey all!</p><p>I’ll start with a short story.</p><p>In my new project I decided to use Vue.js. I needed server-side rendering (SSR), CSS modules, code-splitting and more cool things. Of course, I needed hot module replacing (HMR) to fast development.</p><p>I didn’t want to use high-level frameworks, like Nuxt.js, because the ability to customization very important when project will be huge. But any high-level frameworks don’t allow it (I have similar experience with Next.js for React).</p><p>Main problem of local development with usage server-side rendering and hot reloading is that you aren’t enough run only <em>webpack-dev-server</em>. You must watch and recompile sources, that Node.js executes, otherwise server code won’t be updated, but client code will be.</p><p>Okey, I dived into the documentation and internet and unfortunately, I didn’t find worked examples and boilerplates. So, I created own.</p><p>I determined things, that must be included in my boilerplate to convenient development:</p><ul><li>VueJS</li><li>SSR</li><li>Vuex</li><li>CSS modules</li><li>Code-splitting</li><li>ESLint, Prettier</li></ul><p>In development mode, code must be updated in browser real-time, also server-side code must be updated too.</p><p>In production mode resources must be minified, names of resources must have hash to cache static, paths to resources must be injected to html-template automatically.</p><p>You can check the realisation in <a href="https://github.com/horprogs/vue-ssr-hmr">GitHub repo</a>, I’ll show you code and explain my decisions.</p><p>Note, that Vue.js has very deep <a href="https://ssr.vuejs.org/">documentation</a> about SSR setup. You should check it too.</p><h3>Server side</h3><p>So, for Node.js server we’ll use Express and we’ll need <em>vue-server-renderer</em>. This package allows us to render code to html-string, according to server bundle, html-template and client manifest, which contains names and paths to resources.</p><p>Result file <em>server.js</em> will be:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/0c3e64946172cb6abc6fbcc8848d6b2e/href">https://medium.com/media/0c3e64946172cb6abc6fbcc8848d6b2e/href</a></iframe><p>As you can see, we have 2 files: <em>vue-ssr-server-bundle.json</em> and <em>vue-ssr-client-manifest.json</em>. They are generating during build; the first file contains code, which will be executed on the server, the second contains names and paths to resources.</p><p>Also, options have a property <em>inject: false</em>. It means, that we’ll inject assets manually, because we want finer-grained control.</p><p>Template looks like:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/969dfc771528bd7ed6ba3a5a8164347a/href">https://medium.com/media/969dfc771528bd7ed6ba3a5a8164347a/href</a></iframe><ul><li><em>meta.inject().title.text()</em> and <em>meta.inject().meta.text()</em> needed for generation title and meta-description. It does package <em>vue-meta</em>, about that I’ll tell you later</li><li><em>renderResourceHints()</em> — it renders links <em>rel=”preload/prefetch”</em> to assets from client manifest</li><li><em>renderStyles() — </em>it renders links to styles from client manifest</li><li><em>renderState() — </em>it renders state to default variable window.__INITIAL_STATE__</li><li><em>renderScripts() — </em>it renders scripts for our application</li></ul><p>Comment &lt;! — vue-ssr-outlet — &gt;will be replaced with app html-code. It is required comment.</p><p>Entry point to our Vue app is file <em>entry-server.js</em>.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/b8f084bd92bb8fa046d173561860638a/href">https://medium.com/media/b8f084bd92bb8fa046d173561860638a/href</a></iframe><h3>Client side</h3><p>Client entry point is file <em>entry-client.js</em>.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/6df945006ff1c208d2090613b2dd3470/href">https://medium.com/media/6df945006ff1c208d2090613b2dd3470/href</a></iframe><p>Module <em>app.js</em> creates Vue instance, which using on client and server.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/b9db8b079e20e85f9fa93e8c5bb41f35/href">https://medium.com/media/b9db8b079e20e85f9fa93e8c5bb41f35/href</a></iframe><p><strong>We always create a new instance to avoid situation then several requests use one instance</strong></p><p><em>App.vue</em> — root component which contains tag <em>&lt;router-view&gt;&lt;/router-view&gt;</em>, which will inject components according to route.</p><p>Router looks like:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/1885f2fe8137338740e8c020b02af703/href">https://medium.com/media/1885f2fe8137338740e8c020b02af703/href</a></iframe><p>With <em>Vue.use</em> we connect two plugins <em>Router</em> and <em>VueMeta</em>.</p><p>In routes we specify components as</p><pre>() =&gt; <strong>import</strong>(&#39;./client/components/About.vue&#39;)</pre><p>It needs for code-splitting.</p><p>What about state management (with Vuex), settings have nothing really different. One thing, I divided store into modules and I use constants with names to ease code navigation.</p><p>Now, consider the nuances of Vue components.</p><p>Property <em>metaInfo</em> is responsible of meta rendering, using <em>vue-meta</em> package. We can specify a lot of properties (<a href="https://vue-meta.nuxtjs.org/api/#metainfo-properties">more</a>).</p><pre>metaInfo: {<br>  title: &#39;Main page&#39;,<br>}</pre><p>Components have method, which executes only on server side.</p><pre>serverPrefetch() {<br>  <strong><em>console</em></strong>.log(&#39;Run only on server&#39;);<br>}</pre><p>Also, I wanted to use CSS modules. I like the idea, that you don’t have to take care about class names to avoid intersection between components. If you use CSS modules, result class will look like <em>&lt;class name&gt;_&lt;hash&gt;</em></p><p>To do it, you should specify <em>style module</em> in the component.</p><pre>&lt;<strong>style </strong>module&gt;<br>.item {<br>  padding: 3<strong>px </strong>0;<br>}<br><br>.controls {<br>  margin-top: 12<strong>px</strong>;<br>}<br>&lt;/<strong>style</strong>&gt;</pre><p>And in the template you should specify attribute <em>:class.</em></p><pre>&lt;<strong>div </strong>:class=<strong>&quot;</strong>$style.item<strong>&quot;</strong>&gt;&lt;/<strong>div</strong>&gt;</pre><p>Also, in webpack config you should specify, that we’ll use modules.</p><h3>Bundle</h3><p>Consider the webpack configs.</p><p>We have the base config, which extended by server and clients configs.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/f106d79bd4203a5540b097a38b35c96f/href">https://medium.com/media/f106d79bd4203a5540b097a38b35c96f/href</a></iframe><p>Server config doesn’t have differences with <a href="https://ssr.vuejs.org/guide/build-config.html#server-config">documentation</a>. Except for CSS processing.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/bb4f544049c30c132b46b6c7a59fd816/href">https://medium.com/media/bb4f544049c30c132b46b6c7a59fd816/href</a></iframe><p>At first, all about CSS were in base config, because it needs for client and server. Production minification CSS was in base config too.</p><p>But I faced a problem, document appeared on server side and I got an error, of course. It was a <a href="https://github.com/webpack-contrib/mini-css-extract-plugin/issues/48#issuecomment-375288454">mini-css-extract-plugin problem</a> and was resolved by separation CSS building config for client and server.</p><p><em>VueSSRServerPlugin</em> generates file <em>vue-ssr-server-bundle.json</em>, which contains server execution code.</p><p>Now, consider client config.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/a6bba9fcdadeec20acbc0fb811208bf8/href">https://medium.com/media/a6bba9fcdadeec20acbc0fb811208bf8/href</a></iframe><p>For local development we specify <em>publicPath</em>, linked to <em>webpack-dev-server</em> and generate filename without hash. Also, for devServer we specify <em>writeToDisk:true</em> property.</p><p>An explanation is needed.</p><p>Default, <em>webpack-dev-server</em> serves assets from memory, instead writes it to disk. In this case we face a problem, client manifest (<em>vue-ssr-client-manifest.json</em>) which is on disk, will contain outdated assets, because it won’t be updated. To avoid it, dev-server must write changes to disk, then, client manifest will be updated and actual assets will be used.</p><p>Actually, I’d like to get rid of it. One of decisions — in development mode we can include manifest from dev-server url, instead of <em>/dist</em> folder. But it is async operation. I’ll be glad to receive clean solution of problem.</p><p>To reload server process we use <a href="https://github.com/remy/nodemon">Nodemon</a>, which watches for two files: <em>dist/vue-ssr-server-bundle.json</em> and <em>app/server.js</em> and it restarts app, then these files were changed.</p><p>To have the ability to restart app for <em>server.js</em> changes, we don’t specify this file as the entry point into <em>nodemon</em>. Instead this, we created file <em>nodemon.js</em> which includes <em>server.js</em>. Now, the entry point is a file <em>nodemon.js</em>.</p><p>In production mode the entry point is <em>app/server.js.</em></p><h3>Conclusions</h3><p>So, we have a repo with settings and several tasks.</p><p><strong>For local development:</strong></p><pre>yarn run dev</pre><p>Client side: it runs <em>webpack-dev-server</em>, which watches for changes of Vue components and js code and generates client manifest with assets paths to dev-server, saves it to disk and updates code, styles in browser in real time.</p><p>Server side: it runs <em>webpack</em> as a watcher, builds server bundle (<em>vue-ssr-server-bundle.json</em>) and restarts app, then it changed.</p><p>In this case, code will be updated consistently on the client and server automatically.</p><p>Note, If you run it the first time, you’ll get error, that server bundle wasn’t found.</p><p>It’s normal. Just re-run this task.</p><p><strong>For production build:</strong></p><pre>yarn run build</pre><p>Client side: it builds and minifies assets and adds hash to filenames and generates client manifest with relative paths to assets.</p><p>Server side: it builds server bundle.</p><p>Also, I created task <em>yarn run start-node</em>, which runs <em>server.js</em>, but it only for example. In production app you should use process manager, like PM2.</p><p>I hope that my experience helps you to setup ecosystem faster and concentrate on feature development.</p><h3>Useful links</h3><ul><li><a href="https://github.com/horprogs/vue-ssr-hmr">GitHub repo</a></li><li>D<a href="https://vue-ssr-hmr.herokuapp.com/">emo app</a></li><li><a href="https://ssr.vuejs.org/">Vue.js with SSR documentation</a></li><li><a href="https://vue-loader.vuejs.org/guide/css-modules.html">CSS modules setup documentation</a></li><li><a href="https://vuex.vuejs.org/">Vuex documentation</a></li><li><a href="https://vue-meta.nuxtjs.org/guide/">Vue Meta documentation</a></li><li><a href="https://github.com/vuejs/vue-hot-reload-api">Vue Hot Reload Api documentation</a></li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=fdeebdd7e8d8" width="1" height="1" alt=""><hr><p><a href="https://medium.com/js-dojo/how-i-made-it-easy-to-develop-on-vue-js-with-server-side-rendering-fdeebdd7e8d8">How I made it easy to develop on Vue.js with server-side rendering</a> was originally published in <a href="https://medium.com/js-dojo">Vue.js Developers</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>