@pirateandfox/pagestack 0.1.0-beta.21 → 0.1.0-beta.22

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/CHANGELOG.md CHANGED
@@ -1,5 +1,10 @@
1
1
  # Changelog
2
2
 
3
+ ## 0.1.0-beta.22 — 2026-11-29
4
+
5
+ - **Globals in the editor show what is saved, through `liveGlobal`.** A component that imported a global (`import menus from '../data/menus.json'`) got the copy built into the editor, so on a deployed editor the header, footer and banners in previews, Browse and `liveRoutes` pages showed the global as of the editor's last deploy. A change saved since, in the editor's global screen, by a commit or by an automation, showed on the public site but not in the editor. New in `@pirateandfox/pagestack/live`: `liveGlobal(id, importedFile)` returns the file as stored now inside an editor request, and the imported file everywhere else, so the public build is unchanged. A missing or invalid stored file also gives the imported file. On GitHub storage all globals are read in one request and kept until the branch moves. Hosts opt in by switching their imports; see README, "Globals in the editor". Changes to a site's code still need the editor redeployed.
6
+ - **`examples/bakery`** reads `site` through `liveGlobal`.
7
+
3
8
  ## 0.1.0-beta.21 — 2026-11-28
4
9
 
5
10
  - **An item's title can always be edited.** In the page view the Title box was hidden, and the title could only be edited by clicking it on the page, which needs the site's layout to mark it with `itemField(editing, 'title')`. A layout that didn't left the title uneditable there. Now the Title box shows above the page whenever the page has no editable title. Sites should still mark their heading, so the title is edited in place.
package/README.md CHANGED
@@ -499,6 +499,28 @@ able to render where the editor runs (on Workers: no `node:fs`, and nothing from
499
499
  until the branch moves, so a list costs one request to GitHub once the first one has warmed up.
500
500
  These pages show unpublished work, so they need a signed-in editor.
501
501
 
502
+ ### Globals in the editor (`liveGlobal`)
503
+
504
+ A component that imports a global directly (`import menus from '../data/menus.json'`) gets the copy
505
+ Vite built into the editor. On a deployed editor, its header, footer and banners, in previews, Browse
506
+ and `liveRoutes` pages, then show the global as it was when the editor was deployed. A change saved
507
+ since, in the editor's own global screen, by a commit or by an automation, is on the public site but
508
+ not in the editor until it is deployed again. Read globals through `liveGlobal` instead:
509
+
510
+ ```astro
511
+ ---
512
+ import menusFile from '../data/menus.json';
513
+ import { liveGlobal } from '@pirateandfox/pagestack/live';
514
+ const menus = await liveGlobal('menus', menusFile);
515
+ ---
516
+ ```
517
+
518
+ The id is the global's key in `globals`. Inside an editor request `liveGlobal` returns the file as
519
+ stored now; everywhere else (the public build, prerendering, `astro dev` without the editor) it
520
+ returns the imported file, so the public site is unchanged. A stored file that is missing or isn't
521
+ valid JSON also gives the imported file. On GitHub storage every global is read in one request and
522
+ kept until the branch moves.
523
+
502
524
  ## Security
503
525
 
504
526
  Everything an editor sends is treated as untrusted. On save, every field is cleaned by its type,
@@ -619,6 +641,10 @@ built site pages are served as plain files, without a sign-in; put the host behi
619
641
  if staged work must stay private.) Every
620
642
  editor response is `noindex` and `no-store`.
621
643
 
644
+ In the editor, globals imported directly show the copy from the editor's last deploy; read them
645
+ through `liveGlobal` (see "Globals in the editor"). Changes to the site's code and sections still need
646
+ a redeploy of the editor.
647
+
622
648
  ## Options
623
649
 
624
650
  | option | default | |
package/live.d.mts CHANGED
@@ -22,3 +22,13 @@ export function liveEntries(
22
22
  collection: string,
23
23
  schema?: { parse(data: unknown): any },
24
24
  ): Promise<LiveEntry[] | null>;
