@lumeland/core 1.0.0-beta-2 → 1.0.0-beta-3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/build/src/site.js CHANGED
@@ -6,6 +6,7 @@ import { log } from "./utils/log.js";
6
6
  import { filter404page } from "./utils/page_url.js";
7
7
  import { insertContent } from "./utils/page_content.js";
8
8
  import { getFile, getFiles, isFromCdn } from "./utils/cdn.js";
9
+ import { setCache } from "./utils/read.js";
9
10
  import FS from "./fs.js";
10
11
  import { compileCSS, compileJS, ComponentLoader } from "./components.js";
11
12
  import DataLoader from "./data_loader.js";
@@ -195,6 +196,7 @@ export default class Site {
195
196
  // Initialize the cache if LUME_NOCACHE is not enabled
196
197
  if (envBoolean("LUME_NOCACHE") !== true) {
197
198
  this.cache = new Cache({ folder: this.root("_cache") });
199
+ setCache(this.cache);
198
200
  }
199
201
  // Initialize the debug bar
200
202
  const initDebugBar = this.options.server.debugBar ?? envBoolean("LUME_LIVE_RELOAD");
@@ -1,10 +1,11 @@
1
+ import type Cache from "../cache.ts";
2
+ export declare function setCache(instance: Cache): void;
1
3
  /**
2
4
  * Read a local or remote file and return its content.
3
5
  * If the file is remote, it will be cached in the `lume_remote_files` cache.
4
6
  */
5
- export declare function read(path: string, isBinary: boolean): Promise<Uint8Array<ArrayBuffer> | string>;
6
- export declare function read(path: string, isBinary: true, init?: RequestInit): Promise<Uint8Array<ArrayBuffer>>;
7
7
  export declare function read(path: string, isBinary: false, init?: RequestInit): Promise<string>;
8
+ export declare function read(path: string, isBinary: true, init?: RequestInit): Promise<Uint8Array<ArrayBuffer>>;
8
9
  /** Read a text file like a browser */
9
10
  export declare function readFile(path: string): Promise<string>;
10
11
  /**
@@ -1,9 +1,11 @@
1
1
  import { isUrl } from "./path.js";
2
- import { envBoolean } from "./env.js";
3
2
  import { tokens } from "./tokens.js";
4
3
  import { readFile as readFile_, readTextFile } from "../services/fs.js";
5
4
  import { fromFileUrl } from "../../deps/path.js";
6
- const useCache = envBoolean("LUME_NOCACHE") !== true;
5
+ let cache;
6
+ export function setCache(instance) {
7
+ cache = instance;
8
+ }
7
9
  export async function read(path, isBinary, init) {
8
10
  if (!isUrl(path)) {
9
11
  if (path.startsWith("data:")) {
@@ -23,23 +25,9 @@ export async function read(path, isBinary, init) {
23
25
  headers.set("Authorization", authorization);
24
26
  init = { ...init, headers };
25
27
  }
26
- if (!useCache) {
27
- const response = await fetch(url, init);
28
- if (!response.ok) {
29
- throw new Error(`Failed to fetch "${url}"`);
30
- }
31
- return isBinary ? new Uint8Array(await response.arrayBuffer()) : response.text();
32
- }
33
- const cache = await caches.open("lume_remote_files");
34
- // Prevent https://github.com/denoland/deno/issues/19696
35
- try {
36
- const cached = await cache.match(url);
37
- if (cached) {
38
- return isBinary ? new Uint8Array(await cached.arrayBuffer()) : cached.text();
39
- }
40
- }
41
- catch {
42
- // ignore
28
+ const cached = isBinary ? cache?.getBytes([url]) : cache?.getText([url]);
29
+ if (cached) {
30
+ return cached;
43
31
  }
44
32
  const response = await fetch(url, {
45
33
  signal: AbortSignal.timeout(1000 * 10), // 10s
@@ -48,8 +36,9 @@ export async function read(path, isBinary, init) {
48
36
  if (!response.ok) {
49
37
  throw new Error(`Failed to fetch "${url}"`);
50
38
  }
51
- await cache.put(url, response.clone());
52
- return isBinary ? new Uint8Array(await response.arrayBuffer()) : response.text();
39
+ const content = isBinary ? new Uint8Array(await response.arrayBuffer()) : await response.text();
40
+ cache?.set([url], content);
41
+ return content;
53
42
  }
54
43
  /** Read a text file like a browser */
55
44
  export async function readFile(path) {
@@ -63,5 +52,5 @@ export async function readFile(path) {
63
52
  * Clear the cache of remote files.
64
53
  */
65
54
  export async function clearCache() {
66
- await caches.delete("lume_remote_files");
55
+ cache?.clear();
67
56
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lumeland/core",
3
- "version": "1.0.0-beta-2",
3
+ "version": "1.0.0-beta-3",
4
4
  "description": "The core of Lume SSG",
5
5
  "license": "MIT",
6
6
  "files": [