简体   繁体   English

Angular 通用缓存在浏览器中

[英]Angular Universal Cached in Browsers

Using Angular (v12) with Universal I have a website that has cached in some browsers an old version of angular, so you see old versions in some cases.将 Angular (v12) 与 Universal 一起使用 我有一个网站在某些浏览器中缓存了旧版本的 angular,因此在某些情况下您会看到旧版本。

I can't force the browser to clear the cache in all cases because it's a public website.我不能强制浏览器在所有情况下都清除缓存,因为它是一个公共网站。 So some things have been added to "force it".所以加了一些东西来“强制”。 But it is not working and there are browsers that are still showing an older version of it.但它不起作用,并且有些浏览器仍在显示它的旧版本。

In the index.html has been added:在 index.html 中添加了:

 <meta http-equiv="Expires" content="0">
  <meta http-equiv="Last-Modified" content="0">
  <meta http-equiv="Cache-Control" content="no-cache, must-revalidate">
  <meta http-equiv="Pragma" content="no-cache">
  <meta name="Revisit" content="10 days">

And after compiling, manually modified the angular.js to add a date as a parameter in the style "main.js?t=20221115".并编译后,手动修改angular.js,在样式“main.js?t=20221115”中添加一个日期作为参数。

Any ideas or guidance on how to force all browsers to load the new version?关于如何强制所有浏览器加载新版本的任何想法或指导? I say this because in some cases they are still showing the March version.我这样说是因为在某些情况下他们仍然展示三月版本。

Thanks for your time.谢谢你的时间。

Translated with www.DeepL.com/Translator (free version)www.DeepL.com/Translator翻译(免费版)

I had the same problem.我有同样的问题。 To check if the site is up to date, I request a "version" file (file?t=) and I compare it with the version in the "environment" file.为了检查该站点是否是最新的,我请求一个“版本”文件 (file?t=) 并将它与“环境”文件中的版本进行比较。 If the version is not the same, I display a popup with a reload button如果版本不一样,我会显示一个带有重新加载按钮的弹出窗口

I generate the version in the "environment" and "version" file before the build我在构建之前在“环境”和“版本”文件中生成版本

package.json package.json

"scripts": { 
"build": "yarn run build:browser && yarn run build:server",
"build:browser": "yarn versionning && ng build --configuration=production",
"build:server": "ng run website:server --configuration=production",
"versionning": "node replace-version.js"
}

replace-version.js替换版本.js

var fs = require('fs')
var version =  Math.floor(Math.random()*100000000);
console.log('version',  version)
fs.readFile('src/environments/environment.prod.ts', 'utf8', function (err,data) {
  if (err) { return console.log(err);}
  var result = data.replace(/version\s*:.*/, 'version : ' + version);
  fs.writeFile('src/environments/environment.prod.ts', result, 'utf8', function (err) {
    if (err) return console.log(err);
  });
});


fs.writeFile('src/assets/version.json', '{ "version" : '+version+' }' , 'utf8', function (err) {
  if (err) return console.log(err);
});

environment.prod.ts环境.prod.ts

export const environment = {
  production: true,
  versionFile: 'assets/version.json',
  version : 60065676
};

assets/version.json资产/版本.json

{ "version" : 60065676 }

When loading the site (in the app.component) I call checkVersion()加载网站时(在 app.component 中)我调用 checkVersion()

this.versionService.checkVersion(environment.versionFile, environment.version, environment.production)

version.service.ts版本.service.ts

import {HttpClient} from '@angular/common/http';
import { Inject, Injectable, PLATFORM_ID} from '@angular/core';
import {isPlatformBrowser} from '@angular/common';

@Injectable({  providedIn: 'root' })
export abstract class VersionService {
  constructor(  @Inject(PLATFORM_ID) private platformId: Object, private http: HttpClient) {}
  
  public checkVersion(url, version, prod) {
    if (isPlatformBrowser(this.platformId) && prod) {
      this.http.get(url + '?t=' + new Date().getTime()).subscribe(
        (response: any) => {
          const hashChanged = response.version && version !== response.version;
          if (hashChanged) {
            this.showPopupNewVersion();
          }
        },
        (err) => {
          console.error(err, 'Could not get version');
        }
      );
    }
  }
  
  showPopupNewVersion(){
     // SHOW alert or Popup or snackbar to warn the user
     // Exec  window.location.reload(); after XXXsec or user click
     }  
}

It works fine for me in case an old version is displayed because of the cache.如果由于缓存而显示旧版本,它对我来说很好用。 Hope this answers your question希望这能回答你的问题

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

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