Skip to content

Commit 1a1669c

Browse files
committed
Merge remote-tracking branch 'ybnd/cache-bust-dynamic-configuration-7.6_CLEAN' into cache-bust-dynamic-configuration-7.6
2 parents bc9a33e + d2458e6 commit 1a1669c

4 files changed

Lines changed: 18 additions & 17 deletions

File tree

config/config.example.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -368,6 +368,7 @@ themes:
368368
# - name: BASE_THEME_NAME
369369
#
370370
- name: dspace
371+
prefetch: true
371372
headTags:
372373
- tagName: link
373374
attributes:

server.ts

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

7878
// 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: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,6 @@ import {
2222
} from 'path';
2323
import zlib from 'zlib';
2424
import { hasValue } from '../../app/shared/empty.util';
25-
import { ThemeConfig } from '../../config/theme.config';
26-
2725
import {
2826
HashedFileMapping,
2927
ID,
@@ -46,7 +44,7 @@ export class ServerHashedFileMapping extends HashedFileMapping {
4644
public readonly indexPath: string;
4745
private readonly indexContent: string;
4846

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

5149
constructor(
5250
private readonly root: string,
@@ -112,33 +110,30 @@ export class ServerHashedFileMapping extends HashedFileMapping {
112110
return hashPath;
113111
}
114112

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 path = `${this.root}/${themeConfiguration.name}-theme.css`;
122-
const hashPath = this.add(path);
113+
addThemeStyle(theme: string, prefetch = true) {
114+
const path = `${this.root}/${theme}-theme.css`;
115+
const hashPath = this.add(path);
123116

117+
if (prefetch) {
124118
// We know this CSS is likely needed, so we can avoid a FOUC by retrieving it in advance
125119
// Angular does the same for global styles, but doesn't "know" about our themes
126120
this.addHeadLink({
127121
path,
128122
rel: 'prefetch',
129123
as: 'style',
130124
});
131-
132-
this.ensureCompressedFilesAssumingUnchangedContent(path, hashPath, '.br');
133-
this.ensureCompressedFilesAssumingUnchangedContent(path, hashPath, '.gz');
134125
}
126+
127+
// We know theme CSS has been compressed already
128+
this.ensureCompressedFilesAssumingUnchangedContent(path, hashPath, '.br');
129+
this.ensureCompressedFilesAssumingUnchangedContent(path, hashPath, '.gz');
135130
}
136131

137132
/**
138133
* Include a head link for a given resource to the index HTML.
139134
*/
140135
addHeadLink(headLink: HeadLink) {
141-
this.headLinks.add(headLink);
136+
this.headLinks.set(headLink.path, headLink);
142137
}
143138

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

179-
for (const headLink of this.headLinks) {
174+
for (const headLink of this.headLinks.values()) {
180175
root.querySelector('head')
181176
.appendChild(this.renderHeadLink(headLink) as any);
182177
}

0 commit comments

Comments
 (0)