gpt4 book ai didi

javascript - 当我在服务器中部署时,Angular Service Worker 离线缓存不起作用

转载 作者:行者123 更新时间:2023-11-30 19:29:56 26 4
gpt4 key购买 nike

当我使用 angular/pwa 构建我的 Angular 项目并使用 http-server 在本地服务器上测试它时,它按预期工作。甚至离线缓存在本地也能正常工作。

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

service-workers 没有在 angular 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)"
]
}
}
]
}

最佳答案

像这样通过删除 $schema 来更新你的 serivce worker,我的配置和你的一样

{
"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

所以你的 service worker 不会工作

关于javascript - 当我在服务器中部署时,Angular Service Worker 离线缓存不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56520967/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com