Skip to content

Commit e10d7b5

Browse files
author
abhinav
committed
Merge branch 'cache-bust-dynamic-configuration-7.6_CLEAN' into cache-bust-dynamic-configuration-8.x
2 parents 9fdb823 + d2458e6 commit e10d7b5

File tree

4 files changed

+22
-20
lines changed

4 files changed

+22
-20
lines changed

config/config.example.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -408,6 +408,7 @@ themes:
408408
# - name: BASE_THEME_NAME
409409
#
410410
- name: dspace
411+
prefetch: true
411412
headTags:
412413
- tagName: link
413414
attributes:

server.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ const cookieParser = require('cookie-parser');
7474
const configJson = join(DIST_FOLDER, 'assets/config.json');
7575
const hashedFileMapping = new ServerHashedFileMapping(DIST_FOLDER, 'index.html');
7676
const appConfig: AppConfig = buildAppConfig(configJson, hashedFileMapping);
77-
hashedFileMapping.addThemeStyles(appConfig.themes);
77+
appConfig.themes.forEach(themeConfig => hashedFileMapping.addThemeStyle(themeConfig.name, themeConfig.prefetch));
7878
hashedFileMapping.save();
7979

8080
// cache of SSR pages for known bots, only enabled in production mode

src/config/theme.config.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,11 @@ export interface NamedThemeConfig extends Config {
1313
* A list of HTML tags that should be added to the HEAD section of the document, whenever this theme is active.
1414
*/
1515
headTags?: HeadTagConfig[];
16+
17+
/**
18+
* Whether this theme's CSS should be prefetched in CSR mode
19+
*/
20+
prefetch?: boolean;
1621
}
1722

1823
/**

src/modules/dynamic-hash/hashed-file-mapping.server.ts

Lines changed: 15 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ import {
2323
import zlib from 'zlib';
2424

2525
import { hasValue } from '../../app/shared/empty.util';
26-
import { ThemeConfig } from '../../config/theme.config';
2726
import {
2827
HashedFileMapping,
2928
ID,
@@ -46,7 +45,7 @@ export class ServerHashedFileMapping extends HashedFileMapping {
4645
public readonly indexPath: string;
4746
private readonly indexContent: string;
4847

49-
protected readonly headLinks: Set<HeadLink> = new Set();
48+
protected readonly headLinks: Map<string, HeadLink> = new Map();
5049

5150
constructor(
5251
private readonly root: string,
@@ -71,7 +70,7 @@ export class ServerHashedFileMapping extends HashedFileMapping {
7170

7271
// remove previous files
7372
const ext = extname(path);
74-
glob.GlobSync(path.replace(`${ext}`, `.*${ext}*`))
73+
new glob.GlobSync(path.replace(`${ext}`, `.*${ext}*`))
7574
.found
7675
.forEach(p => rmSync(p));
7776

@@ -112,33 +111,30 @@ export class ServerHashedFileMapping extends HashedFileMapping {
112111
return hashPath;
113112
}
114113

115-
/**
116-
* Add CSS for all configured themes to the mapping
117-
* @param themeConfigurations
118-
*/
119-
addThemeStyles(themeConfigurations: ThemeConfig[]) {
120-
for (const themeConfiguration of themeConfigurations) {
121-
const p = `${this.root}/${themeConfiguration.name}-theme.css`;
122-
const hp = this.add(p);
114+
addThemeStyle(theme: string, prefetch = true) {
115+
const path = `${this.root}/${theme}-theme.css`;
116+
const hashPath = this.add(path);
123117

124-
// We know this CSS is likely needed, so wecan avoid a FOUC by retrieving it in advance
125-
// Angular does the same for global styles, but doesn't "know" about out themes
118+
if (prefetch) {
119+
// We know this CSS is likely needed, so we can avoid a FOUC by retrieving it in advance
120+
// Angular does the same for global styles, but doesn't "know" about our themes
126121
this.addHeadLink({
127-
path: p,
122+
path,
128123
rel: 'prefetch',
129124
as: 'style',
130125
});
131-
132-
this.ensureCompressedFilesAssumingUnchangedContent(p, hp, '.br');
133-
this.ensureCompressedFilesAssumingUnchangedContent(p, hp, '.gz');
134126
}
127+
128+
// We know theme CSS has been compressed already
129+
this.ensureCompressedFilesAssumingUnchangedContent(path, hashPath, '.br');
130+
this.ensureCompressedFilesAssumingUnchangedContent(path, hashPath, '.gz');
135131
}
136132

137133
/**
138134
* Include a head link for a given resource to the index HTML.
139135
*/
140136
addHeadLink(headLink: HeadLink) {
141-
this.headLinks.add(headLink);
137+
this.headLinks.set(headLink.path, headLink);
142138
}
143139

144140
private renderHeadLink(link: HeadLink): string {
@@ -176,7 +172,7 @@ export class ServerHashedFileMapping extends HashedFileMapping {
176172
root.querySelector('head')
177173
.appendChild(`<script id="${ID}" type="application/json">${JSON.stringify(out)}</script>` as any);
178174

179-
for (const headLink of this.headLinks) {
175+
for (const headLink of this.headLinks.values()) {
180176
root.querySelector('head')
181177
.appendChild(this.renderHeadLink(headLink) as any);
182178
}

0 commit comments

Comments
 (0)