实现缓存的方式是在新构建上传时不需要清除缓存。

内容来源于 Stack Overflow,并遵循CC BY-SA 3.0许可协议进行翻译与使用

  • 回答 (1)
  • 关注 (0)
  • 查看 (55)

我已经开发了关于角2的项目网络包。每当我部署我的新版本时,客户端需要清除缓存。以查看代码所做的更改。下面是我的webPack.config.js:

const path = require('path');
const webpack = require('webpack');
const typescript = require('typescript');
const { AotPlugin } = require('@ngtools/webpack');

const rules = [
  { test: /\.html$/, loader: 'html-loader' },
  { test: /\.scss$/, loaders: ['raw-loader', 'sass-loader'] },
  { test: /\.(jpe?g|png|gif|svg)$/i, loader: 'file-loader' }
];

const plugins = [
  new webpack.DefinePlugin({
    'process.env': {
      'NODE_ENV': JSON.stringify(process.env.NODE_ENV)
    }
  }),
  new webpack.optimize.CommonsChunkPlugin({
    name: 'vendor',
    minChunks: (module) => module.context && /node_modules/.test(module.context)
  })
];


rules.push({
    test: /\.ts$/,
    loaders: [
    'awesome-typescript-loader', 'angular-router-loader', 'angular2-template-loader'
    ]
});

plugins.push(
    new webpack.NamedModulesPlugin(),
    new webpack.ContextReplacementPlugin(/angular(\\|\/)core(\\|\/)@angular/, path.resolve(__dirname, './notfound')),
    new webpack.LoaderOptionsPlugin({
    minimize: true,
    debug: false
    }),
    new webpack.optimize.UglifyJsPlugin({
    sourceMap: true,
    beautify: false,
    mangle: {
        screw_ie8: true
    },
    compress: {
        unused: true,
        dead_code: true,
        drop_debugger: true,
        conditionals: true,
        evaluate: true,
        drop_console: true,
        sequences: true,
        booleans: true,
        screw_ie8: true,
        warnings: false
    },
    comments: false
    })
);


module.exports = {
  cache: true,
  context: __dirname,
  devServer: {
    contentBase: __dirname,
    historyApiFallback: true,
    stats: {
      chunks: true,
      chunkModules: true,
      chunkOrigins: true,
      errors: true,
      errorDetails: true,
      hash: true,
      timings: true,
      modules: true,
      warnings: true
    },
    publicPath: './build/',
    port: 3000
  },
  devtool: 'sourcemap',
  entry: {
    app: ['zone.js/dist/zone', './app/main.ts']
  },
  output: {
    filename: '[name].js',
    chunkFilename: 'lfd6093bc62332f579200891cc0e9c8[name].js',
    publicPath: './build/',
    path: path.resolve(__dirname, 'build')
  },
  node: {
    console: true,
    global: true,
    process: true,
    Buffer: false,
    setImmediate: false
  },
  module: {
    rules
  },
  resolve: {
    extensions: ['.ts', '.js'],
    modules: [
      'app',
      'node_modules'
    ]
  },
  plugins
};

有什么办法可以让我克服这个问题吗?只限于客户端?如果没有,我的最后手段将是检查API版本,并给用户弹出清除缓存。

提问于
用户回答回答于

默认情况下,Webpack不知道文件是否已更改,但可以通过更改输出文件名和放置contenthash在上面:

output: { filename: '[name].[contenthash].js'}

通过这样做,将生成基于文件内容的散列,如果文件内容更改,则此散列更改,使webpack知道必须重建所有内容。

扫码关注云+社区

领取腾讯云代金券