1- import { precacheCommonTags } from "../utils/cssTools" ;
2- import { fetchImage } from "../utils/helpers" ;
3- import { extractURL } from "../utils/helpers" ;
4- import { embedCustomFonts } from "../modules/fonts" ;
5- import { imageCache , bgCache , resourceCache , baseCSSCache , computedStyleCache } from "../core/cache" ;
1+ import { getStyle , inlineSingleBackgroundEntry , fetchImage , splitBackgroundImage } from '../utils/helpers.js' ;
2+ import { embedCustomFonts } from '../modules/fonts.js' ;
3+ import { precacheCommonTags } from '../utils/cssTools.js' ;
4+ import { imageCache , bgCache , resourceCache , baseCSSCache } from '../core/cache.js' ;
65
76/**
87 * Preloads images, background images, and optionally fonts into cache before DOM capture.
9- * This helps avoid delays or missing resources during the capture process.
108 *
11- * - If `reset` is true, all caches are cleared and the function returns immediately.
12- * - If `embedFonts` is true, custom fonts are embedded (icon fonts are always embedded).
13- * - If `preWarm` is true, common tag styles are pre-cached.
14- *
15- * @export
16- * @param {Document|Element } [root=document] - The root node to search for resources (defaults to the whole document)
9+ * @param {Document|Element } [root=document] - The root node to search for resources
1710 * @param {Object } [options={}] - Pre-caching options
18- * @param {boolean } [options.embedFonts=true] - Whether to embed custom fonts
19- * @param {boolean } [options.reset=false] - Whether to clear all caches before pre-caching
20- * @param {boolean } [options.preWarm=true] - Whether to pre-cache common tag styles
21- * @param {Function } [options.crossOrigin] - Function that returns CORS mode for each image URL
2211 * @returns {Promise<void> } Resolves when all resources are pre-cached
2312 */
2413
@@ -29,7 +18,7 @@ export async function preCache(root = document, options = {}) {
2918 bgCache . clear ( ) ;
3019 resourceCache . clear ( ) ;
3120 baseCSSCache . clear ( ) ;
32- computedStyleCache . clear ( ) ;
21+ // computedStyleCache.clear(); Not necessary to clear
3322 return ;
3423 }
3524
@@ -55,15 +44,18 @@ export async function preCache(root = document, options = {}) {
5544 }
5645 }
5746 for ( const el of allEls ) {
58- const bg = getComputedStyle ( el ) . backgroundImage ;
59- const url = extractURL ( bg ) ;
60- if ( url && ! bgCache . has ( url ) ) {
61- const crossOrigin = crossOriginFn ? crossOriginFn ( url ) : "anonymous" ;
62- promises . push (
63- fetchImage ( url , 3000 , crossOrigin )
64- . then ( dataURL => bgCache . set ( url , dataURL ) )
65- . catch ( ( ) => { } )
66- ) ;
47+ const bg = getStyle ( el ) . backgroundImage ;
48+ if ( bg && bg !== "none" ) {
49+ const bgSplits = splitBackgroundImage ( bg ) ;
50+ for ( const entry of bgSplits ) {
51+ const isUrl = entry . startsWith ( "url(" ) ;
52+ if ( isUrl ) {
53+ promises . push (
54+ inlineSingleBackgroundEntry ( entry , { crossOrigin : crossOriginFn , skipInline : true } )
55+ . catch ( ( ) => { } )
56+ ) ;
57+ }
58+ }
6759 }
6860 }
6961
0 commit comments