-
-
Notifications
You must be signed in to change notification settings - Fork 54
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
Persist in-memory cache, change hash. #116
Conversation
Benchmarks
These aren't 'true' benchmarks, just the times taken to run the sample/sample.js file. Further, I ran them only once, ideally you would run each case several times and take the median value. All the values are taken with the remote files cache filled, so my Internet connection doesn't influence the results. Also, Huge thanks to everyone who wrote the tests, they were super helpful in identifying the bugs! |
I simplified the hashing and fixed a bug in it in f265638
With this, I think it's now ready for review. |
Please review and merge. |
Reviewing! |
Did some more testing this morning and this is looks like it’s working great! Super exciting PR, thank you! |
Thank you so much for reviewing, and for the incredible feedback! :D Here is a summary of changes (will be kept up to date), to help with documentation: CachingIf the output files exist in the output directory, they will not be reprocessed. We could cache the output images in The flipside is that some people like to clear the
Images will be reprocessed if HashesThe hash is calculated from image content and the options sent to Sharp. The default length is 10 url-safe base64 letters (60 bits). The hashing algorithm is SHA256 from the As the hash truly reflects the image contents, and is included in the name, the images generated can be safely HTTP cached forever. The hash function is also available in the public API as
The hashing algorithm and the encoding are not configurable by the user yet, but we can let them be, if it seems like a good idea. statsSync and statsByDimensionsSync
|
const fileContent = fs.readFileSync(src); | ||
hash.update(fileContent); | ||
} else { | ||
hash.update(src); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
resolved in 091f430
This line means the hash for urls will always be the same, even if the remote content changes.
So even if eleventy-cache-assets fetches the new asset, it will not be replaced in the output because the old version with the same hash exists.
queueImage
downloads the remote asset so we can use the content for hashing, but statsSync
and statsByDimensionsSync
will be broken for urls.
Unless
- The remote asset content be passed in by the user as options.remoteAssetContent (091f430 implements this). or
- We can somehow synchronously fetch assets with eleventy-cache-assets, or
- Use the
options.__validAssetCache
and regenerate images if cache is invalid. But the user will have to treat remote images and local images differently, because remote images are generated from url, not content. For example, such remote images cannot be http cached.
The first option is the most flexible of the three: If the user is unable to pass in the remote content, they can pass in the url as options.remoteAssetContent and the build will not fail. Or they can pass in a random string in production, and be sure that the hash is unique.
Shipping with 1.0, thanks! I did remove the remoteAssetContent piece for hashing, though. We can’t rely on the remote content for the synchronous hash methods, as the content fetching is asynchronous |
Just wanted to note that I tested this in 1.0.0-beta.2 with a lot of local images and it's incredibly awesome. Makes rebuilds happen lightning quick. Thanks! |
Came here to say the same! Very nice |
Fixes #51
Fixes #115
If the processed file exists in the output folder, processing it is skipped. Thus persisting the cache.
Output images are reprocessed if
useCache: false
Because we are using cache across runs, we can't use just the file name for calculating hash:
So I had to change hash to use image content + sharp options. This hashing is done with the built-in crypto library, using SHA1 because it seemed to have the highest throughput.
This PR was meant to solve #115, but it solves #51 too by accident 😂
For testing, please use:
See summary of changes: #116 (comment)
See it in action: https://github.com/zeroby0/demo-eleventy-img-cache