Matt Stow
Matt Stow

Reputation: 6379

How to pre-render multiple Vue app pages?

I'm trying (unsuccessfully) to pre-render the HTML of multiple Vue apps within the same project scaffolded with Vue CLI. I do not want to use Vue Router or Nuxt etc for multiple reasons.

I've tried using prerender-spa-plugin, but since I don't use routes, it only pre-renders the index.

My vue.config.js looks like this:

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

const Renderer = PrerenderSPAPlugin.PuppeteerRenderer;

module.exports = {
  pages: {
    index: 'src/index.js',
    about: 'src/about.js',
  },
  configureWebpack: {
    plugins: [
      new PrerenderSPAPlugin({
        staticDir: path.join(__dirname, 'dist'),
        routes: ['/'],

        postProcess(route) {
          route.html = route.html.replace('</script><div>', '</script><div id="app" data-server-rendered="true">');

          return route;
        },

        renderer: new Renderer({
          headless: true,
          renderAfterDocumentEvent: 'render-event',
        }),
      }),
    ],
  },
};

and my index.js and about.js essentially look like this:

import Vue from 'vue';
import App from './Index.vue';

new Vue({
  render: h => h(App),
  mounted() {
    document.dispatchEvent(new Event('render-event'));
  },
}).$mount('#app');

I also have unique public/ index.html and about.html pages.

The routes parameter of prerender-spa-plugin doesn't seem to recognise things like '/about.html'. Is there an easy way of achieving multiple pre-rendered pages?

Do I have to wrestle with the SSR module?

Thanks in advance.

Upvotes: 3

Views: 2530

Answers (2)

Karthi Kumar
Karthi Kumar

Reputation: 23

I'm also facing the same issue, i have static html uses vue component and i want to pre-render the vue component in output build directory. I'm using laravel-mix package for build process. Could you post the full solution for this i.e calling new PrerenderSPAPlugin multiple times, one for each route.

If i can get the full webpack.config.js, it would easy for me to understand and implement the same using laravel-mix.

Upvotes: 0

Matt Stow
Matt Stow

Reputation: 6379

The solution I've found is to call new PrerenderSPAPlugin multiple times, one for each route.

Upvotes: 2

Related Questions