vue预渲染在根目录的可见 prerender-spa-plugin , 这里写的是打包后文件放到子目录的情况

.env

  1. VUE_APP_publicPath = "/"

.env.production

  1. VUE_APP_publicPath = "/pages/"

router.ts

  1. const router = createRouter({
  2. history: createWebHistory(process.env.VUE_APP_publicPath),
  3. routes,
  4. });

vue.config.js

  1. const isProduction = process.env.NODE_ENV === "production";
  2. const publicPath = process.env.VUE_APP_publicPath;
  3. // eslint-disable-next-line @typescript-eslint/no-var-requires
  4. const PrerenderSPAPlugin = require("prerender-spa-plugin");
  5. // eslint-disable-next-line @typescript-eslint/no-var-requires
  6. const path = require("path");
  7. module.exports = {
  8. publicPath: publicPath,
  9. outputDir: "dist" + publicPath,
  10. productionSourceMap: false,
  11. configureWebpack: (config) => {
  12. if (isProduction) {
  13. const plugins = [];
  14. plugins.push(
  15. new PrerenderSPAPlugin({
  16. staticDir: path.join(__dirname, "dist"),
  17. indexPath: path.join(__dirname, "dist" + publicPath + "index.html"),
  18. routes: ["/original-painting", "/python/online", "/python/data-analysis", "/welfare"],
  19. renderer: new PrerenderSPAPlugin.PuppeteerRenderer({
  20. // 是否要开启debug模式,默认不开启
  21. // headless: true,
  22. // 视图组件是在API请求获取所有必要数据后呈现的,因此我们在dom中存在“data view”属性后创建页面快照
  23. renderAfterDocumentEvent: "render-event",
  24. }),
  25. })
  26. );
  27. config.plugins = [...config.plugins, ...plugins];
  28. }
  29. },
  30. };