Skip to content

Commit

Permalink
Rename to eleventy-fetch
Browse files Browse the repository at this point in the history
  • Loading branch information
zachleat committed Feb 25, 2022
1 parent 308b7ab commit c180d03
Show file tree
Hide file tree
Showing 8 changed files with 30 additions and 25 deletions.
16 changes: 11 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<p align="center"><img src="https://www.11ty.dev/img/logo-github.png" alt="eleventy Logo"></p>

# eleventy-cache-assets
# eleventy-fetch

_Requires Node 12+_

Expand All @@ -10,21 +10,21 @@ With the added benefit that if one successful request completes, you can now wor

This plugin can save any kind of asset—JSON, HTML, images, videos, etc.

## [The full `eleventy-cache-assets` documentation is on 11ty.dev](https://www.11ty.dev/docs/plugins/cache/).
## [The full `eleventy-fetch` documentation is on 11ty.dev](https://www.11ty.dev/docs/plugins/cache/).

* _This is a plugin for the [Eleventy static site generator](https://www.11ty.dev/)._
* Find more [Eleventy plugins](https://www.11ty.dev/docs/plugins/).
* Please star [Eleventy on GitHub](https://github.com/11ty/eleventy/), follow [@eleven_ty](https://twitter.com/eleven_ty) on Twitter, and support [11ty on Open Collective](https://opencollective.com/11ty)

[![npm Version](https://img.shields.io/npm/v/@11ty/eleventy-cache-assets.svg?style=for-the-badge)](https://www.npmjs.com/package/@11ty/eleventy-cache-assets) [![GitHub issues](https://img.shields.io/github/issues/11ty/eleventy-cache-assets.svg?style=for-the-badge)](https://github.com/11ty/eleventy/issues)
[![npm Version](https://img.shields.io/npm/v/@11ty/eleventy-fetch.svg?style=for-the-badge)](https://www.npmjs.com/package/@11ty/eleventy-fetch) [![GitHub issues](https://img.shields.io/github/issues/11ty/eleventy-fetch.svg?style=for-the-badge)](https://github.com/11ty/eleventy/issues)

## Installation

```
npm install @11ty/eleventy-cache-assets
npm install @11ty/eleventy-fetch
```

_[The full `eleventy-cache-assets` documentation is on 11ty.dev](https://www.11ty.dev/docs/plugins/cache/)._
_[The full `eleventy-fetch` documentation is on 11ty.dev](https://www.11ty.dev/docs/plugins/cache/)._

## Tests

Expand All @@ -46,3 +46,9 @@ npm run test
* `flat-cache` save method seems to be synchronous, is there a better async one?
* Our cache stores raw buffers internally, which are pretty bloated compared to the original. Surely there is a more efficient way to do this. Maybe store the files in their original format.
-->

## Community Roadmap

- [Top Feature Requests](https://github.com/11ty/eleventy-fetch/issues?q=label%3Aneeds-votes+sort%3Areactions-%2B1-desc+label%3Aenhancement) (Add your own votes using the 👍 reaction)
- [Top Bugs 😱](https://github.com/11ty/eleventy-fetch/issues?q=is%3Aissue+is%3Aopen+label%3Abug+sort%3Areactions-%2B1-desc) (Add your own votes using the 👍 reaction)
- [Newest Bugs 🙀](https://github.com/11ty/eleventy-fetch/issues?q=is%3Aopen+is%3Aissue+label%3Abug)
File renamed without changes.
14 changes: 7 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
{
"name": "@11ty/eleventy-cache-assets",
"name": "@11ty/eleventy-fetch",
"version": "2.3.0",
"description": "Cache remote assets, locally (automatically).",
"description": "Fetch and locally cache remote API calls and assets.",
"publishConfig": {
"access": "public"
},
"repository": {
"type": "git",
"url": "git+https://github.com/11ty/eleventy-cache-assets.git"
"url": "git+https://github.com/11ty/eleventy-fetch.git"
},
"main": "eleventy-cache-assets.js",
"main": "eleventy-fetch.js",
"scripts": {
"test": "ava",
"sample": "node sample"
},
"files": [
"src/",
"eleventy-cache-assets.js"
"eleventy-fetch.js"
],
"engines": {
"node": ">=12"
Expand All @@ -36,9 +36,9 @@
},
"license": "MIT",
"bugs": {
"url": "https://github.com/11ty/eleventy-cache-assets/issues"
"url": "https://github.com/11ty/eleventy-fetch/issues"
},
"homepage": "https://github.com/11ty/eleventy-cache-assets#readme",
"homepage": "https://github.com/11ty/eleventy-fetch#readme",
"devDependencies": {
"ava": "^4.0.1"
},
Expand Down
2 changes: 1 addition & 1 deletion src/AssetCache.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ class AssetCache {
}

get cacheFilename() {
return `eleventy-cache-assets-${this.hash}`;
return `eleventy-fetch-${this.hash}`;
}

get rootDir() {
Expand Down
6 changes: 3 additions & 3 deletions src/RemoteAssetCache.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,15 +57,15 @@ class RemoteAssetCache extends AssetCache {

let type = optionsOverride.type || this.options.type;
let body = await this.getResponseValue(response, type);
console.log( `[11ty/eleventy-cache-assets] ${isDryRun? "Fetching" : "Caching"}: ${this.cleanUrl}` );
console.log( `[11ty/eleventy-fetch] ${isDryRun? "Fetching" : "Caching"}: ${this.displayUrl}` );
if(!isDryRun) {
await super.save(body, type);
}
return body;
} catch(e) {
if(this.cachedObject) {
console.log( `[11ty/eleventy-cache-assets] Error fetching ${this.cleanUrl}. Message: ${e.message}`);
console.log( `[11ty/eleventy-cache-assets] Failing gracefully with an expired cache entry.` );
console.log( `[11ty/eleventy-fetch] Error fetching ${this.displayUrl}. Message: ${e.message}`);
console.log( `[11ty/eleventy-fetch] Failing gracefully with an expired cache entry.` );
return super.getCachedValue();
} else {
return Promise.reject(e);
Expand Down
8 changes: 4 additions & 4 deletions test/AssetCacheTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,15 @@ test("Absolute path cache directory", t => {
let cache = new AssetCache("lksdjflkjsdf", "/tmp/.cache");
let cachePath = normalizePath(cache.cachePath);

t.is(cachePath, "/tmp/.cache/eleventy-cache-assets-lksdjflkjsdf");
t.is(cachePath, "/tmp/.cache/eleventy-fetch-lksdjflkjsdf");
});

test("Relative path cache directory", t => {
let cache = new AssetCache("lksdjflkjsdf", ".cache");
let cachePath = normalizePath(cache.cachePath);

t.not(cachePath, ".cache/eleventy-cache-assets-lksdjflkjsdf");
t.true(cachePath.endsWith(".cache/eleventy-cache-assets-lksdjflkjsdf"));
t.not(cachePath, ".cache/eleventy-fetch-lksdjflkjsdf");
t.true(cachePath.endsWith(".cache/eleventy-fetch-lksdjflkjsdf"));
});

test("AWS Lambda root directory resolves correctly", t => {
Expand All @@ -36,7 +36,7 @@ test("AWS Lambda root directory resolves correctly", t => {
let cache = new AssetCache("lksdjflkjsdf", ".cache");
let cachePath = normalizePath(cache.cachePath);

t.is(cachePath, `${cwd}/.cache/eleventy-cache-assets-lksdjflkjsdf`);
t.is(cachePath, `${cwd}/.cache/eleventy-fetch-lksdjflkjsdf`);
delete "ELEVENTY_ROOT" in process.env;
delete "LAMBDA_TASK_ROOT" in process.env;
});
Expand Down
1 change: 0 additions & 1 deletion test/QueueTest.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
const test = require("ava");
const fs = require("fs");
const shorthash = require("short-hash");
const Cache = require("../");
const RemoteAssetCache = require("../src/RemoteAssetCache");

Expand Down
8 changes: 4 additions & 4 deletions test/RemoteAssetCacheTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,13 @@ test("getDurationMs", t => {

test("Local hash file names", async t => {
let pngUrl = "https://www.zachleat.com/img/avatar-2017-big.png";
t.is((new RemoteAssetCache(pngUrl)).cachePath, path.resolve(".", `.cache/eleventy-cache-assets-${shorthash(pngUrl)}`));
t.is((new RemoteAssetCache(pngUrl)).cachePath, path.resolve(".", `.cache/eleventy-fetch-${shorthash(pngUrl)}`));

let fontUrl = "https://www.zachleat.com/font.woff";
t.is((new RemoteAssetCache(fontUrl)).cachePath, path.resolve(".", `.cache/eleventy-cache-assets-${shorthash(fontUrl)}`));
t.is((new RemoteAssetCache(fontUrl)).cachePath, path.resolve(".", `.cache/eleventy-fetch-${shorthash(fontUrl)}`));

let fontUrl2 = "https://www.zachleat.com/font.woff2";
t.is((new RemoteAssetCache(fontUrl2)).cachePath, path.resolve(".", `.cache/eleventy-cache-assets-${shorthash(fontUrl2)}`));
t.is((new RemoteAssetCache(fontUrl2)).cachePath, path.resolve(".", `.cache/eleventy-fetch-${shorthash(fontUrl2)}`));
});

test("Clean url", async t => {
Expand All @@ -44,7 +44,7 @@ test("Clean url", async t => {

test("Local hash without file extension in URL", async t => {
let noExt = "https://twitter.com/zachleat/profile_image?size=bigger";
t.is((new RemoteAssetCache(noExt)).cachePath, path.resolve(".", `.cache/eleventy-cache-assets-${shorthash(noExt)}`));
t.is((new RemoteAssetCache(noExt)).cachePath, path.resolve(".", `.cache/eleventy-fetch-${shorthash(noExt)}`));
});

test("Fetching!", async t => {
Expand Down

0 comments on commit c180d03

Please sign in to comment.