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.

Error: Build failing using prerender-spa-plugin

See original GitHub issue

Build fails when I attempt to npm run build when using prerender-spa-plugin. It looks like I’m facing pretty much the same issue described in #282.

I’ve also tried adding data-server-rendered="true" attribute to the root app element as suggested, but this did no good.

I’m on Windows 10 and my project setting is:

The error mesage from npm run build command:

ERROR Build failed with errors. npm ERR! code ELIFECYCLE npm ERR! errno 1 npm ERR! a@0.1.0 build: vue-cli-service build npm ERR! Exit status 1 npm ERR! npm ERR! Failed at the a@0.1.0 build script. npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

My vue.config.js:

var path = require('path')
const PrerenderSPAPlugin = require('prerender-spa-plugin')

module.exports = {
  configureWebpack: config => {
    if (process.env.NODE_ENV === 'production') {
      config.plugins = [
        new PrerenderSPAPlugin({
          staticDir: path.join(__dirname, 'dist'),
          routes: ['/', '/about'],
          renderer: new Renderer({
            renderAfterElementExists: '#app',
            renderAfterTime: 5000
          })
        })
      ]
    }
  }
}

My package.json:

{
  "name": "a",
  "version": "0.1.0",
  "private": true,
  "scripts": {
    "serve": "vue-cli-service serve",
    "build": "vue-cli-service build",
    "lint": "vue-cli-service lint"
  },
  "dependencies": {
    "@vue/eslint-config-prettier": "^4.0.1",
    "axios": "^0.18.0",
    "file-saver": "^2.0.1",
    "form-data": "^2.3.3",
    "js-yaml": "^3.12.1",
    "jsdom": "^13.2.0",
    "markdown-it": "^8.4.2",
    "markdown-it-anchor": "^5.0.2",
    "markdown-it-footnote": "^3.0.1",
    "nprogress": "^0.2.0",
    "prerender-spa-plugin": "^3.4.0",
    "turndown": "^5.0.3",
    "turndown-plugin-gfm": "^1.0.2",
    "vue": "^2.6.9",
    "vue-router": "^3.0.2",
    "vue-social-sharing": "^2.4.2",
    "vuetify": "^1.5.2",
    "vuex": "^3.1.0"
  },
  "devDependencies": {
    "@vue/cli-plugin-babel": "^3.4.1",
    "@vue/cli-plugin-eslint": "^3.4.1",
    "@vue/cli-service": "^3.4.1",
    "babel-eslint": "^10.0.1",
    "eslint": "^5.14.1",
    "eslint-plugin-vue": "^5.2.2",
    "vue-cli-plugin-vuetify": "^0.4.6",
    "vue-template-compiler": "^2.6.9"
  },
  "eslintConfig": {
    "root": true,
    "env": {
      "node": true
    },
    "extends": [
      "plugin:vue/essential",
      "eslint:recommended"
    ],
    "rules": {},
    "parserOptions": {
      "parser": "babel-eslint"
    }
  },
  "postcss": {
    "plugins": {
      "autoprefixer": {}
    }
  },
  "browserslist": [
    "> 1%",
    "last 2 versions",
    "not ie <= 8"
  ]
}

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:9

github_iconTop GitHub Comments

1reaction
SageSanyuecommented, Mar 29, 2021

headless: true It helps me! Reference: https://juejin.cn/post/6844904178326700045#comment “在mac下设置了 headless: false 导致提示 Unable to prerender all routes! google 了一堆,告诉我说不能使用路由懒加载,其实造成这个报错的原因有很多,我是设置了headless: true 就好了 !”

1reaction
bobaekangcommented, Mar 21, 2019

Closing this issue because of the successful prerendering of the original app. Using the code in maoberlenher/how-to-pre-render-vue-powered-websites-with-webpack helped.

New project setting:

Revised vue.config.js:

const path = require('path')
const PrerenderSPAPlugin = require('prerender-spa-plugin')

const productionPlugins = [
  new PrerenderSPAPlugin({
    staticDir: path.join(__dirname, `dist`),
    routes: [`/`, `/about`],
    postProcess(renderedRoute) {
      renderedRoute.html = renderedRoute.html
        .replace(/<script (.*?)>/g, `<script $1 defer>`)
        .replace(`id="app"`, `id="app" data-server-rendered="true"`)

      return renderedRoute
    },
    renderer: new PrerenderSPAPlugin.PuppeteerRenderer({
      inject: {},
      renderAfterElementExists: `[data-view]`
    })
  })
]

