简体   繁体   English

使用SeedStack中的RequireJS防止Javascript文件上的浏览器缓存问题

[英]Prevent browser cache issue on Javascript files with RequireJS in SeedStack

using SeedStack 14.7 we are facing a cache issue when uploading a new version on servers: every user have to clear their cache to get the last version of files. 使用SeedStack 14.7,我们在服务器上上传新版本时面临缓存问题:每个用户都必须清除其缓存才能获取文件的最新版本。

I tried to use "urlArgs": "version=2" in the requireConfig part of the fragment JSON file. 我试图在片段JSON文件的requireConfig部分中使用"urlArgs": "version=2" It do the job by adding argument on every files and so we can use it when changing version, but it also affect the urls in the config of each modules ! 它通过在每个文件上添加参数来完成这项工作,因此我们可以在更改版本时使用它,但它也会影响每个模块的配置中的url!

As we are using this config to pass the REST base url to each module, it breaks all REST requests by adding the argument to the base url. 当我们使用此配置将REST基本URL传递到每个模块时,它通过将参数添加到基本URL来中断所有REST请求。

My fragment JSON file : 我的片段JSON文件:

{
    "id": "mac2-portail",
    "modules": {
        "gestionImage": {
            "path": "{mac2-portail}/modules/gestionImage",
            "autoload": true,
            "config": {
                "apiUrl": "muserver/rest"
            }
        }
    },
    "i18n": {...},
    "routes": {...},
    "requireConfig": {
        "urlArgs": "version=2",
        "shim": {...}
    }
}

Any idea to solve the cache issue without breaking REST requests ? 有什么想法可以解决缓存问题而不破坏REST请求吗?

EDIT : it is not a duplicate of Prevent RequireJS from Caching Required Scripts . 编辑:它不是防止RequireJS从缓存所需的脚本的重复。 Yes SeedStack uses RequireJS and this configuration solve the cache issue, but it also affect other modules defined in the fragment so I need to find another solution to prevent browser to cache files 是的,SeedStack使用RequireJS,此配置解决了缓存问题,但它也影响了片段中定义的其他模块,因此我需要找到另一种解决方案来防止浏览器缓存文件

The module configuration values, like apiUrl in your example, are not touched by RequireJS unless you call require.toUrl() on them explicitly. 除非您显式调用require.toUrl() ,否则RequireJS不会触及模块配置值(如apiUrl中的require.toUrl() I think this is what is happening in your case. 我认为这就是您的情况。 To avoid this problem, you should always do the concatenation first and only then call require.toUrl() on the full resulting URL. 为避免此问题,您应该始终先进行串联,然后require.toUrl()完整的结果URL调用require.toUrl()

So, instead of doing: 因此,与其做:

var fullUrl = require.toUrl(config.apiUrl) + '/my/resource';

Do this: 做这个:

var fullUrl = require.toUrl(config.apiUrl + '/my/resource');

By the way, instead of setting the version directly in the RequireJS configuration, you can simply add the version of your application to the data-w20-app-version attribute on the <html> element of the master page: 顺便说一句,您无需在RequireJS配置中直接设置版本,只需将应用程序的版本添加到母版页<html>元素上的data-w20-app-version属性中即可:

<html data-w20-app data-w20-app-version="2.0.0">

This will provide the same behavior but will work correctly in the case of Angular templates in $templateCache . 这将提供相同的行为,但在$templateCache的Angular模板的情况下将正常工作。 If your master page is automatically generated by the backend, this is done automatically. 如果您的母版页是由后端自动生成的,则会自动完成。 Check this page for the details. 查看此页面以获取详细信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM