Fail gracefully
EllyLoel opened this issue · 4 comments
Currently if the data isn't able to be fetched it fails and kills the whole build:
Error fetching https://api.microlink.io/?url=https://trib.al/5t27Gcr. Message: Bad response for https://api.microlink.io/?url=https://trib.al/5t27Gcr (429): Too Many Requests
Failing gracefully with an expired cache entry.
[11ty] Problem writing Eleventy templates: (more in DEBUG output)
[11ty] 1. Having trouble rendering njk template ./src/garden/CSS Nesting.md (via TemplateContentRenderError)
[11ty] 2. (./src/garden/CSS Nesting.md)
[11ty] EleventyShortcodeError: Error with Nunjucks shortcode `unfurl` (via Template render error)
[11ty] 3. Bad response for https://api.microlink.io/?url=https://developer.chrome.com/blog/help-css-nesting/ (429): Too Many Requests (via Template render error)
[11ty]
[11ty] Original error stack trace: Error: Bad response for https://api.microlink.io/?url=https://developer.chrome.com/blog/help-css-nesting/ (429): Too Many Requests
[11ty] at RemoteAssetCache.fetch (/Users/ellyloel/git/ellyloel.com/node_modules/@11ty/eleventy-cache-assets/src/RemoteAssetCache.js:55:11)
[11ty] at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
[11ty] at async run (/Users/ellyloel/git/ellyloel.com/node_modules/p-queue/dist/index.js:163:29)
[11ty] Copied 65 files / Wrote 0 files in 96.66 seconds (v2.0.0-canary.18)
error Command failed with exit code 1.
It might be nice to fail gracefully and return the link to be displayed instead.
try {
const metadata = await Cache(`https://api.microlink.io/?url=${link}`, {
duration: "1m",
type: "json",
});
if (options.template) {
return options.template(metadata.data);
}
return template(metadata.data);
} catch(e) {
console.error(e);
return link;
}
Also, on another note, it could be good to make an option for the cache duration, and switch from Eleventy cache assets to Eleventy fetch.
const metadata = await EleventyFetch(`https://api.microlink.io/?url=${link}`, {
duration: options?.duration || "1m",
type: "json",
});
I should add, I'm more than happy to put up PRs for this stuff if you think it sounds good, as I've already patched it locally for my own use.
Dang, can't believe I left this plugin like this. Thanks for highlighting @EllyLoel! Totally open to a pull request but no pressure, I should give this plugin some love really since some dependencies are out of date (like the caching plugin you mentioned)
Ah awesome! Thanks so much for doing this 🙌🏻