<i id='ZNV31'><tr id='ZNV31'><dt id='ZNV31'><q id='ZNV31'><span id='ZNV31'><b id='ZNV31'><form id='ZNV31'><ins id='ZNV31'></ins><ul id='ZNV31'></ul><sub id='ZNV31'></sub></form><legend id='ZNV31'></legend><bdo id='ZNV31'><pre id='ZNV31'><center id='ZNV31'></center></pre></bdo></b><th id='ZNV31'></th></span></q></dt></tr></i><div id='ZNV31'><tfoot id='ZNV31'></tfoot><dl id='ZNV31'><fieldset id='ZNV31'></fieldset></dl></div>

      <tfoot id='ZNV31'></tfoot>

      • <bdo id='ZNV31'></bdo><ul id='ZNV31'></ul>
    1. <legend id='ZNV31'><style id='ZNV31'><dir id='ZNV31'><q id='ZNV31'></q></dir></style></legend>

      <small id='ZNV31'></small><noframes id='ZNV31'>

      gulp:自动为请求添加版本号以防止浏览器缓存

      时间:2023-05-28
      <legend id='JToZH'><style id='JToZH'><dir id='JToZH'><q id='JToZH'></q></dir></style></legend>
    2. <small id='JToZH'></small><noframes id='JToZH'>

        <i id='JToZH'><tr id='JToZH'><dt id='JToZH'><q id='JToZH'><span id='JToZH'><b id='JToZH'><form id='JToZH'><ins id='JToZH'></ins><ul id='JToZH'></ul><sub id='JToZH'></sub></form><legend id='JToZH'></legend><bdo id='JToZH'><pre id='JToZH'><center id='JToZH'></center></pre></bdo></b><th id='JToZH'></th></span></q></dt></tr></i><div id='JToZH'><tfoot id='JToZH'></tfoot><dl id='JToZH'><fieldset id='JToZH'></fieldset></dl></div>

        <tfoot id='JToZH'></tfoot>
          <tbody id='JToZH'></tbody>

              <bdo id='JToZH'></bdo><ul id='JToZH'></ul>
                本文介绍了gulp:自动为请求添加版本号以防止浏览器缓存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                问题描述

                限时送ChatGPT账号..

                我通过在服务器上使用 gulp 构建源文件来部署我的项目.为防止出现缓存问题,最佳做法是为请求 url 添加一个唯一编号,请参阅:防止浏览器在 Web 应用程序升级时缓存;

                I deploy my project by building source files with gulp right on the server. To prevent caching issues, the best practice could be adding a unique number to request url, see: Preventing browser caching on web application upgrades;

                在 npm 存储库中,我找不到自动向请求添加版本号的工具.我在问是否有人以前发明过这样的工具.

                In npm repositories, I couldn't find a tool for automatically adding version number to request. I'm asking if someone has invented such tool before.

                可能的实现如下:

                我在 src/ 文件夹中有一个文件 index.html,带有以下脚本标签

                I have a file index.html in src/ folder, with following script tag

                 <script src="js/app.js<!-- %nocache% -->"></script>
                

                在构建过程中,它被复制到 dist/ 文件夹,并且注释被替换为自增号

                During build it is copied to dist/ folder, and comment is replaced by autoincrement number

                 <script src="js/app.js?t=1234"></script>
                

                推荐答案

                你可以使用 gulp-version-number 为此.它可以通过将参数附加到 URL 来将版本号添加到 HTML 文档中的链接脚本、样式表和其他文件.例如:

                You can use gulp-version-number for this. It can add version numbers to linked scripts, stylesheets, and other files in you HTML documents, by appending an argument to the URLs. For example:

                <link rel="stylesheet" href="main.css">
                

                变成:

                <link rel="stylesheet" href="main.css?v=474dee2efac59e2dcac7bf6c37365ed0">
                

                您甚至不必指定占位符,就像您在示例实现中展示的那样.而且是可配置的.

                You don't even have to specify a placeholder, like you showed in your example implementation. And it's configurable.

                示例用法:

                const $ = gulpLoadPlugins();
                const versionConfig = {
                  'value': '%MDS%',
                  'append': {
                    'key': 'v',
                    'to': ['css', 'js'],
                  },
                };
                
                gulp.task('html', () => {
                  return gulp.src('src/**/*.html')
                    .pipe($.htmlmin({collapseWhitespace: true}))
                    .pipe($.versionNumber(versionConfig))
                    .pipe(gulp.dest('docroot'));
                });
                

                这篇关于gulp:自动为请求添加版本号以防止浏览器缓存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                上一篇:禁用 chrome react DevTools 进行生产 下一篇:Gulp 错误:监视任务必须是一个函数

                相关文章

                最新文章

              1. <small id='eZndQ'></small><noframes id='eZndQ'>

                      <bdo id='eZndQ'></bdo><ul id='eZndQ'></ul>
                  1. <legend id='eZndQ'><style id='eZndQ'><dir id='eZndQ'><q id='eZndQ'></q></dir></style></legend>

                  2. <i id='eZndQ'><tr id='eZndQ'><dt id='eZndQ'><q id='eZndQ'><span id='eZndQ'><b id='eZndQ'><form id='eZndQ'><ins id='eZndQ'></ins><ul id='eZndQ'></ul><sub id='eZndQ'></sub></form><legend id='eZndQ'></legend><bdo id='eZndQ'><pre id='eZndQ'><center id='eZndQ'></center></pre></bdo></b><th id='eZndQ'></th></span></q></dt></tr></i><div id='eZndQ'><tfoot id='eZndQ'></tfoot><dl id='eZndQ'><fieldset id='eZndQ'></fieldset></dl></div>
                    <tfoot id='eZndQ'></tfoot>