question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How to add ssr to a vue-cli 3 project?

See original GitHub issue

In all SSR tutorials with Vue that I found the webpack merge is used to create a configuration for the server. Could you explain quickly how I would do this in the design created by vue-cli 3? I am also using the PWA plugin.

My current code is:

factory.js

...
export default () => new Vue({
  router,
  store,
  render: h => h(App)
})

server.js

const express = require('express')
const { resolve } = require('path')
const fs = require('fs')

global.Vue = require('vue')
const layout = fs.readFileSync(resolve(__dirname, './dist/index.html'), 'utf8')
const renderer = require('vue-server-renderer').createRenderer()

const app = express()
app.use('/dist', express.static(
  resolve(__dirname, './dist')
))

app.get('/ssr', (req, res) => {
  renderer.renderToString(
    require(resolve(__dirname, './src/factory'))(),
    (err, html) => {
      if (err) {
        console.error(err)
        return res.status(500).send('Server error')
      }
      res.send(layout.replace('<div id="app"></div>', html))
    }
  )
})

app.get('*', (req, res) => {
  res.send(layout)
})

app.listen(process.env.PORT || 3000, () => console.log('Running :3000'))

I do not understand how to do it, as it should not be taking the factory.js from the raw code, but compiled.

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:1
  • Comments:6 (1 by maintainers)

github_iconTop GitHub Comments

28reactions
eddyerburghcommented, Sep 8, 2019

Here’s an example project that does SSR and uses vue-cli 3—https://github.com/lentoo/vue-cli-ssr-example

8reactions
ediaoscommented, Jan 27, 2019

Hi , I write a project for cli3-ssr-project example, see here

Read more comments on GitHub >

github_iconTop Results From Across the Web

Adding server-side-rendering to existing vue 3 project
In this article we will see how to add server side rendering support to existing vue 3 project.I will be using one of...
Read more >
Server-Side Rendering (SSR) - Vue.js
Let's take a look at the most bare-bones example of Vue SSR in action. Create a new directory and cd into it; Run...
Read more >
How to add ssr to a vue-cli 3 project without webpack?
Step 1: Create a Vue instance const Vue = require('vue') const app ... createRenderer() // Step 3: Render the Vue instance to HTML...
Read more >
Vue 3 Server Side Rendering (SSR)
A guide to building and running a server-side rendered Vue 3 application in a Node environment with routing, state, and meta.
Read more >
Server Side Rendering with Vue.js 3 - YouTube
This video shows how to create and render a Vue 3 application, using Vue CLI 4.5, Express, and Webpack, on a server.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found