25
+
26
+ /**
27
+ * On the editor's server (its previews, Browse, `liveRoutes` pages): the global's JSON as stored now.
28
+ * Everywhere else, including the public build, and when the stored file is missing or invalid:
29
+ * `fallback`, the statically imported file.
30
+ *
31
+ * @param id The editor's global id (a key of `globals` in the sections module).
32
+ * @param fallback The statically imported file.
33
+ */
34
+ export function liveGlobal<T>(id: string, fallback: T): Promise<T>;
package/live.mjs CHANGED
@@ -8,6 +8,9 @@
8
8
  // import { liveEntries } from '@pirateandfox/pagestack/live';
9
9
  // const all = (await liveEntries('articles', articleSchema)) ?? (await getCollection('articles'));
10
10
  //
11
+ // Globals (menus, footer, site details) work the same way through `liveGlobal`: the file as stored
12
+ // now on the editor's server, the statically imported copy everywhere else.
13
+ //
11
14
  // Plain JavaScript with no imports, so a public build that never loads the editor can import it.
12
15
 
13
16
  /**
@@ -28,3 +31,23 @@ export async function liveEntries(collection, schema) {
28
31
  }
29
32
  return out;
30
33
  }
34
+
35
+ /**
36
+ * A global's JSON as stored now, on the editor's server (its previews, Browse, `liveRoutes` pages);
37
+ * `fallback` everywhere else: the public build, prerendering, astro dev without the editor. Also
38
+ * `fallback` when the stored file is missing or isn't valid JSON.
39
+ *
40
+ * import menusFile from '../data/menus.json';
41
+ * import { liveGlobal } from '@pirateandfox/pagestack/live';
42
+ * const menus = await liveGlobal('menus', menusFile);
43
+ *
44
+ * @template T
45
+ * @param {string} id The editor's global id (a key of `globals` in the sections module).
46
+ * @param {T} fallback The statically imported file.
47
+ * @returns {Promise<T>}
48
+ */
49
+ export async function liveGlobal(id, fallback) {
50
+ const live = /** @type {any} */ (globalThis).__PS_LIVE_GLOBAL__;
51
+ if (typeof live !== 'function') return fallback;
52
+ try { return (await live(id)) ?? fallback; } catch { return fallback; }
53
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@pirateandfox/pagestack",
3
- "version": "0.1.0-beta.21",
3
+ "version": "0.1.0-beta.22",
4
4
  "description": "A block editor for Astro sites. Content stays in your repo, saves are git commits, the public site stays static, sign-in is passkeys.",
5
5
  "license": "MIT",
6
6
  "type": "module",
@@ -1,10 +1,11 @@
1
- // Collection entries straight from storage, for host list pages rendered on the editor's server
2
- // (see live.mjs). The collection name is the editor's collection id.
1
+ // Collection entries and globals straight from storage, for host pages rendered on the editor's
2
+ // server (see live.mjs). The collection name is the editor's collection id, a global's its id.
3
3
  //
4
- // With a store that knows its head commit (GitHub), entries are kept until the branch moves, and
5
- // the head is asked at most once a second, so a page that lists four collections costs one request.
4
+ // With a store that knows its head commit (GitHub), what was read is kept until the branch moves,
5
+ // and the head is asked at most once a second, so a page that lists four collections and shows two
6
+ // globals costs two requests.
6
7
  import { AsyncLocalStorage } from 'node:async_hooks';
7
- import { collections, storage } from './context';
8
+ import { collections, globals, storage } from './context';
8
9
  import { parseMarkdown } from '../core/frontmatter';
9
10
 
10
11
  export interface LiveEntry { id: string; collection: string; data: Record<string, any>; body: string; filePath: string }
@@ -63,17 +64,51 @@ export async function liveCollection(name: string, now = Date.now()): Promise<Li
63
64
  return entries;
64
65
  }
65
66
 
