【发布时间】:2019-12-06 05:26:26
【问题描述】:
我创建了一个 Angular PWA 并将其移动到 ASP.Net Core Spa - 应用程序中。通过“npm http-server”提供和安装时,我可以离线运行它。
当我在 ASP.Net 服务器中运行它时,Serviceworker 会在 ngsw-worker.js 中加载错误“无法读取未定义的属性 'put'”:
write(key, value) {
return this.cache.put(this.request(key), this.adapter.newResponse(JSON.stringify(value)));
}
我正在尝试缓存来自 webapi 的请求并在 ngsw-config.json - 文件中配置它们
{
"$schema": "./node_modules/@angular/service-worker/config/schema.json",
"index": "/index.html",
"dataGroups": [
{
"name": "api-freshness",
"urls": [
"/odata/**"
],
"cacheConfig": {
"strategy": "freshness",
"maxSize": 500,
"maxAge": "3d",
"timeout": "2s"
}
}
],
"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)"
]
}
}
]
}
首先,manifest.webmanifest 文件使用了错误的内容类型,所以我在我的 startup.cs 中添加了一个提供程序。但这没有帮助。
provider.Mappings[".webmanifest"] = "application/manifest+json";
app.UseSpaStaticFiles(new StaticFileOptions() {
ContentTypeProvider = provider
});
【问题讨论】:
-
为什么需要这条线?
provider.Mappings[".webmanifest"] = "application/manifest+json"; -
因为 ASP.Net 核心服务的清单文件内容类型错误(文本/html)
-
清单文件与 Service Worker 缓存无关。您可以在没有清单文件的情况下注册服务工作者和缓存。
标签: angular asp.net-core service-worker progressive-web-apps