index.html 文件添加如下代码

<meta http-equiv="pragram" content="no-cache">
<meta http-equiv="cache-control" content="no-cache, no-store, must-revalidate">
<meta http-equiv="expires" content="0">

通过给打包的文件路径添加时间戳,在根目录创建vue.config.js文件,并添加如下代码:

const path = require("path");
const webpack = require("webpack");
const timeStamp = new Date().getTime();
module.exports = {
  publicPath: process.env.NODE_ENV === "production" ? "/dist/" : "/",
  // 打包的时候不使用hash值.因为我们有时间戳来确定项目的唯一性了.
  filenameHashing: false,
  // 将构建好的文件输出到哪里
  outputDir: "dist",
  configureWebpack: {
    // 重点
    // 输出重构 打包编译后的js文件名称,添加时间戳.
    output: {
      filename: `js/[name].${timeStamp}.js`,
      chunkFilename: `js/chunk.[id].${timeStamp}.js`
  css: {
    //重点.
    extract: {
      // 打包后css文件名称添加时间戳
      filename: `css/[name].${timeStamp}.css`,
      chunkFilename: `css/chunk.[id].${timeStamp}.css`
  • filename 指列在entry 中,打包后输出的文件的名称。
  • chunkFilename 指未列在entry 中,却又需要被打包出来的文件的名称。
  • nginx 配置,让index.html不缓存。

    location = /index.html {
        add_header Cache-Control "no-cache, no-store";
    

    项目本地启动无问题,部署上线后访问,控制台提示 引用静态资源报错:net::ERR_ABORTED 404 (Not Found)

  • 一、可能是因为vue.config.js中的publicPath路径错误;保证自己路径正确的前提下,将publicPath修改为如下代码:
  • publicPath: process.env.NODE_ENV === "production" ? "/dist/" : "/",
    
  • 二、可能是因为打包后的dist没有放到服务器的根目录下,查看config文件夹下的index.js文件,找到build.assetsPublicPath,将"/“修改成”./"
  • 项目打包后只能访问首页,其他页面全部报错404或者显示空白

  • 查看路由表,是否将路由模式设置为了history模式,如果设置了,只需将 history 注释掉 然后 将项目打包后上到环境就可以了 前端开发工程师 @ 杭州
    粉丝
  •