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

Skip to content

Commit 1cb193f

Browse files
authored
Merge pull request #1712 from Dymerz/feat/isr_add_inline-critical-css_support
feat(isr): add `inlineCriticalCss` support
2 parents 7875319 + 001b507 commit 1cb193f

File tree

3 files changed

+23
-12
lines changed

3 files changed

+23
-12
lines changed

libs/isr/models/src/isr-handler-config.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,12 @@ export interface ISRHandlerConfig {
5050
*/
5151
browserDistFolder?: string;
5252

53+
/**
54+
* Reduce render blocking requests by inlining critical CSS.
55+
* If not provided, defaults to true.
56+
*/
57+
inlineCriticalCss?: boolean;
58+
5359
/**
5460
* The build ID of the application. This value is used to generate unique cache keys.
5561
* If not provided, defaults to null.

libs/isr/server/src/isr-handler.ts

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -45,14 +45,14 @@ export class ISRHandler {
4545
config.indexHtml,
4646
config.commonEngine,
4747
config.bootstrap,
48-
config.browserDistFolder
48+
config.browserDistFolder,
4949
);
5050
}
5151

5252
async invalidate(
5353
req: Request,
5454
res: Response,
55-
config?: InvalidateConfig
55+
config?: InvalidateConfig,
5656
): Promise<any> {
5757
const { token, urlsToInvalidate } = extractDataFromBody(req);
5858
const { indexHtml } = this.config;
@@ -125,20 +125,21 @@ export class ISRHandler {
125125
const invalidatedUrls = variantUrlsToInvalidate
126126
.map((val) => val.cacheKey)
127127
.filter(
128-
(cacheKey) => !notInCache.includes(cacheKey) && !urlWithErrors[cacheKey]
128+
(cacheKey) =>
129+
!notInCache.includes(cacheKey) && !urlWithErrors[cacheKey],
129130
);
130131

131132
if (notInCache.length) {
132133
this.logger.log(
133-
`Urls: ${notInCache.join(', ')} does not exist in cache.`
134+
`Urls: ${notInCache.join(', ')} does not exist in cache.`,
134135
);
135136
}
136137

137138
if (Object.keys(urlWithErrors).length) {
138139
this.logger.log(
139140
`Urls: ${Object.keys(urlWithErrors).join(
140-
', '
141-
)} had errors while regenerating!`
141+
', ',
142+
)} had errors while regenerating!`,
142143
);
143144
}
144145

@@ -181,7 +182,7 @@ export class ISRHandler {
181182
req: Request,
182183
res: Response,
183184
next: NextFunction,
184-
config?: ServeFromCacheConfig
185+
config?: ServeFromCacheConfig,
185186
): Promise<any> {
186187
try {
187188
const variant = this.getVariant(req);
@@ -208,7 +209,7 @@ export class ISRHandler {
208209
res,
209210
cacheData,
210211
this.logger,
211-
config?.providers
212+
config?.providers,
212213
);
213214
}
214215
}
@@ -226,7 +227,7 @@ export class ISRHandler {
226227
// Cache exists. Send it.
227228
this.logger.log(
228229
`Page was retrieved from cache: `,
229-
getCacheKey(req.url, variant)
230+
getCacheKey(req.url, variant),
230231
);
231232
return res.send(finalHtml);
232233
} catch (error) {
@@ -239,7 +240,7 @@ export class ISRHandler {
239240
req: Request,
240241
res: Response,
241242
next: NextFunction,
242-
config?: RenderConfig
243+
config?: RenderConfig,
243244
): Promise<any> {
244245
const renderUrlConfig: RenderUrlConfig = {
245246
req,
@@ -250,6 +251,7 @@ export class ISRHandler {
250251
bootstrap: this.config.bootstrap,
251252
commonEngine: this.config.commonEngine,
252253
browserDistFolder: this.config.browserDistFolder,
254+
inlineCriticalCss: this.config.inlineCriticalCss,
253255
};
254256

255257
renderUrl(renderUrlConfig).then(async (html) => {
@@ -298,7 +300,7 @@ export class ISRHandler {
298300
}
299301

300302
const extractDataFromBody = (
301-
req: Request
303+
req: Request,
302304
): { token: string | null; urlsToInvalidate: string[] } => {
303305
const { urlsToInvalidate, token } = req.body;
304306
return { urlsToInvalidate, token };

libs/isr/server/src/utils/render-url.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ export interface RenderUrlConfig {
1414
commonEngine?: CommonEngine;
1515
bootstrap?: CommonEngineRenderOptions['bootstrap'];
1616
browserDistFolder?: string;
17+
inlineCriticalCss?: boolean;
1718
}
1819

1920
const EXTRA_PROVIDERS: Provider[] = [
@@ -31,6 +32,7 @@ export const renderUrl = async (options: RenderUrlConfig): Promise<string> => {
3132
commonEngine,
3233
bootstrap,
3334
browserDistFolder,
35+
inlineCriticalCss,
3436
} = options;
3537

3638
const { protocol, originalUrl, baseUrl, headers } = req;
@@ -56,6 +58,7 @@ export const renderUrl = async (options: RenderUrlConfig): Promise<string> => {
5658
documentFilePath: indexHtml,
5759
url: `${protocol}://${headers.host}${originalUrl}`,
5860
publicPath: browserDistFolder,
61+
inlineCriticalCss: inlineCriticalCss ?? true,
5962
providers: [...allProviders] as StaticProvider[], // we need to cast to StaticProvider[] because of a bug in the types
6063
})
6164
.then((html) => {
@@ -73,7 +76,7 @@ export const renderUrl = async (options: RenderUrlConfig): Promise<string> => {
7376
reject(err);
7477
}
7578
resolve(html);
76-
}
79+
},
7780
);
7881
}
7982
});

0 commit comments

Comments
 (0)