Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RFC: Tokens for remote images #162

Open
zeroby0 opened this issue Nov 5, 2022 · 1 comment
Open

RFC: Tokens for remote images #162

zeroby0 opened this issue Nov 5, 2022 · 1 comment

Comments

@zeroby0
Copy link
Contributor

zeroby0 commented Nov 5, 2022

As of eleventy-img v2.0.1, there is a pretty critical architectural bug related to remote images. Currently the hashes of remote images are generated using the URL, instead of the image content, and that creates a class of cache related bugs. Here is a Request For Comments on one way to solve it. Please leave your thoughts.

When returning stats for remote images, lets return a randomly generated token, and store a mapping from URL to tokens in memory. After all the remote images are downloaded, and their hashes are calculated, we do a string replace of the Token with the path of the image generated using the hash.

Example

Stealing code from the docs for this:

const Image = require("@11ty/eleventy-img");

(async () => {
  let url = "https://images.unsplash.com/photo-1608178398319-48f814d0750c";
  let stats = await Image(url, {
    widths: [300]
  });

  console.log( stats );
})();

The output is:

{
  webp: [
    {
      format: 'webp',
      width: 300,
      height: 300,
      filename: 'C90K6oSMsVss7XJ5pPpY-300.webp',
      outputPath: 'img/C90K6oSMsVss7XJ5pPpY-300.webp',
      url: '/img/C90K6oSMsVss7XJ5pPpY-300.webp',
      sourceType: 'image/webp',
      srcset: '/img/C90K6oSMsVss7XJ5pPpY-300.webp 300w',
      size: 10184,
    },
  ],
  jpeg: [
    {
      format: 'jpeg',
      width: 300,
      height: 300,
      filename: 'C90K6oSMsVss7XJ5pPpY-300.jpeg',
      outputPath: 'img/C90K6oSMsVss7XJ5pPpY-300.jpeg',
      url: '/img/C90K6oSMsVss7XJ5pPpY-300.jpeg',
      sourceType: 'image/jpeg',
      srcset: '/img/C90K6oSMsVss7XJ5pPpY-300.jpeg 300w',
      size: 15616
    }
  ]
}

we store a mapping {https://images.unsplash.com/photo-1608178398319-48f814d0750c: 'C90K6oSMsVss7XJ5pPpY-'} inside eleventy-img, and replace it with the actual file hash as post processing of eleventy generated images.

Please leave your comments about potential bugs you see in this system and your thoughts on improvement. Thank you.

@zeroby0
Copy link
Contributor Author

zeroby0 commented Nov 7, 2022

Actually it doesn't need to be random tokens. We can keep the current behaviour and just return the hash of the URL instead of a randomly generated token. Then do pretty much the same thing.

Let's provide an option to enable or disable this new behaviour and keep it disabled by default, atleast until a new major version.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant