Skip to content

Commit 72720fa

Browse files
committed
cache fetched styles
1 parent 3a59549 commit 72720fa

1 file changed

Lines changed: 14 additions & 2 deletions

File tree

src/livecodes/languages/scss/lang-scss-compiler.ts

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,22 @@ import { getLanguageCustomSettings } from '../../utils';
44

55
(self as any).createScssCompiler = (): CompilerFunction => {
66
const sass = (window as any).sass;
7+
const cachedStyles = new Map<string, string>();
78
return async (code, { config, language }): Promise<string> => {
89
const syntax = language === 'sass' ? 'indented' : 'scss';
910
const customSettings = getLanguageCustomSettings(language, config);
1011
let baseUrl: string | null = null;
1112

1213
const moduleServiceUrl = modulesService.getUrl('~').replace('~', '');
1314

14-
const fetchStyles = (url: string) => {
15+
const fetchStyles = (url: string): Promise<{ contents: string; syntax: string }> => {
16+
const cachedContents = cachedStyles.get(url);
17+
if (cachedContents) {
18+
return Promise.resolve({
19+
contents: cachedContents,
20+
syntax,
21+
});
22+
}
1523
const moduleUrl =
1624
baseUrl && baseUrl !== url ? new URL(url.replace(moduleServiceUrl, ''), baseUrl).href : url;
1725
const module = moduleUrl.replace(moduleServiceUrl, '');
@@ -49,7 +57,7 @@ import { getLanguageCustomSettings } from '../../utils';
4957
load(canonicalUrl: URL) {
5058
const urlString = canonicalUrl.href;
5159
const extension = '.' + language; // .scss or .sass
52-
return fetchStyles(urlString)
60+
const result: Promise<{ contents: string; syntax: string }> = fetchStyles(urlString)
5361
.catch(() => fetchStyles(urlString + extension))
5462
.catch(() => fetchStyles(urlString + '.css'))
5563
.catch(() => {
@@ -82,6 +90,10 @@ import { getLanguageCustomSettings } from '../../utils';
8290
});
8391
}),
8492
);
93+
return result.then((res) => {
94+
cachedStyles.set(urlString, res.contents);
95+
return res;
96+
});
8597
},
8698
},
8799
],

0 commit comments

Comments
 (0)