Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
feat(service-worker): finish implementation of refreshAhead feature
Copy and document the refreshAhead option that allows to refresh cache entries before they expire.
This allows to mark cached entries as stale while still retruning them until maxAge in case of service outage.

Closes #46729
  • Loading branch information
yelhouti authored and thePunderWoman committed Oct 9, 2024
commit 7e3f2e065e737a252e69fda37107c3ae99367974
17 changes: 17 additions & 0 deletions adev/src/content/ecosystem/service-workers/config.md
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,7 @@ export interface DataGroup {
maxSize: number;
maxAge: string;
timeout?: string;
refreshAhead?: string;
strategy?: 'freshness' | 'performance';
};
cacheQueryOptions?: {
Expand Down Expand Up @@ -270,6 +271,22 @@ The network timeout is how long the Angular service worker waits for the network

For example, the string `5s30u` translates to five seconds and 30 milliseconds of network timeout.


##### `refreshAhead`

This duration string specifies the time ahead of the expiration of a cached resource when the Angular service worker should proactively attempt to refresh the resource from the network.
The `refreshAhead` duration is an optional configuration that determines how much time before the expiration of a cached response the service worker should initiate a request to refresh the resource from the network.

| Suffixes | Details |
|:--- |:--- |
| `d` | Days |
| `h` | Hours |
| `m` | Minutes |
| `s` | Seconds |
| `u` | Milliseconds |

For example, the string `1h30m` translates to one hour and 30 minutes ahead of the expiration time.

##### `strategy`

The Angular service worker can use either of two caching strategies for data resources.
Expand Down
1 change: 1 addition & 0 deletions goldens/public-api/service-worker/config/index.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ export interface DataGroup {
maxSize: number;
maxAge: Duration;
timeout?: Duration;
refreshAhead?: Duration;
strategy?: 'freshness' | 'performance';
cacheOpaqueResponses?: boolean;
};
Expand Down
4 changes: 4 additions & 0 deletions packages/service-worker/config/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,10 @@
"type": "string",
"description": "This duration string specifies the network timeout. The network timeout is how long the Angular service worker will wait for the network to respond before using a cached response, if configured to do so. 'timeout' is a duration string, using the following unit suffixes: d= days, h= hours, m= minutes, s= seconds, u= milliseconds. For example, the string '5s30u' will translate to five seconds and 30 milliseconds of network timeout."
},
"refreshAhead": {
"type": "string",
"description": "This duration string specifies the time ahead of the expiration of a cached resource when the Angular service worker should proactively attempt to refresh the resource from the network. The `refreshAhead` duration is an optional configuration that determines how much time before the expiration of a cached response the service worker should initiate a request to refresh the resource from the network."
},
"strategy": {
"enum": [
"freshness",
Expand Down
2 changes: 2 additions & 0 deletions packages/service-worker/config/src/generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,8 @@ export class Generator {
maxSize: group.cacheConfig.maxSize,
maxAge: parseDurationToMs(group.cacheConfig.maxAge),
timeoutMs: group.cacheConfig.timeout && parseDurationToMs(group.cacheConfig.timeout),
refreshAheadMs:
group.cacheConfig.refreshAhead && parseDurationToMs(group.cacheConfig.refreshAhead),
cacheOpaqueResponses: group.cacheConfig.cacheOpaqueResponses,
cacheQueryOptions: buildCacheQueryOptions(group.cacheQueryOptions),
version: group.version !== undefined ? group.version : 1,
Expand Down
1 change: 1 addition & 0 deletions packages/service-worker/config/src/in.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ export interface DataGroup {
maxSize: number;
maxAge: Duration;
timeout?: Duration;
refreshAhead?: Duration;
strategy?: 'freshness' | 'performance';
cacheOpaqueResponses?: boolean;
};
Expand Down
9 changes: 9 additions & 0 deletions packages/service-worker/config/test/generator_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ describe('Generator', () => {
maxAge: 259200000,
timeoutMs: 60000,
version: 1,
refreshAheadMs: undefined,
cacheOpaqueResponses: undefined,
cacheQueryOptions: {ignoreVary: true},
},
Expand Down Expand Up @@ -351,6 +352,7 @@ describe('Generator', () => {
maxSize: 100,
maxAge: 259200000,
timeoutMs: undefined,
refreshAheadMs: undefined,
version: 1,
cacheOpaqueResponses: undefined,
cacheQueryOptions: {ignoreVary: true},
Expand All @@ -362,6 +364,7 @@ describe('Generator', () => {
maxSize: 100,
maxAge: 259200000,
timeoutMs: undefined,
refreshAheadMs: undefined,
version: 1,
cacheOpaqueResponses: false,
cacheQueryOptions: {ignoreVary: true},
Expand All @@ -373,6 +376,7 @@ describe('Generator', () => {
maxSize: 100,
maxAge: 259200000,
timeoutMs: undefined,
refreshAheadMs: undefined,
version: 1,
cacheOpaqueResponses: true,
cacheQueryOptions: {ignoreVary: true},
Expand All @@ -384,6 +388,7 @@ describe('Generator', () => {
maxSize: 100,
maxAge: 259200000,
timeoutMs: undefined,
refreshAheadMs: undefined,
version: 1,
cacheOpaqueResponses: undefined,
cacheQueryOptions: {ignoreVary: true},
Expand All @@ -395,6 +400,7 @@ describe('Generator', () => {
maxSize: 100,
maxAge: 259200000,
timeoutMs: undefined,
refreshAheadMs: undefined,
version: 1,
cacheOpaqueResponses: false,
cacheQueryOptions: {ignoreVary: true},
Expand All @@ -406,6 +412,7 @@ describe('Generator', () => {
maxSize: 100,
maxAge: 259200000,
timeoutMs: undefined,
refreshAheadMs: undefined,
version: 1,
cacheOpaqueResponses: true,
cacheQueryOptions: {ignoreVary: true},
Expand Down Expand Up @@ -448,6 +455,7 @@ describe('Generator', () => {
maxSize: 100,
strategy: 'performance',
timeout: '1m',
refreshAhead: '1h',
},
cacheQueryOptions: {ignoreSearch: false},
},
Expand Down Expand Up @@ -477,6 +485,7 @@ describe('Generator', () => {
maxSize: 100,
maxAge: 259200000,
timeoutMs: 60000,
refreshAheadMs: 3600000,
version: 1,
cacheOpaqueResponses: undefined,
cacheQueryOptions: {ignoreSearch: false, ignoreVary: true},
Expand Down