@yysng/astro-boilerplate 1.1.35 → 1.1.36
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/package.json
CHANGED
|
@@ -1,17 +1,28 @@
|
|
|
1
1
|
import { CONTENT_REGISTRY } from "./registry.js";
|
|
2
2
|
|
|
3
|
+
const isEdge = typeof process === "undefined";
|
|
4
|
+
|
|
3
5
|
export async function loadContent(key, env = {}) {
|
|
4
6
|
const entry = CONTENT_REGISTRY[key];
|
|
5
7
|
if (!entry) throw new Error(`Unknown content key: ${key}`);
|
|
6
8
|
|
|
7
|
-
// Cloudflare runtime
|
|
9
|
+
// Cloudflare runtime (KV)
|
|
8
10
|
if (env?.CONTENT_KV) {
|
|
9
11
|
const stored = await env.CONTENT_KV.get(entry.file, "json");
|
|
10
12
|
if (stored) return stored;
|
|
11
13
|
throw new Error(`KV missing content for key "${key}"`);
|
|
12
14
|
}
|
|
13
15
|
|
|
14
|
-
//
|
|
15
|
-
|
|
16
|
+
// 🚨 Edge runtime must NEVER attempt local filesystem
|
|
17
|
+
if (isEdge) {
|
|
18
|
+
throw new Error(
|
|
19
|
+
`Edge runtime missing CONTENT_KV binding for "${key}". ` +
|
|
20
|
+
`Make sure you pass Astro.locals.runtime.env into loadContent() and your Pages project has CONTENT_KV bound.`
|
|
21
|
+
);
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
// Local dev only — prevent bundling into Worker by using @vite-ignore + variable specifier
|
|
25
|
+
const modPath = "./storage.node.js";
|
|
26
|
+
const { readLocal } = await import(/* @vite-ignore */ modPath);
|
|
16
27
|
return await readLocal(entry.file);
|
|
17
28
|
}
|
|
@@ -4,13 +4,21 @@ import { CONTENT_REGISTRY } from "./registry.js";
|
|
|
4
4
|
|
|
5
5
|
const isEdge = typeof process === "undefined";
|
|
6
6
|
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
7
|
+
// IMPORTANT:
|
|
8
|
+
// - On edge, never import node storage.
|
|
9
|
+
// - Use @vite-ignore + variable specifier to avoid bundling node fs into worker.
|
|
10
|
+
async function getStorage() {
|
|
11
|
+
if (isEdge) {
|
|
12
|
+
const p = "@yysng/astro-boilerplate/content-storage/edge";
|
|
13
|
+
return await import(/* @vite-ignore */ p);
|
|
14
|
+
} else {
|
|
15
|
+
const p = "@yysng/astro-boilerplate/content-storage/node";
|
|
16
|
+
return await import(/* @vite-ignore */ p);
|
|
17
|
+
}
|
|
18
|
+
}
|
|
10
19
|
|
|
11
20
|
function mergeDefined(existing, incoming) {
|
|
12
21
|
const result = { ...existing };
|
|
13
|
-
|
|
14
22
|
for (const k in incoming) {
|
|
15
23
|
const v = incoming[k];
|
|
16
24
|
if (v === undefined) continue;
|
|
@@ -21,7 +29,6 @@ function mergeDefined(existing, incoming) {
|
|
|
21
29
|
result[k] = v;
|
|
22
30
|
}
|
|
23
31
|
}
|
|
24
|
-
|
|
25
32
|
return result;
|
|
26
33
|
}
|
|
27
34
|
|
|
@@ -35,13 +42,21 @@ export async function updateContent(key, incoming, env = {}) {
|
|
|
35
42
|
|
|
36
43
|
schema.validate(merged);
|
|
37
44
|
|
|
38
|
-
// Cloudflare
|
|
45
|
+
// Cloudflare KV
|
|
39
46
|
if (env?.CONTENT_KV) {
|
|
40
47
|
await env.CONTENT_KV.put(entry.file, JSON.stringify(merged));
|
|
41
48
|
return merged;
|
|
42
49
|
}
|
|
43
50
|
|
|
44
|
-
//
|
|
51
|
+
// Edge must never write local
|
|
52
|
+
if (isEdge) {
|
|
53
|
+
throw new Error(
|
|
54
|
+
`Edge runtime missing CONTENT_KV binding for updateContent("${key}").`
|
|
55
|
+
);
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
// Local dev filesystem write
|
|
59
|
+
const { writeLocal } = await getStorage();
|
|
45
60
|
await writeLocal(entry.file, merged);
|
|
46
61
|
return merged;
|
|
47
62
|
}
|