1

Using Vue CLI 3 how can I create a project that contains some static html files at the root of the public directory and an SPA inside of an app folder?

I'd like several static html files including an index.html at the root of the project. I want these static HTML files served outside of the SPA for SEO purposes.

Right now, my project structure looks like this:

.
├── README.md
├── babel.config.js
├── package.json
├── public
│   ├── app
│   │   └── index.html
│   ├── favicon.ico
│   └── index.html
├── src
│   ├── App.vue
│   ├── assets
│   │   └── logo.png
│   ├── components
│   │   └── HelloWorld.vue
│   └── main.js
├── vue.config.js
└── yarn.lock

I've tried many different combinations of publicPath and indexPath values in my vue.config.js file. None have achieved what I'm hoping for. I'd like yarn serve to serve both the static HTML files and SPA locally for development. More importantly, I'd like the static HTML files and the SPA properly bundled into the dist folder when I run yarn build. I haven't been able to achieve either goal.

With the configuration below, the public/index.html file that's meant to be static and only displaying at / is being served at both http://localhost:8080/ and http://localhost:8080/app/. Interestingly, at http://localhost:8080/app/ the js resources are being injected into the response along with what's meant to be static HTML.

After running yarn build with the config below I'm left with a /dist/app/index.html file that has the static index.html file code with no javascript injected instead of the SPA code with javascript injected. The /dist/index.html file has the static HTML I expect but all the javascript that's meant for the SPA is injected.

// vue.config.js
module.exports = {
    publicPath: '/app/',
    indexPath: 'index.html'
}

How can I configure this project to support static html files at the project root and an SPA in the app folder?

2
  • I think for SEO purposes you have to use NuxtJS that will provide your component as HTML files. Commented Jan 7, 2020 at 7:24
  • @KushalSuthar I considered using NuxtJS but that seems like overkill for the few pages where I care about SEO. The multipage apps option in the accepted answer works really well. Commented Jan 7, 2020 at 23:36

1 Answer 1

2

You can leverage the feature of Vue CLI to build multipage apps and actually have only one page...

// vue.config.js
module.exports = {
  pages: {
    index: {
      // entry for the page
      entry: "src/main.js",
      // the source template
      template: "public/app/index.html",
      // output as dist/app/index.html
      filename: "app/index.html",
      // when using title option,
      // template title tag needs to be <title><%= htmlWebpackPlugin.options.title %></title>
      title: "App Index Page",
      // chunks to include on this page, by default includes
      // extracted common chunks and vendor chunks.
      chunks: ["chunk-vendors", "chunk-common", "index"]
    }
  }
};
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you so much for this. I am not sure why this isn't a default thing for nested paths support

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.