Skip to content

Commit

Permalink
Cache the remote files with Web Cache API
Browse files Browse the repository at this point in the history
  • Loading branch information
oscarotero committed May 12, 2023
1 parent c7864dc commit 411602a
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ Any BREAKING CHANGE between minor versions will be documented here in upper case
- New `site.searcher` property with a instance of `Searcher` class.
It's used by plugins like `search`, `nav`, `sitemap` and `feed`.
(Previouly, each plugin had it's own instance).
- Cache the remote files using Web Cache API.

## [1.17.3] - 2023-05-10
### Fixed
Expand Down
18 changes: 17 additions & 1 deletion core/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -444,7 +444,23 @@ export async function read(
return isBinary ? Deno.readFile(path) : Deno.readTextFile(path);
}

const response = await fetch(path);
const url = new URL(path);

if (url.protocol === "file:") {
return isBinary ? Deno.readFile(url) : Deno.readTextFile(url);
}

const cache = await caches.open("lume_remote_files");
const cached = await cache.match(url);

if (cached) {
return isBinary
? new Uint8Array(await cached.arrayBuffer())
: cached.text();
}

const response = await fetch(url);
await cache.put(url, response.clone());

return isBinary
? new Uint8Array(await response.arrayBuffer())
Expand Down

0 comments on commit 411602a

Please sign in to comment.