阻止UI5应用程序资源的浏览器缓存

时间:2017-05-24 07:38:19

标签: caching sap sapui5

我们在SAP PO上部署了SAPUI5应用程序。问题在于,每当我们更改并部署新版本的应用程序时,都不会反映更改,我们需要执行Hard Reload和Clear浏览器缓存来获取新的更改。

这导致了很多问题,因为我们不能要求客户在每次更改后都清除缓存。

以下是我们迄今为止尝试的失败方法:

  1. 在SAPUI5引导程序中启用“resources / sap-ui-cachebuster / sap-ui-core.js”。

  2. 将“Application Cache buster”用于应用程序资源(使用sap-ui-cachebuster-info.json)

  3. 设置HTML标头以保持无缓存:

  4. <meta http-equiv='cache-control' content='no-cache, no-store, must-revalidate'>
    <meta http-equiv='Expires' content='-1'>
    <meta http-equiv='Pragma' content='no-cache'>
    
    1. 使用以下代码清除Cookie:
    2. document.cookie.split(";").forEach(function(c) { 
      document.cookie = c.replace(/^ +/, "").replace(/=.*/, "=;expires=" + new Date().toUTCString() + ";path=/"); 
      });
      

      到目前为止,上述解决方案都没有奏效。这就是我们在Chrome的“投放网络”标签中看到的内容:

      enter image description here

      注意:应用程序部署在SAP PO 7.4(JAVA堆栈)

1 个答案:

答案 0 :(得分:2)

我们在SAP MII上遇到了与您相同的问题,而且我花了数月时间为SAP提供了几个OSS呼叫,以提供可接受的解决方案。

他们在SAP MII的SP3中这样做了(我们尚未更新但我希望他们的更正是正确的),但这不适用于您的情况,因为您在SAP PO上但是#&# 39; s仍然是Java Stack。

所以我认为您应该打开一个OSS调用,建议SAP咨询SAP Notes:

他们可能会将您重定向到以下堆栈溢出主题: http://stackoverflow.com/questions/118884/how-to-force-browser-to-reload-cached-css-js-files

但这只是一个解决方法,Java堆栈上的SAP Web服务器似乎没有正常工作,他们必须提供更正。

希望这会对你有所帮助。

修改

嗨,

这是一个更新,我们有时会使用它。 我们有一个URL参数,用于标识是否需要重新加载页面。 请参阅下面我们嵌入SAPUI5应用程序的index.html页面的JS片段。

希望这会对你有所帮助。

    <script>

window.onload = function () {

    version = undefined;
    fCheckVersionMatch = false;

    onInit();
};

/***************************************************************************
 * Function launch when we start the application it test
 *  - if the Reload parameters is set in the url
 *      - if we are loading an hold application with a false reload value
 ****************************************************************************/
var onInit = function() {
    checkParamReload();
};

/***************************************************************************
 * Check in the url if there is the reload value and if this value is less 
 * than the difference with the date now => avoid when using favorite link
 * to load a previous version with an incorrect time stamp
 ****************************************************************************/
var checkParamReload = function() {

    var sUrlParameters = window.top.document.location.search;
    var regexReload = /(\?|&)reload=([^&]*)/;
    var aReload = sUrlParameters.match(regexReload);
    var nTime = aReload == null ? NaN : parseInt(aReload[2]);

    if ( isNaN(nTime) || Math.abs(Date.now() - nTime) > 60000 ) {               
        // In case no reload tag is present or it's value is more than 1 minute ago, reload page
        reloadPage(true); // True means force reload => reset retry count.
    }

};

/***************************************************************************
 * Reload page and make sure the reload param is updated.
 * If force reload is used, retry count is resetted, otherwise it is
 * it is incremented up to a limit, which - in case it is reached - stops
 * the reload process and instead display an error message.
 ****************************************************************************/
var reloadPage = function (bForce) {

    var retries = 0;
    var oLocation = window.top.document.location;

    var sSearch = oLocation.search;
    sSearch = queryReplace(sSearch, "reload", _ => Date.now());
    if (bForce) {
        sSearch = queryReplace(sSearch, "retry", _ => 0);
    } else {
        sSearch = queryReplace(sSearch, "retry", function (n) {
            if (isNaN(parseInt(n))) {
                return 0;
            } else {
                retries = parseInt(n);
                return retries + 1;
            }
        });
    }

    if (retries < 10) {
        // Reload Page
        window.top.document.location.replace(oLocation.origin + oLocation.pathname + sSearch + oLocation.hash);
    } else {
        // Display error
        document.getElementById('Error').style.display = "block";
    }

};

var queryReplace = function (sQuery, sAttribute, fnReplacement) {
    // Match the attribute with the value
    var matcher = new RegExp(`(\\?|&)${ sAttribute }=([^&]*)`);
    var sNewQuery = sQuery.length < 2 ? `?${ sAttribute }=` : sQuery;
    if (sNewQuery.search(matcher) < 0) {
        // If we could not match, we add the attribute at the end
        sNewQuery += "&" + sAttribute + "=" + fnReplacement("");
    } else {
        sNewQuery = sNewQuery.replace(matcher, (_, delim, oldVal) => delim + sAttribute + "=" + fnReplacement(oldVal));
    }
    return sNewQuery;
}

</script>