Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 8d1e979

Browse files
SkyZeroZxthePunderWoman
authored andcommitted
docs: add references to SSR guide for caching options (#64347)
PR Close #64347
1 parent bc9c814 commit 8d1e979

File tree

2 files changed

+82
-7
lines changed

2 files changed

+82
-7
lines changed

‎adev/src/content/guide/ssr.md‎

Lines changed: 78 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -330,20 +330,87 @@ To configure this, update your `angular.json` file as follows:
330330

331331
## Caching data when using HttpClient
332332

333-
[`HttpClient`](api/common/http/HttpClient) cached outgoing network requests when running on the server. This information is serialized and transferred to the browser as part of the initial HTML sent from the server. In the browser, `HttpClient` checks whether it has data in the cache and if so, reuses it instead of making a new HTTP request during initial application rendering. `HttpClient` stops using the cache once an application becomes [stable](api/core/ApplicationRef#isStable) while running in a browser.
333+
`HttpClient` caches outgoing network requests when running on the server. This information is serialized and transferred to the browser as part of the initial HTML sent from the server. In the browser, `HttpClient` checks whether it has data in the cache and if so, reuses it instead of making a new HTTP request during initial application rendering. `HttpClient` stops using the cache once an application becomes [stable](api/core/ApplicationRef#isStable) while running in a browser.
334334

335-
By default, `HttpClient` caches all `HEAD` and `GET` requests which don't contain `Authorization` or `Proxy-Authorization` headers. You can override those settings by using [`withHttpTransferCacheOptions`](api/platform-browser/withHttpTransferCacheOptions) when providing hydration.
335+
### Configuring the caching options
336+
337+
You can customize how Angular caches HTTP responses during server‑side rendering (SSR) and reuses them during hydration by configuring `HttpTransferCacheOptions`.
338+
This configuration is provided globally using `withHttpTransferCacheOptions` inside `provideClientHydration()`.
339+
340+
By default, `HttpClient` caches all `HEAD` and `GET` requests which don't contain `Authorization` or `Proxy-Authorization` headers. You can override those settings by using `withHttpTransferCacheOptions` to the hydration configuration.
341+
342+
```ts
343+
import { bootstrapApplication } from '@angular/platform-browser';
344+
import { provideClientHydration, withHttpTransferCacheOptions } from '@angular/platform-browser';
336345

337-
```typescript
338346
bootstrapApplication(AppComponent, {
339347
providers: [
340-
provideClientHydration(withHttpTransferCacheOptions({
341-
includePostRequests: true
342-
}))
343-
]
348+
provideClientHydration(
349+
withHttpTransferCacheOptions({
350+
includeHeaders: ['ETag', 'Cache-Control'],
351+
filter: (req) => !req.url.includes('/api/profile'),
352+
includePostRequests: true,
353+
includeRequestsWithAuthHeaders: false,
354+
}),
355+
),
356+
],
357+
});
358+
```
359+
360+
---
361+
362+
### `includeHeaders`
363+
364+
Specifies which headers from the server response should be included in cached entries.
365+
No headers are included by default.
366+
367+
```ts
368+
withHttpTransferCacheOptions({
369+
includeHeaders: ['ETag', 'Cache-Control'],
370+
});
371+
```
372+
373+
IMPORTANT: Avoid including sensitive headers like authentication tokens. These can leak user‑specific data between requests.
374+
375+
---
376+
377+
### `includePostRequests`
378+
379+
By default, only `GET` and `HEAD` requests are cached.
380+
You can enable caching for `POST` requests when they are used as read operations such as GraphQL queries.
381+
382+
```ts
383+
withHttpTransferCacheOptions({
384+
includePostRequests: true,
385+
});
386+
```
387+
388+
Use this only when `POST` requests are **idempotent** and safe to reuse between server and client renders.
389+
390+
---
391+
392+
### `includeRequestsWithAuthHeaders`
393+
394+
Determines whether requests containing `Authorization` or `Proxy‑Authorization` headers are eligible for caching.
395+
By default, these are excluded to prevent caching user‑specific responses.
396+
397+
```ts
398+
withHttpTransferCacheOptions({
399+
includeRequestsWithAuthHeaders: true,
344400
});
345401
```
346402

403+
Enable only when authentication headers do **not** affect the response content (for example, public tokens for analytics APIs).
404+
405+
### Per‑request overrides
406+
407+
You can override caching behavior for a specific request using the `transferCache` request option.
408+
409+
```ts
410+
// Include specific headers for this request
411+
http.get('/api/profile', { transferCache: { includeHeaders: ['CustomHeader'] } });
412+
```
413+
347414
### Disabling caching
348415

349416
You can disable HTTP caching of requests sent from the server either globally or individually.
@@ -362,6 +429,8 @@ bootstrapApplication(AppComponent, {
362429
});
363430
```
364431

432+
#### `filter`
433+
365434
You can also selectively disable caching for certain requests using the [`filter`](api/common/http/HttpTransferCacheOptions) option in `withHttpTransferCacheOptions`. For example, you can disable caching for a specific API endpoint:
366435

367436
```ts
@@ -376,6 +445,8 @@ bootstrapApplication(AppComponent, {
376445
});
377446
```
378447

448+
Use this option to exclude endpoints with user‑specific or dynamic data (for example `/api/profile`).
449+
379450
#### Individually
380451

381452
To disable caching for an individual request, you can specify the [`transferCache`](api/common/http/HttpRequest#transferCache) option in an `HttpRequest`.

‎packages/platform-browser/src/hydration.ts‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,8 @@ function hydrationFeature<FeatureKind extends HydrationFeatureKind>(
6565
* Disables HTTP transfer cache. Effectively causes HTTP requests to be performed twice: once on the
6666
* server and other one on the browser.
6767
*
68+
* @see [Disabling Caching](guide/ssr#disabling-caching)
69+
*
6870
* @publicApi
6971
*/
7072
export function withNoHttpTransferCache(): HydrationFeature<HydrationFeatureKind.NoHttpTransferCache> {
@@ -79,6 +81,8 @@ export function withNoHttpTransferCache(): HydrationFeature<HydrationFeatureKind
7981
* whether POST requests should be cached or a callback function to determine if a
8082
* particular request should be cached.
8183
*
84+
* @see [Configuring HTTP transfer cache options](guide/ssr#caching-data-when-using-httpclient)
85+
*
8286
* @publicApi
8387
*/
8488
export function withHttpTransferCacheOptions(

0 commit comments

Comments
 (0)