使用相对 URL 时如何使用 HTTP 传输状态
How to use HTTP Transfer State when using relative URLs
我正在尝试实现内置 TransferHttpCacheModule 以消除重复请求。我在我的应用程序中使用这个拦截器:
intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
const authService = this.injector.get(AuthenticationService);
const url = `${this.request ? this.request.protocol + '://' + this.request.get('host') : ''}${environment.baseBackendUrl}${req.url}`
let headers = new HttpHeaders();
if (this.request) {
// Server side: forward the cookies
const cookies = this.request.cookies;
const cookiesArray = [];
for (const name in cookies) {
if (cookies.hasOwnProperty(name)) {
cookiesArray.push(`${name}=${cookies[name]}`);
}
}
headers = headers.append('Cookie', cookiesArray.join('; '));
}
headers = headers.append('Content-Type', 'application/json');
const finalReq: HttpRequest<any> = req.clone({ url, headers });
...
它为客户端启用相对 URLs,为服务器端启用完整 URLs,因为服务器不知道它自己的 URL。
问题是TransferHttpCacheModule
根据方法、URL和参数使用key,服务器URL与客户端不匹配URLs.
有没有办法强制TransferHttpCacheInterceptor
在我自己的拦截器之前执行?我想避免在客户端强制使用完整的 URLs。
您可以将拦截器放在它自己的模块中:
@NgModule({
providers: [
{ provide: HTTP_INTERCEPTORS, useClass: MyOwnInterceptor, multi: true }
]
})
export class MyOwnInterceptorModule {}
然后您可以将此模块放在 AppModule 中 TransferHttpCacheModule
的导入下方:
@NgModule({
imports: [
// ...
TransferHttpCacheModule,
MyOwnInterceptorModule
],
// ...
})
export class AppModule {}
这样您的拦截器将在 TransferHttpCacheInterceptor
之后应用。不过感觉很奇怪,因为据我所知,首先是导入,然后是提供者。这样您就可以覆盖导入的提供程序。您确定不想反过来吗?
我在 angularspree
的 Angular 普遍支持方面遇到了同样的问题
我遵循了这些方法:
=> 创建一个 TransferStateService,公开函数来设置和获取缓存数据。
import { Inject, Injectable, PLATFORM_ID } from '@angular/core';
import { TransferState, makeStateKey } from '@angular/platform-browser';
import { isPlatformBrowser } from '@angular/common';
/**
* Keep caches (makeStateKey) into it in each `setCache` function call
* @type {any[]}
*/
const transferStateCache: String[] = [];
@Injectable()
export class TransferStateService {
constructor(private transferState: TransferState,
@Inject(PLATFORM_ID) private platformId: Object,
// @Inject(APP_ID) private _appId: string
) {
}
/**
* Set cache only when it's running on server
* @param {string} key
* @param data Data to store to cache
*/
setCache(key: string, data: any) {
if (!isPlatformBrowser(this.platformId)) {
transferStateCache[key] = makeStateKey<any>(key);
this.transferState.set(transferStateCache[key], data);
}
}
/**
* Returns stored cache only when it's running on browser
* @param {string} key
* @returns {any} cachedData
*/
getCache(key: string): any {
if (isPlatformBrowser(this.platformId)) {
const cachedData: any = this.transferState['store'][key];
/**
* Delete the cache to request the data from network next time which is the
* user's expected behavior
*/
delete this.transferState['store'][key];
return cachedData;
}
}
}
=> 创建一个TransferStateInterceptor 来拦截服务器端平台上的请求。
import { tap } from 'rxjs/operators';
import { Observable, of } from 'rxjs';
import { Injectable } from '@angular/core';
import {
HttpRequest,
HttpHandler,
HttpEvent,
HttpInterceptor,
HttpResponse
} from '@angular/common/http';
import { TransferStateService } from '../services/transfer-state.service';
@Injectable()
export class TransferStateInterceptor implements HttpInterceptor {
constructor(private transferStateService: TransferStateService) {
}
intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
/**
* Skip this interceptor if the request method isn't GET.
*/
if (req.method !== 'GET') {
return next.handle(req);
}
const cachedResponse = this.transferStateService.getCache(req.url);
if (cachedResponse) {
// A cached response exists which means server set it before. Serve it instead of forwarding
// the request to the next handler.
return of(new HttpResponse<any>({ body: cachedResponse }));
}
/**
* No cached response exists. Go to the network, and cache
* the response when it arrives.
*/
return next.handle(req).pipe(
tap(event => {
if (event instanceof HttpResponse) {
this.transferStateService.setCache(req.url, event.body);
}
})
);
}
}
=> 将其添加到 module 中的提供商部分。
providers: [
{provide: HTTP_INTERCEPTORS, useClass: TransferStateInterceptor, multi: true},
TransferStateService,
]
我遇到了同样的问题,通过删除 makeStateKey 中的主机解决了这个问题。
你的OwnHttpInterceptor
你可以改变这个
const key: StateKey<string> = makeStateKey<string>(request.url);
至此
const key: StateKey<string> = makeStateKey<string>(request.url.split("/api").pop());
我正在尝试实现内置 TransferHttpCacheModule 以消除重复请求。我在我的应用程序中使用这个拦截器:
intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
const authService = this.injector.get(AuthenticationService);
const url = `${this.request ? this.request.protocol + '://' + this.request.get('host') : ''}${environment.baseBackendUrl}${req.url}`
let headers = new HttpHeaders();
if (this.request) {
// Server side: forward the cookies
const cookies = this.request.cookies;
const cookiesArray = [];
for (const name in cookies) {
if (cookies.hasOwnProperty(name)) {
cookiesArray.push(`${name}=${cookies[name]}`);
}
}
headers = headers.append('Cookie', cookiesArray.join('; '));
}
headers = headers.append('Content-Type', 'application/json');
const finalReq: HttpRequest<any> = req.clone({ url, headers });
...
它为客户端启用相对 URLs,为服务器端启用完整 URLs,因为服务器不知道它自己的 URL。
问题是TransferHttpCacheModule
根据方法、URL和参数使用key,服务器URL与客户端不匹配URLs.
有没有办法强制TransferHttpCacheInterceptor
在我自己的拦截器之前执行?我想避免在客户端强制使用完整的 URLs。
您可以将拦截器放在它自己的模块中:
@NgModule({
providers: [
{ provide: HTTP_INTERCEPTORS, useClass: MyOwnInterceptor, multi: true }
]
})
export class MyOwnInterceptorModule {}
然后您可以将此模块放在 AppModule 中 TransferHttpCacheModule
的导入下方:
@NgModule({
imports: [
// ...
TransferHttpCacheModule,
MyOwnInterceptorModule
],
// ...
})
export class AppModule {}
这样您的拦截器将在 TransferHttpCacheInterceptor
之后应用。不过感觉很奇怪,因为据我所知,首先是导入,然后是提供者。这样您就可以覆盖导入的提供程序。您确定不想反过来吗?
我在 angularspree
的 Angular 普遍支持方面遇到了同样的问题我遵循了这些方法:
=> 创建一个 TransferStateService,公开函数来设置和获取缓存数据。
import { Inject, Injectable, PLATFORM_ID } from '@angular/core';
import { TransferState, makeStateKey } from '@angular/platform-browser';
import { isPlatformBrowser } from '@angular/common';
/**
* Keep caches (makeStateKey) into it in each `setCache` function call
* @type {any[]}
*/
const transferStateCache: String[] = [];
@Injectable()
export class TransferStateService {
constructor(private transferState: TransferState,
@Inject(PLATFORM_ID) private platformId: Object,
// @Inject(APP_ID) private _appId: string
) {
}
/**
* Set cache only when it's running on server
* @param {string} key
* @param data Data to store to cache
*/
setCache(key: string, data: any) {
if (!isPlatformBrowser(this.platformId)) {
transferStateCache[key] = makeStateKey<any>(key);
this.transferState.set(transferStateCache[key], data);
}
}
/**
* Returns stored cache only when it's running on browser
* @param {string} key
* @returns {any} cachedData
*/
getCache(key: string): any {
if (isPlatformBrowser(this.platformId)) {
const cachedData: any = this.transferState['store'][key];
/**
* Delete the cache to request the data from network next time which is the
* user's expected behavior
*/
delete this.transferState['store'][key];
return cachedData;
}
}
}
=> 创建一个TransferStateInterceptor 来拦截服务器端平台上的请求。
import { tap } from 'rxjs/operators';
import { Observable, of } from 'rxjs';
import { Injectable } from '@angular/core';
import {
HttpRequest,
HttpHandler,
HttpEvent,
HttpInterceptor,
HttpResponse
} from '@angular/common/http';
import { TransferStateService } from '../services/transfer-state.service';
@Injectable()
export class TransferStateInterceptor implements HttpInterceptor {
constructor(private transferStateService: TransferStateService) {
}
intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
/**
* Skip this interceptor if the request method isn't GET.
*/
if (req.method !== 'GET') {
return next.handle(req);
}
const cachedResponse = this.transferStateService.getCache(req.url);
if (cachedResponse) {
// A cached response exists which means server set it before. Serve it instead of forwarding
// the request to the next handler.
return of(new HttpResponse<any>({ body: cachedResponse }));
}
/**
* No cached response exists. Go to the network, and cache
* the response when it arrives.
*/
return next.handle(req).pipe(
tap(event => {
if (event instanceof HttpResponse) {
this.transferStateService.setCache(req.url, event.body);
}
})
);
}
}
=> 将其添加到 module 中的提供商部分。
providers: [
{provide: HTTP_INTERCEPTORS, useClass: TransferStateInterceptor, multi: true},
TransferStateService,
]
我遇到了同样的问题,通过删除 makeStateKey 中的主机解决了这个问题。
你的OwnHttpInterceptor
你可以改变这个
const key: StateKey<string> = makeStateKey<string>(request.url);
至此
const key: StateKey<string> = makeStateKey<string>(request.url.split("/api").pop());