【问题标题】:Handling cache beyong Nginx server and webpack js and css versioning在 Nginx 服务器和 webpack js 和 css 版本控制之外处理缓存
【发布时间】:2018-05-16 08:49:42
【问题描述】:

我有一个在 EC2 上运行的 React nodejs 应用程序。

我已经在 Nginx 之外设置了 3 个实例用于负载平衡。

我还在 Nginx 配置中启用了缓存。

基本上所有内容都应该缓存在不同版本的 app.js 旁边,其中包含捆绑的 React 代码和同样捆绑的 style.css。

我想在 js 和 css src 链接中添加一个版本号(例如http://mywebsite.com/app.js?1.0

我的问题是,我可以使用 webpack 自动执行此操作吗?这是要走的路吗?

【问题讨论】:

    标签: javascript css node.js caching nginx


    【解决方案1】:

    html-webpack-plugin 是你的朋友。

    不要创建 index.html 文件,而是让 webpack 为你做这件事。

    var HtmlWebpackPlugin = require('html-webpack-plugin');
    
    module.exports = {
    
        entry: "./index.js",
        output: {
            filename: "./dist/app.bundle.[hash].js"
        },
        plugins: [
            new HtmlWebpackPlugin({
                hash: true,
                filename: './dist/index.html'
            })
       ]
    
    }
    

    这将自动将输出脚本添加到 index.html 中,并为文件生成哈希。

    【讨论】:

    • 成功了,对于 js 和 css 都适用。但是我注意到,如果您将 hash 设置为 true,它会将 ?[hash] 添加到 src 链接,这是 google speed test 和其他基准测试工具不推荐的。我已将其删除,并按照您的建议将其添加到文件名中。我也使用了模板选项,因为我想保留一些初始的东西。再次感谢
    猜你喜欢
    • 1970-01-01
    • 2012-05-27
    • 1970-01-01
    • 2013-03-10
    • 2013-09-30
    • 2018-04-15
    • 1970-01-01
    • 1970-01-01
    • 2023-03-16
    相关资源
    最近更新 更多