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

Skip to content

Commit 8cb8673

Browse files
committed
docs(isr): add documentation for the cache variant feature
1 parent bc7bf5f commit 8cb8673

File tree

1 file changed

+115
-0
lines changed

1 file changed

+115
-0
lines changed

apps/docs/docs/isr/cache-variants.md

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
---
2+
sidebar_label: Cache Variants
3+
sidebar_position: 11
4+
title: Cache Variants
5+
---
6+
7+
## Why do we need it?
8+
9+
In some apps, pages are displayed in different variants. This can be the case, for example, if the application has a login. Users who are already logged in may not see a login button or something similar, whereas an authenticated user will.
10+
11+
These scenarios would lead to a content shift after delivery of the cached variant that does not correspond to the user status.
12+
13+
## How to use it?
14+
15+
For these cases the configuration can be extended by any number of render variants by the `variants` property.
16+
17+
```typescript title="server.ts"
18+
const isr = new ISRHandler({
19+
...
20+
// highlight-next-line
21+
variants: [] // 👈 register your variants here
22+
});
23+
```
24+
25+
The `RenderVariant` interface is defined as follows:
26+
27+
```typescript
28+
export interface RenderVariant {
29+
identifier: string;
30+
detectVariant: (req: Request) => boolean;
31+
simulateVariant?: (req: Request) => Request;
32+
}
33+
```
34+
35+
### identifier
36+
37+
The `identifier` must be unique and is used next to the URL as a key for the cache.
38+
39+
### detectVariant
40+
41+
The `detectVariant` callback detects from the current request whether a defined variant must be delivered. This can happen e.g. on the basis of a cookie or similar. The return value is a boolean which determines whether this request fits the variant or not.
42+
43+
### simulateVariant
44+
45+
If on-demand revalidation is also to be used for the different variants, a way must be provided to modify the request so that it can be recognized again by the `detectVariant` callback.
46+
47+
For example, a placeholder for an authentication cookie can be added so that an authenticated variant of the app is rendered.
48+
For this we use the `simulateVariant` callback, which is called before rendering to modify the request.
49+
50+
### Example
51+
52+
```typescript title="server.ts"
53+
const isr = new ISRHandler({
54+
...
55+
variants: [
56+
{
57+
identifier: 'logged-in', // 👈 key to cache the variant
58+
detectVariant: (req) => {
59+
// 👇 Variant is recognized as soon as the request contains a cookie 'access_token'
60+
return req.cookies && req.cookies.access_token;
61+
},
62+
simulateVariant: (req) => {
63+
// 👇 For on-demand revalidation we insert a placeholder 'access_token' cookie
64+
req.cookies['access_token'] = 'isr';
65+
return req;
66+
}
67+
},
68+
]
69+
});
70+
```
71+
72+
## Gotchas
73+
74+
If more than one variant is registered, the one where the `detectVariant` returns `true` the first time is used.
75+
76+
If a variant is to become a combination of different variants, this state should be registered as an independent variant before the corresponding single variant.
77+
78+
```typescript title="server.ts"
79+
const isr = new ISRHandler({
80+
...
81+
variants: [
82+
{
83+
identifier: 'A_AND_B',
84+
detectVariant: (req) => req.cookies && req.cookies.is_A && req.cookies.is_B,
85+
simulateVariant: (req) => {
86+
req.cookies['is_A'] = true;
87+
req.cookies['is_B'] = true;
88+
return req;
89+
}
90+
},
91+
{
92+
identifier: 'A',
93+
detectVariant: (req) => req.cookies && req.cookies.is_A,
94+
simulateVariant: (req) => {
95+
req.cookies['is_A'] = true;
96+
return req;
97+
}
98+
},
99+
{
100+
identifier: 'B',
101+
detectVariant: (req) => req.cookies && req.cookies.is_B,
102+
simulateVariant: (req) => {
103+
req.cookies['is_B'] = true;
104+
return req;
105+
}
106+
},
107+
]
108+
});
109+
```
110+
111+
:::caution **Important:**
112+
For each variant entered here, a corresponding duplicate is cached per page. So be aware that depending on the cache handler this could significantly increase the disk or RAM usage.
113+
:::
114+
115+
Note that when using placeholder cookies or similar, the application should have appropriate exceptions defined to avoid runtime errors caused by invalid values.

0 commit comments

Comments
 (0)