fix: add url for cached response (#1355)

This commit is contained in:
Yichang Liu 2018-07-17 04:41:40 +08:00 committed by Martijn Walraven
parent 3b3c50a1ab
commit cd1a4630f8
3 changed files with 14 additions and 2 deletions

View file

@ -37,7 +37,11 @@ export class HTTPCache {
if (policy.satisfiesWithoutRevalidation(policyRequestFrom(request))) {
const headers = policy.responseHeaders();
return new Response(body, { status: policy._status, headers });
return new Response(body, {
url: policy._url,
status: policy._status,
headers,
});
} else {
const revalidationHeaders = policy.revalidationHeaders(
policyRequestFrom(request),
@ -57,6 +61,7 @@ export class HTTPCache {
modified
? revalidationResponse
: new Response(body, {
url: revalidatedPolicy._url,
status: revalidatedPolicy._status,
headers: revalidatedPolicy.responseHeaders(),
}),
@ -91,6 +96,7 @@ export class HTTPCache {
// To avoid https://github.com/bitinn/node-fetch/issues/151, we don't use
// response.clone() but create a new response from the consumed body
return new Response(body, {
url: response.url,
status: response.status,
statusText: response.statusText,
headers: policy.responseHeaders(),

View file

@ -31,6 +31,7 @@ declare module 'http-cache-semantics' {
static fromObject(object: object): CachePolicy;
toObject(): object;
_url: string;
_status: number;
}

View file

@ -78,7 +78,8 @@ export type ReferrerPolicy =
| 'unsafe-url';
export declare class Response extends Body {
constructor(body?: BodyInit, init?: ResponseInit);
// node-fetch accepts options as the second argument instead of a pure ResponseInit
constructor(body?: BodyInit, options?: ResponseOptions);
static error(): Response;
static redirect(url: string, status?: number): Response;
@ -98,4 +99,8 @@ export interface ResponseInit {
statusText?: string;
}
export interface ResponseOptions extends ResponseInit {
url?: string;
}
export type BodyInit = ArrayBuffer | ArrayBufferView | string;