Angular Service Worker - 新链接

发布于 2025-01-08 23:38:47 字数 2269 浏览 4 评论 0原文

假设您有一个带有以下路由器链接的应用程序 v2.0 的部署版本:

const routes: Routes = [
  {
    path: "home",
    loadChildren: () =>
      import("src/main/home.module").then((m) => m.HomeModule),
  },
  { path: "**", component: 404Component },
];

然后,当您添加新的路由器链接并部署 v2.1 时:

const routes: Routes = [
  {
    path: "home",
    loadChildren: () =>
      import("src/main/home.module").then((m) => m.HomeModule),
  },
  {
    path: "test",
    loadChildren: () =>
      import("src/main/test.module").then((m) => m.TestModule),
  },
  { path: "**", component: 404Component },
];

该应用程序使用 Service Worker,因此之前访问过的用户将拥有版本 2.0缓存。

现在,如果我向一些老用户分享链接,例如:mywebsite.com/test,那么老用户将获得 404 页面,30 秒后如果用户仍在该页面上,版本 v2.1在后台下载,应用程序刷新并显示正确的页面(TestModule)。

以下是我的 Service Worker 片段:

注册

ServiceWorkerModule.register("ngsw-worker.js", {
  enabled: ENV.production,
  registrationStrategy: "registerImmediately",
}),

ngsw-worker.js

{
      "$schema": "./node_modules/@angular/service-worker/config/schema.json",
      "index": "/index.html",
      "assetGroups": [
        {
          "name": "app",
          "installMode": "prefetch",
          "resources": {
            "files": [
              "/favicon.ico",
              "/index.html",
              "/manifest.webmanifest",
              "/*.css",
              "/*.js"
            ]
          }
        }, {
          "name": "assets",
          "installMode": "lazy",
          "updateMode": "prefetch",
          "resources": {
            "files": [
              "/assets/**",
              "/*.(eot|svg|cur|jpg|png|webp|gif|otf|ttf|woff|woff2|ani)"
            ]
          }
        }
      ]
    }

检查更新:

export class UpdateService {

  constructor(public updates: SwUpdate) {
    if (updates.isEnabled) {
      interval(6 * 60 * 60).subscribe(() => updates.checkForUpdate())
    }
  }

  public checkForUpdates(): void {
    this.updates.available.subscribe(event => {
      this.promptUser();
    });
  }

  private promptUser(): void {
    this.updates.activateUpdate().then(() => document.location.reload()); 
  }
}

有什么办法强制使用新版本吗?

Say you have a deployed version of your app v2.0 with these router links:

const routes: Routes = [
  {
    path: "home",
    loadChildren: () =>
      import("src/main/home.module").then((m) => m.HomeModule),
  },
  { path: "**", component: 404Component },
];

Then when you add a new router link and you deploy v2.1:

const routes: Routes = [
  {
    path: "home",
    loadChildren: () =>
      import("src/main/home.module").then((m) => m.HomeModule),
  },
  {
    path: "test",
    loadChildren: () =>
      import("src/main/test.module").then((m) => m.TestModule),
  },
  { path: "**", component: 404Component },
];

The app uses a service worker, so users that have visited previously will have the version 2.0 cached.

Now if I share a link to some old users such as: mywebsite.com/test then the old users will get 404 page and 30 seconds later if the user is still on the page, version v2.1 is downloaded in the background and the app refreshes and shows the correct page (TestModule).

Below is my service worker snippets:

Registration

ServiceWorkerModule.register("ngsw-worker.js", {
  enabled: ENV.production,
  registrationStrategy: "registerImmediately",
}),

ngsw-worker.js

{
      "$schema": "./node_modules/@angular/service-worker/config/schema.json",
      "index": "/index.html",
      "assetGroups": [
        {
          "name": "app",
          "installMode": "prefetch",
          "resources": {
            "files": [
              "/favicon.ico",
              "/index.html",
              "/manifest.webmanifest",
              "/*.css",
              "/*.js"
            ]
          }
        }, {
          "name": "assets",
          "installMode": "lazy",
          "updateMode": "prefetch",
          "resources": {
            "files": [
              "/assets/**",
              "/*.(eot|svg|cur|jpg|png|webp|gif|otf|ttf|woff|woff2|ani)"
            ]
          }
        }
      ]
    }

Check for updates:

export class UpdateService {

  constructor(public updates: SwUpdate) {
    if (updates.isEnabled) {
      interval(6 * 60 * 60).subscribe(() => updates.checkForUpdate())
    }
  }

  public checkForUpdates(): void {
    this.updates.available.subscribe(event => {
      this.promptUser();
    });
  }

  private promptUser(): void {
    this.updates.activateUpdate().then(() => document.location.reload()); 
  }
}

Is there any way to force the new version?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

等风来 2025-01-15 23:38:47

据我所知,不幸的是,没有办法在应用程序加载时强制更新,但您可以缩短 30 秒的等待时间。在您的构造函数中,使用计时器而不是间隔,它将立即检查更新。因此您的应用程序将在 5 秒左右更新。

As far as I know there is unfortunately no way to force the update on application load, but you could improve the 30 seconds wait time. In your constructor instead of interval use timer which will check for update immediately. So your application will update around 5 seconds.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文