module.exports = {
  lintOnSave: false,
  configureWebpack: config => {
    if (process.env.NODE_ENV === `production`) {
      config.plugins.push(...productionPlugins)
    }
  }
}

Revised package.json:

{
  "name": "a",
  "version": "0.1.0",
  "private": true,
  "scripts": {
    "serve": "vue-cli-service serve",
    "build": "vue-cli-service build",
    "lint": "vue-cli-service lint"
  },
  "dependencies": {
    "@vue/eslint-config-prettier": "^4.0.1",
    "axios": "^0.18.0",
    "file-saver": "^2.0.1",
    "form-data": "^2.3.3",
    "js-yaml": "^3.12.1",
    "jsdom": "^13.2.0",
    "markdown-it": "^8.4.2",
    "markdown-it-anchor": "^5.0.2",
    "markdown-it-footnote": "^3.0.1",
    "nprogress": "^0.2.0",
    "prerender-spa-plugin": "^3.4.0",
    "turndown": "^5.0.3",
    "turndown-plugin-gfm": "^1.0.2",
    "vue": "^2.6.10",
    "vue-router": "^3.0.2",
    "vue-social-sharing": "^2.4.2",
    "vuetify": "^1.5.7",
    "vuex": "^3.1.0"
  },
  "devDependencies": {
    "@vue/cli-plugin-babel": "^3.4.1",
    "@vue/cli-plugin-eslint": "^3.4.1",
    "@vue/cli-service": "^3.4.1",
    "babel-eslint": "^10.0.1",
    "eslint": "^5.15.3",
    "eslint-plugin-vue": "^5.2.2",
    "vue-cli-plugin-vuetify": "^0.4.6",
    "vue-template-compiler": "^2.6.10"
  },
  "eslintConfig": {
    "root": true,
    "env": {
      "node": true
    },
    "extends": [
      "plugin:vue/essential",
      "eslint:recommended"
    ],
    "rules": {},
    "parserOptions": {
      "parser": "babel-eslint"
    }
  },
  "postcss": {
    "plugins": {
      "autoprefixer": {}
    }
  },
  "browserslist": [
    "> 1%",
    "last 2 versions",
    "not ie <= 8"
  ]
}

Revised main.js:

import Vue from 'vue'
import Vuetify from 'vuetify'
import 'vuetify/dist/vuetify.min.css'
// importing stuff...

import App from './App.vue'
import router from './router'
import store from './store/index'

// from maoberlenher/how-to-pre-render-vue-powered-websites-with-webpack
import injectInitialState from './services/inject-initial-state' 

Vue.config.productionTip = false
Vue.use(Vuetify)
// using stuff...

const app = new Vue({
  router,
  store,
  render: h => h(App)
})

if (window.__INITIAL_STATE__) store.replaceState(window.__INITIAL_STATE__)

router.beforeResolve(async (to, from, next) => {
  try {
    const components = router.getMatchedComponents(to)
    await Promise.all(components.map(x => x.fetch && x.fetch({ store })))

    if (window.__PRERENDER_INJECTED) injectInitialState(store.state)
  } catch (error) {
    console.log(error)
  }
  return next()
})

app.$mount(`#app`)

Revised views/Home.vue and views/About.vue

<template>
  <div data-view> <!-- adding "data-view" here -->
     <!-- omitted -->
  </div>
</template>

<script>
// omitted
</script>
Read more comments on GitHub >

github_iconTop Results From Across the Web

Vue js babel build error after installing prerender-spa-plugin
So I have a vue-js application and today I started using prerender-spa-plugin to generate some static pages for better SEO.
Read more >
How to fix [prerender-spa-plugin] Unable to prerender all routes
[prerender-spa-plugin] Unable to prerender all routes! (node:28824) UnhandledPromiseRejectionWarning: Build failed with errors.
Read more >
How to prerender a Vuejs 3 application with Vue Cli? - Get Help
[prerender-spa-plugin] Unable to prerender all routes! ERROR Error: Build failed with errors. Error: Build failed with errors.
Read more >
prerender-spa-plugin - Bountysource
- Building for production... Starting type checking and linting service... Using 1 worker with 2048MB memory limit Error: Failed to launch chrome! /app/ ......
Read more >
npm run build" error when building on VSTS/Azure DevOps ...
Router in ./src/main.js. The first problem here is that these lines were NOT following each other on the log file, so hard to...
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