66
- type Read = (name: string) => Promise<LiveEntry[] | null>;
67
+ let cachedGlobals: { sha: string; files: Promise<Record<string, string | null>> } | null = null;
67
68
 
68
69
  /**
69
- * Runs `run` with live.mjs's `liveEntries` giving stored entries, for that request alone: its whole
70
- * render, streamed parts included. Anything else in the process must keep getting null. In dev the
71
- * same process runs the site's getStaticPaths, and a stored entry has no rendered HTML, so a post
72
- * page built from one came out blank. Kept on globalThis because dev can load this module twice.
70
+ * A global's JSON as stored now, or null when the editor doesn't know the id, the file is missing or
71
+ * isn't valid JSON: the host then shows the copy it was built with rather than a broken header.
72
+ * At a head commit every global is read in one request, since a header and a footer render together.
73
+ * Parsed on each call, so a component that changes what it gets can't change it for the next one.
74
+ */
75
+ export async function liveGlobalFile(id: string, now = Date.now()): Promise<unknown> {
76
+ const g = Object.hasOwn(globals, id) ? globals[id] : null;
77
+ if (!g) return null;
78
+ const sha = await currentHead(now);
79
+ let files: Promise<Record<string, string | null>>;
80
+ if (!sha) files = storage().readFiles([g.file]); // local storage: the working tree, read every time
81
+ else {
82
+ if (cachedGlobals?.sha !== sha) {
83
+ const mine = { sha, files: storage().readFiles([...new Set(Object.values(globals).map((x) => x.file))], sha) };
84
+ // A failed read is not kept: the next request tries again.
85
+ mine.files.catch(() => { if (cachedGlobals === mine) cachedGlobals = null; });
86
+ cachedGlobals = mine;
87
+ }
88
+ files = cachedGlobals.files;
89
+ }
90
+ const raw = (await files)[g.file];
91
+ if (typeof raw !== 'string') return null;
92
+ try { return JSON.parse(raw); } catch { return null; }
93
+ }
94
+
95
+ interface Scope { entries(name: string): Promise<LiveEntry[] | null>; global(id: string): Promise<unknown> }
96
+
97
+ /**
98
+ * Runs `run` with live.mjs's `liveEntries` and `liveGlobal` giving what is stored, for that request
99
+ * alone: its whole render, streamed parts included. Anything else in the process must keep getting
100
+ * null. In dev the same process runs the site's getStaticPaths, and a stored entry has no rendered
101
+ * HTML, so a post page built from one came out blank. Kept on globalThis because dev can load this
102
+ * module twice; the readers are set each time, so a reloaded module's shape always matches.
73
103
  */
74
104
  export function withLive<T>(run: () => T): T {
75
- const g = globalThis as { __PS_LIVE_SCOPE__?: AsyncLocalStorage<Read>; __PS_LIVE__?: (name: string) => ReturnType<Read> | null };
76
- const scope = (g.__PS_LIVE_SCOPE__ ??= new AsyncLocalStorage<Read>());
77
- g.__PS_LIVE__ ??= (name) => scope.getStore()?.(name) ?? null;
78
- return scope.run((name) => liveCollection(name), run);
105
+ const g = globalThis as {
106
+ __PS_LIVE_SCOPE__?: AsyncLocalStorage<Scope>;
107
+ __PS_LIVE__?: (name: string) => Promise<LiveEntry[] | null> | null;
108
+ __PS_LIVE_GLOBAL__?: (id: string) => Promise<unknown> | null;
109
+ };
110
+ const scope = (g.__PS_LIVE_SCOPE__ ??= new AsyncLocalStorage<Scope>());
111
+ g.__PS_LIVE__ = (name) => scope.getStore()?.entries(name) ?? null;
112
+ g.__PS_LIVE_GLOBAL__ = (id) => scope.getStore()?.global(id) ?? null;
113
+ return scope.run({ entries: (name) => liveCollection(name), global: (id) => liveGlobalFile(id) }, run);
79
114
  }