src/loader.js
/**
   * Init cache system for result files (if enabled).
   * Failsafe (exceptions are gracefully converted to warnings, cache disregarded)
   * 
   * @param {Buffer} inputBuffer - The buffer holding the source image.
   */
Init cache system for result files (if enabled).
Failsafe (exceptions are gracefully converted to warnings, cache disregarded)

@param {Buffer} inputBuffer - The buffer holding the source image.
Init cache system for result files ( if enabled ) . Failsafe ( exceptions are gracefully converted to warnings cache disregarded ) @param { Buffer } inputBuffer - The buffer holding the source image .
async initCache(inputBuffer) {
    let result = {
      imageDataFound: null,
      save: (imageData) => false,
      debugInfo: ''
    };
    if (this.options.cacheResults) {
      try {
        const cacheKey = createHash('md4')
          .update(this.context.resource) 
          .update(JSON.stringify(this.imageOutParams)) 
          .update(inputBuffer) 
          .digest('hex');
        const { AssetCache } = this.eleventyCache;
        const cache = new AssetCache(cacheKey, this.options.cacheDir);
        if (cache.isCacheValid(this.options.cacheDuration)) {
          result.imageDataFound = await cache.getCachedValue();
          result.imageDataFound.buffer = Buffer.from(result.imageDataFound.buffer);
          result.debugInfo = 'from cache';
        }
        result.save = (imageData) => {
          try {
            cache.save(imageData, 'json');
            result.debugInfo = 'saved to cache';
          } catch (err) {
            this.context.emitWarning(
              new LoaderError('Cannot save image data to cache.', err)
            );
          }
        };
      } catch (err) {
        this.context.emitWarning(
          new LoaderError('Cannot read cache (initialization failure)', err)
        );
      }
    }
    return result;
  }
async initCache ( inputBuffer ) { let result = { imageDataFound : null , save : ( imageData ) => false , debugInfo : '' } ; if ( this . options . cacheResults ) { try { const cacheKey = createHash ( 'md4' ) . update ( this . context . resource ) . update ( JSON . stringify ( this . imageOutParams ) ) . update ( inputBuffer ) . digest ( 'hex' ) ; const { AssetCache } = this . eleventyCache ; const cache = new AssetCache ( cacheKey , this . options . cacheDir ) ; if ( cache . isCacheValid ( this . options . cacheDuration ) ) { result . imageDataFound = await cache . getCachedValue ( ) ; result . imageDataFound . buffer = Buffer . from ( result . imageDataFound . buffer ) ; result . debugInfo = 'from cache' ; } result . save = ( imageData ) => { try { cache . save ( imageData , 'json' ) ; result . debugInfo = 'saved to cache' ; } catch ( err ) { this . context . emitWarning ( new LoaderError ( 'Cannot save image data to cache.' , err ) ) ; } } ; } catch ( err ) { this . context . emitWarning ( new LoaderError ( 'Cannot read cache (initialization failure)' , err ) ) ; } } return result ; }
Init cache system for result files (if enabled).
Init cache system for result files ( if enabled ) .
// path to the local resource file (image or fetch file with query params) // size, format, sharp config, etc (options to eleventy-img) // .update(String(Buffer.byteLength(buffer))) //idea: instead content hash (faster) // buffer holding binary content of the image beeing processed // https://www.11ty.dev/docs/plugins/cache/#manually-store-your-own-data-in-the-cache // we need to convert the buffer string from json back to a real buffer // Falling gracefully with adding a warning to Webpack's log system.