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.

Configuring for Vue when publicPath is anything but /

See original GitHub issue

I’ve installed this plugin to pre-render my Vue project, which uses vue-router (in history mode) and Webpack.

The Vue app is not in the root directory of the server serving it. It is in a subdirectory of that server (specifically, /vue, meaning it is served from /vue/dist).

When publicPath: '/vue/dist', in vue.config.js, the plugin only pre-renders around the parent <router-view /> component. If I change publicPath: '/',, the markup for each route is pre-rendered as expected.

This is what my vue.config.js file looks like:

const path = require('path');
const PrerenderSPAPlugin = require('prerender-spa-plugin');
module.exports = {
    publicPath: '/vue/dist',
    configureWebpack: {
        plugins: [
            new PrerenderSPAPlugin({
                staticDir: path.join(__dirname, '/dist'),
                routes: [
                    '/',
                    '/vps',
                    '/contact',
                    '/web-hosting',
                    '/status',
                    '/domain-names',
                    '/agreements',
                    '/reseller-hosting',
                    '/about',
                    '/managed-wordpress'
                ],
            })
        ],
    },
};

It looks as if the pre-renderer is not passed the publicPath, but I am not confident in saying that. I would like to know whether there is a way to pass the publicPath parameter to the pre-renderer, and if so, what it is. I’ve looked through the open and closed issues on this project, but the suggestions I found assume that the server root is one within the app’s base directory. In my case, it is outside this directory.

Issue Analytics

  • State:open
  • Created 4 years ago
  • Reactions:2
  • Comments:8

github_iconTop GitHub Comments

13reactions
takatamacommented, Apr 26, 2020

If you would like to serve the Vue app for subdirectory /vue (https://example.com/vue), you should specify not only publicPath, but also outputDir, staticDir, and indexPath as follows:

  • outputDir is dist/vue
  • routes includes the subdirectory like [‘/vue’, ‘/vue/vps’, ‘/vue/contact’, …]
  • staticDir is dist
  • indexPath is dist/vue/index.html
const path = require('path')
const PrerenderSPAPlugin = require('prerender-spa-plugin')

module.exports = {
  publicPath: '/vue/',
  outputDir: 'dist/vue/',
  configureWebpack: () => {
    if (process.env.NODE_ENV === 'production') {
      return {
        plugins: [
          new PrerenderSPAPlugin({
            staticDir: path.join(__dirname, 'dist'),
            indexPath: path.join(__dirname, 'dist/vue/index.html'),
            ['/vue', '/vue/vps', '/vue/contact']
        ]
      }
    }
  }
}
0reactions
cnvoacommented, Dec 22, 2020

If you would like to serve the Vue app for subdirectory /vue (https://example.com/vue), you should specify not only publicPath, but also outputDir, staticDir, and indexPath as follows:

  • outputDir is dist/vue
  • routes includes the subdirectory like [‘/vue’, ‘/vue/vps’, ‘/vue/contact’, …]
  • staticDir is dist
  • indexPath is dist/vue/index.html
const path = require('path')
const PrerenderSPAPlugin = require('prerender-spa-plugin')

module.exports = {
  publicPath: '/vue/',
  outputDir: 'dist/vue/',
  configureWebpack: () => {
    if (process.env.NODE_ENV === 'production') {
      return {
        plugins: [
          new PrerenderSPAPlugin({
            staticDir: path.join(__dirname, 'dist'),
            indexPath: path.join(__dirname, 'dist/vue/index.html'),
            ['/vue', '/vue/vps', '/vue/contact']
        ]
      }
    }
  }
}

You can add another line Make it work better

new PrerenderSPAPlugin({
    staticDir: path.join(__dirname, 'dist'),
    outputDir: path.join(__dirname, '/dist/vue/'),
    indexPath: path.join(__dirname, 'dist/vue/index.html'),
    ['/vue', '/vue/vps', '/vue/contact']
})
Read more comments on GitHub >

github_iconTop Results From Across the Web

How do you control your publicPath property in vue.config.js
I would compute publicPath in vue.config.js like this: function getPublicPath() { switch (process.env.NODE_ENV) { case 'production': return ...
Read more >
Configuration Reference | Vue CLI
vue.config.js is an optional config file that will be automatically loaded by @vue/cli-service if it's present in your project root (next to ...
Read more >
You will need to take following steps: 1) add vue.config.js file ...
This works but we can find the source file under js folder. If NW.js keep source file then why I'am use it? It...
Read more >
how to change Vue publicpath for production? - Laracasts
Local setup has the application as the root of the domain: https://my-app. But when we deploy to production links within our Vue components...
Read more >
The build Property - Nuxt
Customize Babel configuration for JavaScript and Vue files. .babelrc is ignored by ... config object,; An object with the following keys (all boolean...
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