在多页应用程序中缓存供应商捆绑文件

时间:2019-10-22 13:52:00

标签: javascript webpack browser-cache

我们的情况很特殊:我们有Golang模板,其中包含服务器渲染的HTML和前端资产(HTML / CSS / JS标签)的混合。我们正在从无处不在的.center { margin: auto; height: 500px; width: 500px; } body { background-color: lightgrey; height: 100%; display: grid; } 标签和jQuery转移到ReactJS和Webpack管理资产。

模板

我们项目中的典型模板如下:

<script>

Webpack配置

截至昨天,当我从所有捆绑包文件中拆分{{/* some Golang template logic */}} <link rel=stylesheet href="/static/css/[some-folder-or-folders]/[css-file].css"> <script src="/static/js/ViewModelViews/[some-folder-or-folders]/bundle.js"></script> {{end}} 时,我们的webpack配置如下:

node_modules

我能够将所有module.exports = { node: { fs: "empty" }, entry: entryObj, output: { path: path.resolve(__dirname, `./static/js`), filename: '[name]/bundle.js' }, optimization: { splitChunks: { cacheGroups: { vendor: { test: /[\\/]node_modules[\\/]/, name: 'vendors', chunks: 'all', }, }, } }, module: { rules: [ { test: /\.(js|jsx)$/, exclude: /node_modules/, loader: "babel-loader", query: { plugins: ["transform-class-properties"] } }, { test: /\.s[ac]ss$/i, use: ['style-loader', 'css-loader', 'sass-loader'] } ] }, plugins: [ new NodemonPlugin(), ] } 拆分为node_modules,但是由于某种原因,我无法让浏览器对其进行缓存。

我在vendors/bundle.js上明确包含了该文件:

base.html

如何在保持对其他捆绑软件文件使用方式的控制权的同时,使<!DOCTYPE html> <html> <head> <!-- some irrelevant logic --> </head> <body> <!-- a bunch of irrelevant logic --> <script src="/static/js/vendors/bundle.js"></script> <!-- other global bundle files --> </body> </html> 缓存?我看到人们在文件名中使用哈希码,这会破坏我们对业务逻辑的导入。

开发工具的网络部分

当我进入登录页面时,浏览器请求vendors/bundle.js

enter image description here

当我转到另一个页面时,请求再次发生!

enter image description here

0 个答案:

没有答案
相关问题