我在服务器中部署后,Angular Service Worker脱机缓存不起作用

时间:2019-06-10 05:34:10

标签: angular service-worker offline offline-caching angular-service-worker

当我使用angular / pwa构建我的angular项目并使用http-server在本地服务器上对其进行测试时,它正在按预期方式工作。甚至离线缓存在本地都可以正常工作。

但是,当我在服务器中部署了相同的东西时,我能够在任何设备上安装和运行它,但是当我离线时就无法打开。

服务工作者未将内容缓存在有角度的pwa中

这是我的app.module.ts

import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { HttpClientModule } from '@angular/common/http';

import { AppRoutingModule } from './app-routing.module';
import { AppComponent } from './app.component';
import { GetchucknorrisjokesService } from './getchucknorrisjokes.service';
import { ServiceWorkerModule } from '@angular/service-worker';
import { environment } from '../environments/environment';

@NgModule({
  declarations: [
    AppComponent,
  ],
  imports: [
    BrowserModule,
    AppRoutingModule,
    HttpClientModule,
    ServiceWorkerModule.register('ngsw-worker.js', { enabled: environment.production })
  ],
  providers: [ GetchucknorrisjokesService ],
  bootstrap: [AppComponent]
})
export class AppModule { }

这是我的ngsw-config.json文件

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

1 个答案:

答案 0 :(得分:0)

通过删除$ schema来像这样更新您的服务工人,我的配置与您的

相同
{
  "index": "/index.html",
  "assetGroups": [
    {
      "name": "app",
      "installMode": "prefetch",
      "resources": {
        "files": [
          "/favicon.ico",
          "/index.html",
          "/*.css",
          "/*.js"
        ]
      }
    }, {
      "name": "assets",
      "installMode": "lazy",
      "updateMode": "prefetch",
      "resources": {
        "files": [
          "/assets/**",
          "/*.(eot|svg|cur|jpg|png|webp|gif|otf|ttf|woff|woff2|ani)"
        ]
      }
    }
  ]
}

您遇到了问题,因为当您进入生产模式时,您的代码正在寻找此文件./node_modules/@angular/service-worker/config/schema.json

所以您的服务人员将无法工作