@tramvai/module-cache-warmup 6.68.6 → 6.77.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +36 -2
- package/lib/browser.d.ts +1 -0
- package/lib/browser.js +1 -0
- package/lib/server.d.ts +1 -0
- package/lib/server.es.js +63 -19
- package/lib/server.js +64 -18
- package/lib/tokens.browser.js +8 -0
- package/lib/tokens.d.ts +20 -0
- package/lib/tokens.es.js +8 -0
- package/lib/tokens.js +13 -0
- package/lib/utils.d.ts +5 -2
- package/lib/utils.es.js +6 -14
- package/lib/utils.js +6 -14
- package/lib/warmup.d.ts +7 -3
- package/lib/warmup.es.js +16 -5
- package/lib/warmup.js +16 -4
- package/package.json +7 -7
package/README.md
CHANGED
|
@@ -19,8 +19,42 @@ createApp({
|
|
|
19
19
|
|
|
20
20
|
> Module is executed only when `NODE_ENV === production`.
|
|
21
21
|
|
|
22
|
-
1. When app starts the module will request list of app urls from papi-route `
|
|
23
|
-
2. For every url from the response it sends
|
|
22
|
+
1. When app starts the module will request list of app urls from papi-route `prerenderRoutes`.
|
|
23
|
+
2. For every url from the response it sends two requests: one for mobile and one for desktop device. But only `1` requests are running simultaneously in total.
|
|
24
|
+
3. Routes with dynamic parameters, like `/blog/:id/`, will be skipped by default
|
|
25
|
+
|
|
26
|
+
### Routes with dynamic parameters
|
|
27
|
+
|
|
28
|
+
For example, you want to warmp up dynamic routes like `/blog/1/`, `/blog/2/` and `/blog/3/`. CacheWarmupModule will use the same urls list as `tramvai static` command, so you need to use the same mechanism - [custom `prerender:routes` hook](03-features/010-rendering/04-ssg.md#routes-with-dynamic-parameters).
|
|
29
|
+
|
|
30
|
+
### Filtering routes from warmup
|
|
31
|
+
|
|
32
|
+
If you want to exclude some routes from warmup, you can use `cache-warmup:request` hook for `CACHE_WARMUP_HOOKS_TOKEN` token:
|
|
33
|
+
|
|
34
|
+
```ts
|
|
35
|
+
const provider = provide({
|
|
36
|
+
provide: commandLineListTokens.listen,
|
|
37
|
+
useFactory: ({ hooks, logger }) => {
|
|
38
|
+
return async function filterWarmupRoutes() {
|
|
39
|
+
hooks['cache-warmup:request'].wrap(async (_, payload, next) => {
|
|
40
|
+
// Match request url or any other parameters, and skip it
|
|
41
|
+
if (payload.parameters.url?.includes('/some-unpopular-and-heavy-page')) {
|
|
42
|
+
return {
|
|
43
|
+
parameters: payload.parameters,
|
|
44
|
+
result: 'skipped',
|
|
45
|
+
};
|
|
46
|
+
}
|
|
47
|
+
// Otherwise, continue with the request
|
|
48
|
+
return next(payload);
|
|
49
|
+
});
|
|
50
|
+
};
|
|
51
|
+
},
|
|
52
|
+
deps: {
|
|
53
|
+
hooks: CACHE_WARMUP_HOOKS_TOKEN,
|
|
54
|
+
logger: LOGGER_TOKEN,
|
|
55
|
+
},
|
|
56
|
+
});
|
|
57
|
+
```
|
|
24
58
|
|
|
25
59
|
### User-agent
|
|
26
60
|
|
package/lib/browser.d.ts
CHANGED
package/lib/browser.js
CHANGED
package/lib/server.d.ts
CHANGED
package/lib/server.es.js
CHANGED
|
@@ -1,8 +1,10 @@
|
|
|
1
1
|
import { __decorate } from 'tslib';
|
|
2
|
-
import { Module, provide, commandLineListTokens } from '@tramvai/core';
|
|
2
|
+
import { Module, provide, commandLineListTokens, TAPABLE_HOOK_FACTORY_TOKEN } from '@tramvai/core';
|
|
3
3
|
import { PAPI_SERVICE } from '@tramvai/tokens-http-client';
|
|
4
4
|
import { LOGGER_TOKEN, ENV_MANAGER_TOKEN } from '@tramvai/module-common';
|
|
5
5
|
import { warmUpCache } from './warmup.es.js';
|
|
6
|
+
import { CACHE_WARMUP_ENABLED_TOKEN, CACHE_WARMUP_HOOKS_TOKEN } from './tokens.es.js';
|
|
7
|
+
export { CACHE_WARMUP_ENABLED_TOKEN, CACHE_WARMUP_HOOKS_TOKEN } from './tokens.es.js';
|
|
6
8
|
|
|
7
9
|
let CacheWarmupModule = class CacheWarmupModule {
|
|
8
10
|
};
|
|
@@ -10,35 +12,77 @@ CacheWarmupModule = __decorate([
|
|
|
10
12
|
Module({
|
|
11
13
|
imports: [],
|
|
12
14
|
providers: [
|
|
15
|
+
provide({
|
|
16
|
+
provide: CACHE_WARMUP_ENABLED_TOKEN,
|
|
17
|
+
useFactory: ({ papiService, logger }) => {
|
|
18
|
+
const log = logger('cache-warmup');
|
|
19
|
+
if (!papiService) {
|
|
20
|
+
log.info('Skip cache warmup when @tramvai/module-http-client is not enabled');
|
|
21
|
+
return false;
|
|
22
|
+
}
|
|
23
|
+
if (process.env.CACHE_WARMUP_DISABLED === 'true') {
|
|
24
|
+
log.info('Skip cache warm up due to CACHE_WARMUP_DISABLED env');
|
|
25
|
+
return false;
|
|
26
|
+
}
|
|
27
|
+
if (process.env.NODE_ENV !== 'production') {
|
|
28
|
+
log.info('Skip cache warm up in dev environment');
|
|
29
|
+
return false;
|
|
30
|
+
}
|
|
31
|
+
// TODO: revisit this condition
|
|
32
|
+
// if (process.env.MOCKER_ENABLED === 'true') {
|
|
33
|
+
// log.info('Skip cache warm up when mocker is enabled');
|
|
34
|
+
// return false;
|
|
35
|
+
// }
|
|
36
|
+
return true;
|
|
37
|
+
},
|
|
38
|
+
deps: {
|
|
39
|
+
papiService: { token: PAPI_SERVICE, optional: true },
|
|
40
|
+
logger: LOGGER_TOKEN,
|
|
41
|
+
},
|
|
42
|
+
}),
|
|
13
43
|
provide({
|
|
14
44
|
provide: commandLineListTokens.listen,
|
|
15
45
|
multi: true,
|
|
16
|
-
useFactory({ papiService, logger, environmentManager }) {
|
|
17
|
-
return ()
|
|
18
|
-
|
|
19
|
-
if (!papiService) {
|
|
20
|
-
log.info('Skip cache warmup when @tramvai/module-http-client is not enabled');
|
|
46
|
+
useFactory({ papiService, logger, environmentManager, hooks, cacheWarmupEnabled }) {
|
|
47
|
+
return function cacheWarmup() {
|
|
48
|
+
if (!cacheWarmupEnabled) {
|
|
21
49
|
return;
|
|
22
50
|
}
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
51
|
+
hooks['cache-warmup:request'].tapPromise('CacheWarmupPlugin', async (_, { request, parameters }) => {
|
|
52
|
+
try {
|
|
53
|
+
await request(parameters);
|
|
54
|
+
return {
|
|
55
|
+
parameters,
|
|
56
|
+
result: 'resolved',
|
|
57
|
+
};
|
|
58
|
+
}
|
|
59
|
+
catch {
|
|
60
|
+
return {
|
|
61
|
+
parameters,
|
|
62
|
+
result: 'rejected',
|
|
63
|
+
};
|
|
64
|
+
}
|
|
65
|
+
});
|
|
66
|
+
warmUpCache({ papiService: papiService, logger, environmentManager, hooks });
|
|
36
67
|
};
|
|
37
68
|
},
|
|
38
69
|
deps: {
|
|
39
70
|
papiService: { token: PAPI_SERVICE, optional: true },
|
|
40
71
|
logger: LOGGER_TOKEN,
|
|
41
72
|
environmentManager: ENV_MANAGER_TOKEN,
|
|
73
|
+
hooks: CACHE_WARMUP_HOOKS_TOKEN,
|
|
74
|
+
cacheWarmupEnabled: CACHE_WARMUP_ENABLED_TOKEN,
|
|
75
|
+
},
|
|
76
|
+
}),
|
|
77
|
+
provide({
|
|
78
|
+
provide: CACHE_WARMUP_HOOKS_TOKEN,
|
|
79
|
+
useFactory: ({ hooksFactory }) => {
|
|
80
|
+
return {
|
|
81
|
+
'cache-warmup:request': hooksFactory.createAsync('cache-warmup:request'),
|
|
82
|
+
};
|
|
83
|
+
},
|
|
84
|
+
deps: {
|
|
85
|
+
hooksFactory: TAPABLE_HOOK_FACTORY_TOKEN,
|
|
42
86
|
},
|
|
43
87
|
}),
|
|
44
88
|
],
|
package/lib/server.js
CHANGED
|
@@ -7,6 +7,7 @@ var core = require('@tramvai/core');
|
|
|
7
7
|
var tokensHttpClient = require('@tramvai/tokens-http-client');
|
|
8
8
|
var moduleCommon = require('@tramvai/module-common');
|
|
9
9
|
var warmup = require('./warmup.js');
|
|
10
|
+
var tokens = require('./tokens.js');
|
|
10
11
|
|
|
11
12
|
exports.CacheWarmupModule = class CacheWarmupModule {
|
|
12
13
|
};
|
|
@@ -14,37 +15,82 @@ exports.CacheWarmupModule = tslib.__decorate([
|
|
|
14
15
|
core.Module({
|
|
15
16
|
imports: [],
|
|
16
17
|
providers: [
|
|
18
|
+
core.provide({
|
|
19
|
+
provide: tokens.CACHE_WARMUP_ENABLED_TOKEN,
|
|
20
|
+
useFactory: ({ papiService, logger }) => {
|
|
21
|
+
const log = logger('cache-warmup');
|
|
22
|
+
if (!papiService) {
|
|
23
|
+
log.info('Skip cache warmup when @tramvai/module-http-client is not enabled');
|
|
24
|
+
return false;
|
|
25
|
+
}
|
|
26
|
+
if (process.env.CACHE_WARMUP_DISABLED === 'true') {
|
|
27
|
+
log.info('Skip cache warm up due to CACHE_WARMUP_DISABLED env');
|
|
28
|
+
return false;
|
|
29
|
+
}
|
|
30
|
+
if (process.env.NODE_ENV !== 'production') {
|
|
31
|
+
log.info('Skip cache warm up in dev environment');
|
|
32
|
+
return false;
|
|
33
|
+
}
|
|
34
|
+
// TODO: revisit this condition
|
|
35
|
+
// if (process.env.MOCKER_ENABLED === 'true') {
|
|
36
|
+
// log.info('Skip cache warm up when mocker is enabled');
|
|
37
|
+
// return false;
|
|
38
|
+
// }
|
|
39
|
+
return true;
|
|
40
|
+
},
|
|
41
|
+
deps: {
|
|
42
|
+
papiService: { token: tokensHttpClient.PAPI_SERVICE, optional: true },
|
|
43
|
+
logger: moduleCommon.LOGGER_TOKEN,
|
|
44
|
+
},
|
|
45
|
+
}),
|
|
17
46
|
core.provide({
|
|
18
47
|
provide: core.commandLineListTokens.listen,
|
|
19
48
|
multi: true,
|
|
20
|
-
useFactory({ papiService, logger, environmentManager }) {
|
|
21
|
-
return ()
|
|
22
|
-
|
|
23
|
-
if (!papiService) {
|
|
24
|
-
log.info('Skip cache warmup when @tramvai/module-http-client is not enabled');
|
|
25
|
-
return;
|
|
26
|
-
}
|
|
27
|
-
if (process.env.CACHE_WARMUP_DISABLED === 'true') {
|
|
28
|
-
log.info('Skip cache warm up due to CACHE_WARMUP_DISABLED env');
|
|
29
|
-
return;
|
|
30
|
-
}
|
|
31
|
-
if (process.env.NODE_ENV !== 'production') {
|
|
32
|
-
log.info('Skip cache warm up in dev environment');
|
|
33
|
-
return;
|
|
34
|
-
}
|
|
35
|
-
if (process.env.MOCKER_ENABLED === 'true') {
|
|
36
|
-
log.info('Skip cache warm up when mocker is enabled');
|
|
49
|
+
useFactory({ papiService, logger, environmentManager, hooks, cacheWarmupEnabled }) {
|
|
50
|
+
return function cacheWarmup() {
|
|
51
|
+
if (!cacheWarmupEnabled) {
|
|
37
52
|
return;
|
|
38
53
|
}
|
|
39
|
-
warmup.
|
|
54
|
+
hooks['cache-warmup:request'].tapPromise('CacheWarmupPlugin', async (_, { request, parameters }) => {
|
|
55
|
+
try {
|
|
56
|
+
await request(parameters);
|
|
57
|
+
return {
|
|
58
|
+
parameters,
|
|
59
|
+
result: 'resolved',
|
|
60
|
+
};
|
|
61
|
+
}
|
|
62
|
+
catch {
|
|
63
|
+
return {
|
|
64
|
+
parameters,
|
|
65
|
+
result: 'rejected',
|
|
66
|
+
};
|
|
67
|
+
}
|
|
68
|
+
});
|
|
69
|
+
warmup.warmUpCache({ papiService: papiService, logger, environmentManager, hooks });
|
|
40
70
|
};
|
|
41
71
|
},
|
|
42
72
|
deps: {
|
|
43
73
|
papiService: { token: tokensHttpClient.PAPI_SERVICE, optional: true },
|
|
44
74
|
logger: moduleCommon.LOGGER_TOKEN,
|
|
45
75
|
environmentManager: moduleCommon.ENV_MANAGER_TOKEN,
|
|
76
|
+
hooks: tokens.CACHE_WARMUP_HOOKS_TOKEN,
|
|
77
|
+
cacheWarmupEnabled: tokens.CACHE_WARMUP_ENABLED_TOKEN,
|
|
78
|
+
},
|
|
79
|
+
}),
|
|
80
|
+
core.provide({
|
|
81
|
+
provide: tokens.CACHE_WARMUP_HOOKS_TOKEN,
|
|
82
|
+
useFactory: ({ hooksFactory }) => {
|
|
83
|
+
return {
|
|
84
|
+
'cache-warmup:request': hooksFactory.createAsync('cache-warmup:request'),
|
|
85
|
+
};
|
|
86
|
+
},
|
|
87
|
+
deps: {
|
|
88
|
+
hooksFactory: core.TAPABLE_HOOK_FACTORY_TOKEN,
|
|
46
89
|
},
|
|
47
90
|
}),
|
|
48
91
|
],
|
|
49
92
|
})
|
|
50
93
|
], exports.CacheWarmupModule);
|
|
94
|
+
|
|
95
|
+
exports.CACHE_WARMUP_ENABLED_TOKEN = tokens.CACHE_WARMUP_ENABLED_TOKEN;
|
|
96
|
+
exports.CACHE_WARMUP_HOOKS_TOKEN = tokens.CACHE_WARMUP_HOOKS_TOKEN;
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { createToken, Scope } from '@tramvai/core';
|
|
2
|
+
|
|
3
|
+
const CACHE_WARMUP_ENABLED_TOKEN = createToken('tramvai cache warmup enabled', {
|
|
4
|
+
scope: Scope.SINGLETON,
|
|
5
|
+
});
|
|
6
|
+
const CACHE_WARMUP_HOOKS_TOKEN = createToken('tramvai cache warmup hooks', { scope: Scope.SINGLETON });
|
|
7
|
+
|
|
8
|
+
export { CACHE_WARMUP_ENABLED_TOKEN, CACHE_WARMUP_HOOKS_TOKEN };
|
package/lib/tokens.d.ts
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { AsyncTapableHookInstance } from '@tramvai/core';
|
|
2
|
+
import type { Request } from '@tinkoff/request-core';
|
|
3
|
+
export declare const CACHE_WARMUP_ENABLED_TOKEN: (false & {
|
|
4
|
+
__type?: "base token" | undefined;
|
|
5
|
+
}) | (true & {
|
|
6
|
+
__type?: "base token" | undefined;
|
|
7
|
+
});
|
|
8
|
+
export type CacheWarmupHooks = {
|
|
9
|
+
'cache-warmup:request': AsyncTapableHookInstance<{
|
|
10
|
+
request: (parameters: Request) => Promise<unknown>;
|
|
11
|
+
parameters: Request;
|
|
12
|
+
}, {
|
|
13
|
+
parameters: Request;
|
|
14
|
+
result: 'resolved' | 'rejected' | 'skipped';
|
|
15
|
+
}>;
|
|
16
|
+
};
|
|
17
|
+
export declare const CACHE_WARMUP_HOOKS_TOKEN: CacheWarmupHooks & {
|
|
18
|
+
__type?: "base token" | undefined;
|
|
19
|
+
};
|
|
20
|
+
//# sourceMappingURL=tokens.d.ts.map
|
package/lib/tokens.es.js
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { createToken, Scope } from '@tramvai/core';
|
|
2
|
+
|
|
3
|
+
const CACHE_WARMUP_ENABLED_TOKEN = createToken('tramvai cache warmup enabled', {
|
|
4
|
+
scope: Scope.SINGLETON,
|
|
5
|
+
});
|
|
6
|
+
const CACHE_WARMUP_HOOKS_TOKEN = createToken('tramvai cache warmup hooks', { scope: Scope.SINGLETON });
|
|
7
|
+
|
|
8
|
+
export { CACHE_WARMUP_ENABLED_TOKEN, CACHE_WARMUP_HOOKS_TOKEN };
|
package/lib/tokens.js
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
|
+
|
|
5
|
+
var core = require('@tramvai/core');
|
|
6
|
+
|
|
7
|
+
const CACHE_WARMUP_ENABLED_TOKEN = core.createToken('tramvai cache warmup enabled', {
|
|
8
|
+
scope: core.Scope.SINGLETON,
|
|
9
|
+
});
|
|
10
|
+
const CACHE_WARMUP_HOOKS_TOKEN = core.createToken('tramvai cache warmup hooks', { scope: core.Scope.SINGLETON });
|
|
11
|
+
|
|
12
|
+
exports.CACHE_WARMUP_ENABLED_TOKEN = CACHE_WARMUP_ENABLED_TOKEN;
|
|
13
|
+
exports.CACHE_WARMUP_HOOKS_TOKEN = CACHE_WARMUP_HOOKS_TOKEN;
|
package/lib/utils.d.ts
CHANGED
|
@@ -1,4 +1,6 @@
|
|
|
1
1
|
import type { Request } from '@tinkoff/request-core';
|
|
2
|
+
import { ExtractDependencyType } from '@tramvai/core';
|
|
3
|
+
import { CACHE_WARMUP_HOOKS_TOKEN } from './tokens';
|
|
2
4
|
type QueueReuestsOptions<T> = {
|
|
3
5
|
requestsOptions: T[];
|
|
4
6
|
makeRequest: (requestOptions: T) => Promise<unknown>;
|
|
@@ -7,10 +9,11 @@ type QueueReuestsOptions<T> = {
|
|
|
7
9
|
* @default 2
|
|
8
10
|
*/
|
|
9
11
|
maxSimultaneous?: number;
|
|
12
|
+
hooks: ExtractDependencyType<typeof CACHE_WARMUP_HOOKS_TOKEN>;
|
|
10
13
|
};
|
|
11
14
|
export declare function queueRequests<T>(options: QueueReuestsOptions<T>): Promise<{
|
|
12
|
-
|
|
13
|
-
result:
|
|
15
|
+
parameters: Request;
|
|
16
|
+
result: "resolved" | "rejected" | "skipped";
|
|
14
17
|
}[]>;
|
|
15
18
|
export declare function createRequestsOptions(options: {
|
|
16
19
|
urls: string[];
|
package/lib/utils.es.js
CHANGED
|
@@ -6,19 +6,11 @@ const request = requestFactory([httpPlugin()]);
|
|
|
6
6
|
async function queueRequests(options) {
|
|
7
7
|
const { maxSimultaneous = 2, makeRequest, requestsOptions } = options;
|
|
8
8
|
const req = async (option) => {
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
};
|
|
15
|
-
}
|
|
16
|
-
catch {
|
|
17
|
-
return {
|
|
18
|
-
option,
|
|
19
|
-
result: 'rejected',
|
|
20
|
-
};
|
|
21
|
-
}
|
|
9
|
+
const result = await options.hooks['cache-warmup:request'].callPromise({
|
|
10
|
+
request: makeRequest,
|
|
11
|
+
parameters: option,
|
|
12
|
+
});
|
|
13
|
+
return result;
|
|
22
14
|
};
|
|
23
15
|
const queue = [];
|
|
24
16
|
const result = [];
|
|
@@ -45,7 +37,7 @@ function createRequestsOptions(options) {
|
|
|
45
37
|
url: format({
|
|
46
38
|
hostname: 'localhost',
|
|
47
39
|
port,
|
|
48
|
-
path: url
|
|
40
|
+
path: url,
|
|
49
41
|
}),
|
|
50
42
|
headers: {
|
|
51
43
|
'User-Agent': userAgent,
|
package/lib/utils.js
CHANGED
|
@@ -15,19 +15,11 @@ const request = requestFactory__default["default"]([httpPlugin__default["default
|
|
|
15
15
|
async function queueRequests(options) {
|
|
16
16
|
const { maxSimultaneous = 2, makeRequest, requestsOptions } = options;
|
|
17
17
|
const req = async (option) => {
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
};
|
|
24
|
-
}
|
|
25
|
-
catch {
|
|
26
|
-
return {
|
|
27
|
-
option,
|
|
28
|
-
result: 'rejected',
|
|
29
|
-
};
|
|
30
|
-
}
|
|
18
|
+
const result = await options.hooks['cache-warmup:request'].callPromise({
|
|
19
|
+
request: makeRequest,
|
|
20
|
+
parameters: option,
|
|
21
|
+
});
|
|
22
|
+
return result;
|
|
31
23
|
};
|
|
32
24
|
const queue = [];
|
|
33
25
|
const result = [];
|
|
@@ -54,7 +46,7 @@ function createRequestsOptions(options) {
|
|
|
54
46
|
url: url.format({
|
|
55
47
|
hostname: 'localhost',
|
|
56
48
|
port,
|
|
57
|
-
path: url$1
|
|
49
|
+
path: url$1,
|
|
58
50
|
}),
|
|
59
51
|
headers: {
|
|
60
52
|
'User-Agent': userAgent,
|
package/lib/warmup.d.ts
CHANGED
|
@@ -1,8 +1,12 @@
|
|
|
1
1
|
import type { PAPI_SERVICE } from '@tramvai/tokens-http-client';
|
|
2
2
|
import type { ENV_MANAGER_TOKEN, LOGGER_TOKEN } from '@tramvai/module-common';
|
|
3
|
+
import { ExtractDependencyType } from '@tramvai/core';
|
|
4
|
+
import { CACHE_WARMUP_HOOKS_TOKEN } from './tokens';
|
|
5
|
+
export declare const deduplicateArray: <T>(list: T[]) => T[];
|
|
3
6
|
export declare function warmUpCache(options: {
|
|
4
|
-
papiService: typeof PAPI_SERVICE
|
|
5
|
-
logger: typeof LOGGER_TOKEN
|
|
6
|
-
environmentManager: typeof ENV_MANAGER_TOKEN
|
|
7
|
+
papiService: ExtractDependencyType<typeof PAPI_SERVICE>;
|
|
8
|
+
logger: ExtractDependencyType<typeof LOGGER_TOKEN>;
|
|
9
|
+
environmentManager: ExtractDependencyType<typeof ENV_MANAGER_TOKEN>;
|
|
10
|
+
hooks: ExtractDependencyType<typeof CACHE_WARMUP_HOOKS_TOKEN>;
|
|
7
11
|
}): Promise<void>;
|
|
8
12
|
//# sourceMappingURL=warmup.d.ts.map
|
package/lib/warmup.es.js
CHANGED
|
@@ -6,35 +6,46 @@ const userAgents = [
|
|
|
6
6
|
/** Chrome on Mobile */
|
|
7
7
|
'Mozilla/5.0 (Linux; Android 12.0; Pixel 5 XL) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3904.108 Mobile Safari/537.36',
|
|
8
8
|
];
|
|
9
|
+
const deduplicateArray = (list) => {
|
|
10
|
+
return Array.from(new Set(list));
|
|
11
|
+
};
|
|
9
12
|
async function warmUpCache(options) {
|
|
10
|
-
const { papiService, logger, environmentManager } = options;
|
|
13
|
+
const { papiService, logger, environmentManager, hooks } = options;
|
|
11
14
|
const log = logger('cache-warmup');
|
|
12
15
|
const startTimestamp = Date.now();
|
|
13
16
|
log.info("Cache warmup process 'START'");
|
|
14
17
|
try {
|
|
15
18
|
const { payload: urls } = await papiService.request({
|
|
16
|
-
path: '
|
|
19
|
+
path: 'prerenderRoutes',
|
|
17
20
|
});
|
|
18
21
|
const requestsOptions = createRequestsOptions({
|
|
19
22
|
urls,
|
|
20
23
|
port: environmentManager.get('PORT'),
|
|
21
24
|
userAgents,
|
|
22
25
|
});
|
|
26
|
+
log.info(`Cache warmup URLs:\n${deduplicateArray(requestsOptions.map((v) => v.url)).join('\n')}`);
|
|
23
27
|
const results = await queueRequests({
|
|
24
28
|
makeRequest: sendWarmUpRequest,
|
|
25
29
|
requestsOptions,
|
|
26
30
|
maxSimultaneous: 1,
|
|
31
|
+
hooks,
|
|
27
32
|
});
|
|
28
33
|
const failed = results.filter((result) => result.result === 'rejected');
|
|
34
|
+
const skipped = results.filter((result) => result.result === 'skipped');
|
|
29
35
|
if (failed.length) {
|
|
30
36
|
log.info(`Cache warmup process 'FINISHED' with errors, failed URLs:\n${failed
|
|
31
|
-
.map((v) => v.
|
|
37
|
+
.map((v) => v.parameters.url)
|
|
38
|
+
.join('\n')}`);
|
|
39
|
+
}
|
|
40
|
+
else if (skipped.length) {
|
|
41
|
+
log.info(`Cache warmup process 'SUCCESS', skipped URLs:\n${skipped
|
|
42
|
+
.map((v) => v.parameters.url)
|
|
32
43
|
.join('\n')}`);
|
|
33
44
|
}
|
|
34
45
|
else {
|
|
35
46
|
log.info("Cache warmup process 'SUCCESS'");
|
|
36
47
|
}
|
|
37
|
-
log.info(`Cache warmup
|
|
48
|
+
log.info(`Cache warmup made ${results.length - skipped.length} requests for ${urls.length} URLs`);
|
|
38
49
|
log.info(`Cache warmup took - ${Date.now() - startTimestamp}ms`);
|
|
39
50
|
}
|
|
40
51
|
catch (error) {
|
|
@@ -42,4 +53,4 @@ async function warmUpCache(options) {
|
|
|
42
53
|
}
|
|
43
54
|
}
|
|
44
55
|
|
|
45
|
-
export { warmUpCache };
|
|
56
|
+
export { deduplicateArray, warmUpCache };
|
package/lib/warmup.js
CHANGED
|
@@ -10,35 +10,46 @@ const userAgents = [
|
|
|
10
10
|
/** Chrome on Mobile */
|
|
11
11
|
'Mozilla/5.0 (Linux; Android 12.0; Pixel 5 XL) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3904.108 Mobile Safari/537.36',
|
|
12
12
|
];
|
|
13
|
+
const deduplicateArray = (list) => {
|
|
14
|
+
return Array.from(new Set(list));
|
|
15
|
+
};
|
|
13
16
|
async function warmUpCache(options) {
|
|
14
|
-
const { papiService, logger, environmentManager } = options;
|
|
17
|
+
const { papiService, logger, environmentManager, hooks } = options;
|
|
15
18
|
const log = logger('cache-warmup');
|
|
16
19
|
const startTimestamp = Date.now();
|
|
17
20
|
log.info("Cache warmup process 'START'");
|
|
18
21
|
try {
|
|
19
22
|
const { payload: urls } = await papiService.request({
|
|
20
|
-
path: '
|
|
23
|
+
path: 'prerenderRoutes',
|
|
21
24
|
});
|
|
22
25
|
const requestsOptions = utils.createRequestsOptions({
|
|
23
26
|
urls,
|
|
24
27
|
port: environmentManager.get('PORT'),
|
|
25
28
|
userAgents,
|
|
26
29
|
});
|
|
30
|
+
log.info(`Cache warmup URLs:\n${deduplicateArray(requestsOptions.map((v) => v.url)).join('\n')}`);
|
|
27
31
|
const results = await utils.queueRequests({
|
|
28
32
|
makeRequest: utils.sendWarmUpRequest,
|
|
29
33
|
requestsOptions,
|
|
30
34
|
maxSimultaneous: 1,
|
|
35
|
+
hooks,
|
|
31
36
|
});
|
|
32
37
|
const failed = results.filter((result) => result.result === 'rejected');
|
|
38
|
+
const skipped = results.filter((result) => result.result === 'skipped');
|
|
33
39
|
if (failed.length) {
|
|
34
40
|
log.info(`Cache warmup process 'FINISHED' with errors, failed URLs:\n${failed
|
|
35
|
-
.map((v) => v.
|
|
41
|
+
.map((v) => v.parameters.url)
|
|
42
|
+
.join('\n')}`);
|
|
43
|
+
}
|
|
44
|
+
else if (skipped.length) {
|
|
45
|
+
log.info(`Cache warmup process 'SUCCESS', skipped URLs:\n${skipped
|
|
46
|
+
.map((v) => v.parameters.url)
|
|
36
47
|
.join('\n')}`);
|
|
37
48
|
}
|
|
38
49
|
else {
|
|
39
50
|
log.info("Cache warmup process 'SUCCESS'");
|
|
40
51
|
}
|
|
41
|
-
log.info(`Cache warmup
|
|
52
|
+
log.info(`Cache warmup made ${results.length - skipped.length} requests for ${urls.length} URLs`);
|
|
42
53
|
log.info(`Cache warmup took - ${Date.now() - startTimestamp}ms`);
|
|
43
54
|
}
|
|
44
55
|
catch (error) {
|
|
@@ -46,4 +57,5 @@ async function warmUpCache(options) {
|
|
|
46
57
|
}
|
|
47
58
|
}
|
|
48
59
|
|
|
60
|
+
exports.deduplicateArray = deduplicateArray;
|
|
49
61
|
exports.warmUpCache = warmUpCache;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tramvai/module-cache-warmup",
|
|
3
|
-
"version": "6.
|
|
3
|
+
"version": "6.77.2",
|
|
4
4
|
"description": "tramvai cache warmup module",
|
|
5
5
|
"browser": "lib/browser.js",
|
|
6
6
|
"main": "lib/server.js",
|
|
@@ -20,15 +20,15 @@
|
|
|
20
20
|
},
|
|
21
21
|
"dependencies": {
|
|
22
22
|
"@tinkoff/request-core": "^0.10.0",
|
|
23
|
-
"@tinkoff/request-plugin-protocol-http": "0.
|
|
24
|
-
"@tinkoff/url": "0.12.
|
|
23
|
+
"@tinkoff/request-plugin-protocol-http": "0.16.0",
|
|
24
|
+
"@tinkoff/url": "0.12.2",
|
|
25
25
|
"undici": "^7.16.0"
|
|
26
26
|
},
|
|
27
27
|
"peerDependencies": {
|
|
28
|
-
"@tramvai/core": "6.
|
|
29
|
-
"@tramvai/module-common": "6.
|
|
30
|
-
"@tramvai/state": "6.
|
|
31
|
-
"@tramvai/tokens-http-client": "6.
|
|
28
|
+
"@tramvai/core": "6.77.2",
|
|
29
|
+
"@tramvai/module-common": "6.77.2",
|
|
30
|
+
"@tramvai/state": "6.77.2",
|
|
31
|
+
"@tramvai/tokens-http-client": "6.77.2",
|
|
32
32
|
"tslib": "^2.4.0"
|
|
33
33
|
},
|
|
34
34
|
"module": "lib/server.es.js",
|