我创建了一个站点,它也可以用作 Angular 8 的 PWA。我也将 Angular Universal 用于 SSR。它可以工作,也可以离线。问题是只有站点被缓存,而不是服务器请求。我将其添加dataGroups
到 ngsw 设置中。
这些是我的ngsw-config.json
{
"$schema": "./node_modules/@angular/service-worker/config/schema.json",
"index": "/index.html",
"dataGroups": [
{
"name": "exercises-requests",
"urls": [
"/api/provide-exercise"
],
"cacheConfig": {
"strategy": "performance",
"maxSize": 100,
"maxAge": "3d"
}
}
],
"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)"
]
}
}
]
}
这是我的http 服务:
import { Injectable, isDevMode } from '@angular/core';
import { HttpClient, HttpHeaders, HttpErrorResponse } from '@angular/common/http';
import { throwError, Observable } from 'rxjs';
import { catchError } from 'rxjs/operators';
import { Input } from '../exercise/exercise.service';
export interface UserInfo {
// ...
}
export interface ProvideExerciseBody {
// ...
}
export interface ProvideExerciseResponse {
// ...
}
const httpOptions = {
headers: new HttpHeaders({
'Content-Type': 'application/json'
})
};
@Injectable({
providedIn: 'root',
})
export class HttpService {
get
serverDomain(): string {
return isDevMode() ? 'https://unitn-statistica.herokuapp.com' : '';
}
constructor(private http: HttpClient) { }
provideExercise(body: ProvideExerciseBody): Observable<ProvideExerciseResponse> {
return this.http.post<ProvideExerciseResponse>(this.serverDomain + '/api/provide-exercise', body, httpOptions)
.pipe(
catchError(this.handleError)
);
}
private handleError(err: HttpErrorResponse) {
const error = err.error;
if (error instanceof ErrorEvent) {
// A client-side or network error occurred. Handle it accordingly.
console.error('An error occurred:', error.message);
console.error(error.message);
} else {
// The backend returned an unsuccessful response code.
// The response body may contain clues as to what went wrong,
console.error(
`Backend returned code ${err.status}, ` +
`body was: ${error}`);
console.log(error);
}
// return an observable with a user-facing error message
return throwError(error.message);
}
}
而且我确信服务器调用是正确的
app.post('/api/provide-exercise', (req, res) => { ... }
我不明白为什么我对服务器的发布请求没有被缓存。