| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 45e8fb5 commit 8ec0197
3 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -432,7 +432,7 @@ To configure this, update your `angular.json` file as follows: | |||
| 432 | 432 | You can customize how Angular caches HTTP responses during server‑side rendering (SSR) and reuses them during hydration by configuring `HttpTransferCacheOptions`. | |
| 433 | 433 | This configuration is provided globally using `withHttpTransferCacheOptions` inside `provideClientHydration()`. | |
| 434 | 434 | ||
| 435 | - By default, `HttpClient` caches all `HEAD` and `GET` requests which don't contain `Authorization`, `Proxy-Authorization`, or `Cookie` headers and are not sent with `withCredentials`. You can override those settings by using `withHttpTransferCacheOptions` to the hydration configuration. | ||
| 435 | + By default, `HttpClient` caches all `HEAD` and `GET` requests which don't contain `Authorization`, `Proxy-Authorization`, or `Cookie` headers and are not sent with `withCredentials` or Fetch API `credentials` modes that can send credentials. You can override those settings by using `withHttpTransferCacheOptions` to the hydration configuration. | ||
| 436 | 436 | ||
| 437 | 437 | ```ts | |
| 438 | 438 | import {bootstrapApplication} from '@angular/platform-browser'; | |
@@ -487,7 +487,7 @@ Use this only when `POST` requests are **idempotent** and safe to reuse between | |||
| 487 | 487 | ### `includeRequestsWithAuthHeaders` | |
| 488 | 488 | ||
| 489 | 489 | Determines whether requests containing `Authorization`, `Proxy‑Authorization`, or `Cookie` headers are eligible for caching. | |
| 490 | - By default, these are excluded to prevent caching user‑specific responses. Requests sent with `withCredentials` are also excluded by default. | ||
| 490 | + By default, these are excluded to prevent caching user‑specific responses. Requests sent with `withCredentials` or Fetch API `credentials` set to `include` or `same-origin` are also excluded by default. | ||
| 491 | 491 | ||
| 492 | 492 | ```ts | |
| 493 | 493 | withHttpTransferCacheOptions({ | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -42,7 +42,8 @@ import {HttpParams} from './params'; | |||
| 42 | 42 | * (for example using GraphQL). | |
| 43 | 43 | * @param includeRequestsWithAuthHeaders Enables caching of requests containing `Authorization`, | |
| 44 | 44 | * `Proxy-Authorization`, or `Cookie` headers. By default, these requests are excluded from | |
| 45 | - * caching. Requests sent using `withCredentials` are also excluded by default. | ||
| 45 | + * caching. Requests sent using `withCredentials` or Fetch API `credentials` modes that can send | ||
| 46 | + * credentials are also excluded by default. | ||
| 46 | 47 | * | |
| 47 | 48 | * @see [Configuring the caching options](guide/ssr#configuring-the-caching-options) | |
| 48 | 49 | * | |
@@ -132,7 +133,7 @@ function shouldCacheRequest(req: HttpRequest<unknown>, options: CacheOptions): b | |||
| 132 | 133 | !isCacheActive || | |
| 133 | 134 | requestOptions === false || | |
| 134 | 135 | // Do not cache requests sent with credentials. | |
| 135 | - req.withCredentials || | ||
| 136 | + hasOutgoingCredentials(req) || | ||
| 136 | 137 | // POST requests are allowed either globally or at request level | |
| 137 | 138 | (requestMethod === 'POST' && !globalOptions.includePostRequests && !requestOptions) || | |
| 138 | 139 | (requestMethod !== 'POST' && !ALLOWED_METHODS.includes(requestMethod)) || | |
@@ -300,6 +301,10 @@ function hasAuthHeaders(req: HttpRequest<unknown>): boolean { | |||
| 300 | 301 | ); | |
| 301 | 302 | } | |
| 302 | 303 | ||
| 304 | + function hasOutgoingCredentials(req: HttpRequest<unknown>): boolean { | ||
| 305 | + return req.withCredentials || req.credentials === 'include' || req.credentials === 'same-origin'; | ||
| 306 | + } | ||
| 307 | + | ||
| 303 | 308 | function getFilteredHeaders( | |
| 304 | 309 | headers: HttpHeaders, | |
| 305 | 310 | includeHeaders: string[] | undefined, | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -41,6 +41,7 @@ interface RequestParams { | |||
| 41 | 41 | transferCache?: {includeHeaders: string[]} | boolean; | |
| 42 | 42 | headers?: {[key: string]: string}; | |
| 43 | 43 | withCredentials?: boolean; | |
| 44 | + credentials?: RequestCredentials; | ||
| 44 | 45 | body?: RequestBody; | |
| 45 | 46 | } | |
| 46 | 47 | ||
@@ -440,6 +441,36 @@ describe('TransferCache', () => { | |||
| 440 | 441 | }); | |
| 441 | 442 | }); | |
| 442 | 443 | ||
| 444 | + it('should not cache requests with included credentials', async () => { | ||
| 445 | + makeRequestAndExpectOne('/test-auth', 'foo', { | ||
| 446 | + credentials: 'include', | ||
| 447 | + }); | ||
| 448 | + | ||
| 449 | + makeRequestAndExpectOne('/test-auth', 'foo', { | ||
| 450 | + credentials: 'include', | ||
| 451 | + }); | ||
| 452 | + }); | ||
| 453 | + | ||
| 454 | + it('should not cache requests with same-origin credentials', async () => { | ||
| 455 | + makeRequestAndExpectOne('/test-auth', 'foo', { | ||
| 456 | + credentials: 'same-origin', | ||
| 457 | + }); | ||
| 458 | + | ||
| 459 | + makeRequestAndExpectOne('/test-auth', 'foo', { | ||
| 460 | + credentials: 'same-origin', | ||
| 461 | + }); | ||
| 462 | + }); | ||
| 463 | + | ||
| 464 | + it('should cache requests with omitted credentials', async () => { | ||
| 465 | + makeRequestAndExpectOne('/test-auth', 'foo', { | ||
| 466 | + credentials: 'omit', | ||
| 467 | + }); | ||
| 468 | + | ||
| 469 | + makeRequestAndExpectNone('/test-auth', 'GET', { | ||
| 470 | + credentials: 'omit', | ||
| 471 | + }); | ||
| 472 | + }); | ||
| 473 | + | ||
| 443 | 474 | it('should cache POST with the differing body in string form', () => { | |
| 444 | 475 | makeRequestAndExpectOne('/test-1', null, {method: 'POST', transferCache: true, body: 'foo'}); | |
| 445 | 476 | makeRequestAndExpectNone('/test-1', 'POST', {transferCache: true, body: 'foo'}); | |
@@ -602,6 +633,16 @@ describe('TransferCache', () => { | |||
| 602 | 633 | }); | |
| 603 | 634 | }); | |
| 604 | 635 | ||
| 636 | + it(`should not cache requests with included credentials when 'includeRequestsWithAuthHeaders' is 'true'`, async () => { | ||
| 637 | + makeRequestAndExpectOne('/test-auth', 'foo', { | ||
| 638 | + credentials: 'include', | ||
| 639 | + }); | ||
| 640 | + | ||
| 641 | + makeRequestAndExpectOne('/test-auth', 'foo', { | ||
| 642 | + credentials: 'include', | ||
| 643 | + }); | ||
| 644 | + }); | ||
| 645 | + | ||
| 605 | 646 | it('should cache a POST request', () => { | |
| 606 | 647 | makeRequestAndExpectOne('/include?foo=1', 'post-body', {method: 'POST'}); | |
| 607 | 648 | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments