11ty / eleventy-fetch

Utility to cache any remote asset: Image, Video, Web Font, CSS, JSON, etc
https://www.11ty.dev/docs/plugins/fetch/
144 stars 17 forks source link

shorthash can generate the same identifier for different URLs #7

Closed eatyourgreens closed 2 years ago

eatyourgreens commented 4 years ago

Hi,

I'm using this plugin to cache API responses and I've run into a problem where, occasionally, a URL will resolve to the wrong resource from our database.

The problem seems to occur here, where shorthash is used to generate the cache ID. https://github.com/11ty/eleventy-cache-assets/blob/4ff6669de4fecba7b74cb46decb91e890c89693d/src/RemoteAssetCache.js#L12

Logging a couple of the problem URLs from out API, here's the URL, shorthash(URL) and the resolved resource ID. The second subject ASC0000q71 resolves as subject ASC0000qu3.

https://api.zooniverse.org/projects/illustratedlife/talk/subjects/ASC0000qu3, bc88de75, ASC0000qu3
https://api.zooniverse.org/projects/illustratedlife/talk/subjects/ASC0000q71, bc88de75, ASC0000qu3
eatyourgreens commented 4 years ago

Would passing in an optional ID, that overrides the hash, be a useful fix? I'm thinking of trying something like this for our project.

let url = 'https://api.zooniverse.org/projects/illustratedlife/talk/subjects/ASC0000q71';
let subjectData = await CacheAsset(url, {
  id: 'ASC0000q71',
  duration: "1d";
  type: 'json'
});
zachleat commented 2 years ago

Merged, shipping with 3.0.0