create-shapio 0.4.1 → 0.5.1

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.
Files changed (56) hide show
  1. package/package.json +1 -1
  2. package/templates/astro/.env.example +6 -0
  3. package/templates/astro/README.md +9 -0
  4. package/templates/astro/package.json +7 -4
  5. package/templates/astro/scripts/seed.ts +77 -24
  6. package/templates/astro/scripts/smokeDrafts.ts +109 -0
  7. package/templates/astro/scripts/smokeHttp.ts +29 -1
  8. package/templates/astro/src/layouts/Base.astro +8 -0
  9. package/templates/astro/src/lib/config.ts +13 -1
  10. package/templates/astro/src/lib/renderTime.test.ts +31 -0
  11. package/templates/astro/src/lib/renderTime.ts +24 -0
  12. package/templates/astro/src/lib/shapio.ts +88 -64
  13. package/templates/astro/src/lib/site.ts +4 -0
  14. package/templates/astro/src/lib/snapshotResolver.test.ts +64 -0
  15. package/templates/astro/src/lib/snapshotResolver.ts +52 -0
  16. package/templates/astro/src/lib/types.ts +0 -9
  17. package/templates/astro/src/pages/[locale]/[...slug].astro +12 -1
  18. package/templates/astro/src/pages/[locale]/articles/[slug].astro +12 -1
  19. package/templates/astro/src/pages/[locale]/articles/index.astro +6 -1
  20. package/templates/astro/src/pages/build.json.ts +9 -2
  21. package/templates/astro/src/styles/site.css +14 -0
  22. package/templates/next/.env.example +10 -0
  23. package/templates/next/README.md +37 -11
  24. package/templates/next/next.config.ts +17 -12
  25. package/templates/next/package.json +4 -2
  26. package/templates/next/scripts/seed.ts +77 -24
  27. package/templates/next/scripts/smokeDrafts.ts +109 -0
  28. package/templates/next/scripts/smokeHttp.ts +29 -1
  29. package/templates/next/src/app/[locale]/layout.tsx +3 -0
  30. package/templates/next/src/app/api/revalidate/route.ts +3 -1
  31. package/templates/next/src/app/site.css +14 -0
  32. package/templates/next/src/components/DraftsBadge/index.tsx +10 -0
  33. package/templates/next/src/lib/config.ts +35 -1
  34. package/templates/next/src/lib/revalidation.ts +30 -12
  35. package/templates/next/src/lib/revalidationTags.test.ts +43 -0
  36. package/templates/next/src/lib/revalidationTags.ts +30 -0
  37. package/templates/next/src/lib/shapio.ts +42 -13
  38. package/templates/next/src/lib/shapioClient.ts +19 -0
  39. package/templates/next/src/lib/site.ts +4 -0
  40. package/templates/next/src/lib/snapshotResolver.test.ts +64 -0
  41. package/templates/next/src/lib/snapshotResolver.ts +52 -0
  42. package/templates/sveltekit/.env.example +6 -0
  43. package/templates/sveltekit/README.md +9 -0
  44. package/templates/sveltekit/package.json +7 -4
  45. package/templates/sveltekit/scripts/seed.ts +77 -24
  46. package/templates/sveltekit/scripts/smokeDrafts.ts +109 -0
  47. package/templates/sveltekit/scripts/smokeHttp.ts +29 -1
  48. package/templates/sveltekit/src/env.ts +2 -0
  49. package/templates/sveltekit/src/lib/server/config.ts +11 -1
  50. package/templates/sveltekit/src/lib/server/shapio.ts +28 -13
  51. package/templates/sveltekit/src/lib/server/snapshotResolver.test.ts +64 -0
  52. package/templates/sveltekit/src/lib/server/snapshotResolver.ts +52 -0
  53. package/templates/sveltekit/src/lib/site.css +14 -0
  54. package/templates/sveltekit/src/lib/site.ts +4 -0
  55. package/templates/sveltekit/src/routes/[locale]/+layout.server.ts +3 -2
  56. package/templates/sveltekit/src/routes/[locale]/+layout.svelte +3 -0
@@ -1,14 +1,30 @@
1
- import { createClient, ShapioApiError, type DeliveryListQuery, type DeliverySite } from '@shapio/client';
2
- import { deliveryToken, shapioUrl, siteKey } from './config';
1
+ import {
2
+ ShapioApiError,
3
+ type DeliveryListQuery,
4
+ type DeliverySite,
5
+ type ShapioDeliveryClient,
6
+ } from '@shapio/client';
7
+ import { configuredSnapshot, isDevServer, isDraftsMode } from './config';
3
8
  import { liveSnapshot } from './liveSnapshot';
9
+ import { createShapioClient } from './shapioClient';
4
10
  import type { Locale } from './site';
11
+ import { createSnapshotResolver } from './snapshotResolver';
5
12
  import type { Article, Page, SiteSettings } from './types';
6
13
 
7
14
  /**
8
- * The site's read side: Shapio's delivery API through `@shapio/client`, at one publication snapshot. Next
15
+ * The site's read side: Shapio's delivery API (over HTTP, or in this process with SHAPIO_MODE=in-process:
16
+ * src/lib/shapioClient.ts), at one publication snapshot. Next
9
17
  * renders pages in several worker processes, so the snapshot is pinned once in next.config.ts (it sets
10
18
  * SHAPIO_SNAPSHOT for the whole build) and every request here sends it. Under `next start`, /api/revalidate
11
- * moves that snapshot forward (src/lib/liveSnapshot.ts).
19
+ * moves that snapshot forward (src/lib/liveSnapshot.ts). `next dev` pins nothing: it reads the current snapshot
20
+ * again once the last read is a second old, so a publish shows on reload (src/lib/snapshotResolver.ts). Drafts
21
+ * mode (SHAPIO_DRAFTS=true) reads saved drafts, which have no snapshot: no read sends one, and every read is
22
+ * fresh (the client never caches drafts).
23
+ *
24
+ * The client tags every read with the site, model and entry, and /api/revalidate expires the tags of what
25
+ * changed (src/lib/revalidationTags.ts) as well as the pages. No `next: { cache }` option: reads pinned to a
26
+ * snapshot are `force-cache` regardless, and `site.get()` (no snapshot) stays fresh on every build, since Next's
27
+ * data cache outlives `next build`.
12
28
  */
13
29
  const PAGE_SIZE = 100;
14
30
 
@@ -22,13 +38,25 @@ const PAGE_FIELDS = ['title', 'slug', 'description', 'sections', 'seo'];
22
38
  const ARTICLE_FIELDS = ['title', 'slug', 'excerpt', 'body', 'cover', 'author', 'publishedOn', 'seo'];
23
39
  const SITE_SETTINGS_FIELDS = ['siteName', 'tagline', 'footer', 'colophon'];
24
40
 
25
- export const createShapio = () =>
26
- createClient({ baseUrl: shapioUrl(), token: deliveryToken(), site: siteKey() });
41
+ let client: ShapioDeliveryClient | undefined;
42
+ export const shapio = () => (client ??= createShapioClient());
27
43
 
28
- let client: ReturnType<typeof createShapio> | undefined;
29
- export const shapio = () => (client ??= createShapio());
44
+ let devSnapshot: (() => Promise<number>) | undefined;
30
45
 
31
- const snapshot = liveSnapshot;
46
+ /**
47
+ * The build's snapshot (moved forward by /api/revalidate); under `next dev`, the current one; in drafts mode,
48
+ * none (drafts cannot be pinned).
49
+ */
50
+ const snapshot = async (): Promise<number | undefined> =>
51
+ isDraftsMode()
52
+ ? undefined
53
+ : isDevServer()
54
+ ? (devSnapshot ??= createSnapshotResolver({
55
+ dev: true,
56
+ configured: configuredSnapshot(),
57
+ current: async () => (await shapio().snapshots.current()).snapshot,
58
+ }))()
59
+ : liveSnapshot();
32
60
 
33
61
  /** Every published entry of a collection in one locale, page by page, at the pinned snapshot. */
34
62
  const listAll = async <T>(routeKey: string, query: DeliveryListQuery): Promise<T[]> => {
@@ -40,7 +68,7 @@ const listAll = async <T>(routeKey: string, query: DeliveryListQuery): Promise<T
40
68
  // its SEO fields with the site's defaults filled in.
41
69
  richText: 'html',
42
70
  seo: 'resolved',
43
- snapshot: snapshot(),
71
+ snapshot: await snapshot(),
44
72
  page,
45
73
  pageSize: PAGE_SIZE,
46
74
  });
@@ -66,7 +94,7 @@ export const getSiteSettings = async (locale: Locale): Promise<SiteSettings | nu
66
94
  try {
67
95
  const { data } = await shapio().delivery.singleton<SiteSettings>(SITE_SETTINGS, {
68
96
  locale,
69
- snapshot: snapshot(),
97
+ snapshot: await snapshot(),
70
98
  fields: SITE_SETTINGS_FIELDS,
71
99
  richText: 'html',
72
100
  });
@@ -80,8 +108,9 @@ export const getSiteSettings = async (locale: Locale): Promise<SiteSettings | nu
80
108
  };
81
109
 
82
110
  /**
83
- * The site and its SEO defaults (name and title template per locale, default image, Twitter handle). Read on
84
- * every render: a `site.updated` webhook revalidates every page, which then shows the new defaults. Null when
111
+ * The site and its SEO defaults (name and title template per locale, default image, Twitter handle). Cached
112
+ * under the site tag: a `site.updated` webhook expires it and revalidates every page, which then shows the new
113
+ * defaults. Null when
85
114
  * the Shapio predates them (the pages still render, with plain titles).
86
115
  */
87
116
  export const getSite = async (): Promise<DeliverySite | null> => {
@@ -0,0 +1,19 @@
1
+ import { createClient, type ShapioDeliveryClient } from '@shapio/client';
2
+ import { createLocalClient } from '@shapio/local';
3
+ import { databaseUrl, deliveryToken, isDraftsMode, shapioMode, shapioUrl, siteKey } from './config';
4
+
5
+ /**
6
+ * The client the site reads with: Shapio's delivery API over HTTP (SHAPIO_MODE=http, the default), or the same
7
+ * API in this process (SHAPIO_MODE=in-process, `@shapio/local`), reading Shapio's database directly. Both
8
+ * expose the same `delivery`, `site` and `snapshots` calls and answer them identically, with the same delivery
9
+ * token deciding what the site may read. SHAPIO_DRAFTS=true: drafts mode (`drafts`), saved drafts instead of
10
+ * published content; Shapio refuses it with DRAFTS_FORBIDDEN unless the token's role grants Read drafts.
11
+ */
12
+ export const createShapioClient = (): ShapioDeliveryClient & { close?: () => Promise<void> } => {
13
+ const site = siteKey();
14
+ const drafts = isDraftsMode();
15
+ if (shapioMode() === 'in-process') {
16
+ return createLocalClient({ databaseUrl: databaseUrl(), token: deliveryToken(), site, drafts });
17
+ }
18
+ return createClient({ baseUrl: shapioUrl(), token: deliveryToken(), site, drafts });
19
+ };
@@ -19,6 +19,8 @@ const STRINGS = {
19
19
  by: 'By',
20
20
  switchTo: 'Français',
21
21
  skipToContent: 'Skip to content',
22
+ drafts: 'Drafts',
23
+ draftsNote: 'Drafts mode: this server shows saved drafts, not the published site.',
22
24
  footer: 'Built with Shapio',
23
25
  previewBanner: 'Preview: this is a draft, not the published page.',
24
26
  previewLoading: 'Loading the draft…',
@@ -32,6 +34,8 @@ const STRINGS = {
32
34
  by: 'Par',
33
35
  switchTo: 'English',
34
36
  skipToContent: 'Aller au contenu',
37
+ drafts: 'Brouillons',
38
+ draftsNote: 'Mode brouillons : ce serveur montre les brouillons enregistrés, pas le site publié.',
35
39
  footer: 'Construit avec Shapio',
36
40
  previewBanner: 'Aperçu : ceci est un brouillon, pas la page publiée.',
37
41
  previewLoading: 'Chargement du brouillon…',
@@ -0,0 +1,64 @@
1
+ import { describe, expect, it, vi } from 'vitest';
2
+ import { cachedRead, createSnapshotResolver, DEV_WINDOW_MS } from './snapshotResolver';
3
+
4
+ const clock = (start = 0) => {
5
+ let time = start;
6
+ return { now: () => time, advance: (ms: number) => (time += ms) };
7
+ };
8
+
9
+ describe('createSnapshotResolver', () => {
10
+ it('pins the first snapshot for the whole build', async () => {
11
+ const { now, advance } = clock();
12
+ const current = vi.fn().mockResolvedValueOnce(7).mockResolvedValue(8);
13
+ const snapshot = createSnapshotResolver({ dev: false, configured: undefined, current, now });
14
+
15
+ expect(await snapshot()).toBe(7);
16
+ advance(60 * 60 * 1000);
17
+ expect(await snapshot()).toBe(7);
18
+ expect(current).toHaveBeenCalledTimes(1);
19
+ });
20
+
21
+ it('reuses a dev read within the window and reads the current snapshot again after it', async () => {
22
+ const { now, advance } = clock();
23
+ const current = vi.fn().mockResolvedValueOnce(7).mockResolvedValue(8);
24
+ const snapshot = createSnapshotResolver({ dev: true, configured: undefined, current, now });
25
+
26
+ expect(await snapshot()).toBe(7);
27
+ advance(DEV_WINDOW_MS - 1);
28
+ expect(await snapshot()).toBe(7);
29
+ expect(current).toHaveBeenCalledTimes(1);
30
+
31
+ advance(1);
32
+ expect(await snapshot()).toBe(8);
33
+ expect(current).toHaveBeenCalledTimes(2);
34
+ });
35
+
36
+ it('keeps SHAPIO_SNAPSHOT in dev and in builds, without asking Shapio', async () => {
37
+ const current = vi.fn().mockResolvedValue(8);
38
+ for (const dev of [true, false]) {
39
+ const snapshot = createSnapshotResolver({ dev, configured: 3, current });
40
+ expect(await snapshot()).toBe(3);
41
+ }
42
+ expect(current).not.toHaveBeenCalled();
43
+ });
44
+ });
45
+
46
+ describe('cachedRead', () => {
47
+ it('shares one read between calls that overlap it', async () => {
48
+ const read = vi.fn().mockResolvedValue('value');
49
+ const cached = cachedRead(read, DEV_WINDOW_MS, clock().now);
50
+
51
+ const [first, second] = await Promise.all([cached(), cached()]);
52
+ expect([first, second]).toEqual(['value', 'value']);
53
+ expect(read).toHaveBeenCalledTimes(1);
54
+ });
55
+
56
+ it('does not keep a failed read', async () => {
57
+ const read = vi.fn().mockRejectedValueOnce(new Error('down')).mockResolvedValue('value');
58
+ const cached = cachedRead(read, Number.POSITIVE_INFINITY, clock().now);
59
+
60
+ await expect(cached()).rejects.toThrow('down');
61
+ expect(await cached()).toBe('value');
62
+ expect(read).toHaveBeenCalledTimes(2);
63
+ });
64
+ });
@@ -0,0 +1,52 @@
1
+ /**
2
+ * Which publication snapshot the site reads, and for how long a read is reused. A build pins one snapshot
3
+ * for all its pages (SHAPIO_SNAPSHOT, else the current one when the build starts). The dev server reads the
4
+ * current snapshot again once the last read is older than DEV_WINDOW_MS, so a publish shows on reload while
5
+ * the reads of one page render still share a snapshot.
6
+ */
7
+ export const DEV_WINDOW_MS = 1000;
8
+
9
+ /** How long a cached read is reused: for the whole process in a build, DEV_WINDOW_MS in dev. */
10
+ export const cacheWindow = (dev: boolean): number => (dev ? DEV_WINDOW_MS : Number.POSITIVE_INFINITY);
11
+
12
+ /** `read`, reused for `windowMs` after it starts. A read that fails is not kept: the next call tries again. */
13
+ export const cachedRead = <T>(
14
+ read: () => Promise<T>,
15
+ windowMs: number,
16
+ now: () => number = Date.now,
17
+ ): (() => Promise<T>) => {
18
+ let cached: { value: Promise<T>; at: number } | undefined;
19
+ return () => {
20
+ const at = now();
21
+ if (cached && at - cached.at < windowMs) {
22
+ return cached.value;
23
+ }
24
+ const entry = { value: read(), at };
25
+ cached = entry;
26
+ entry.value.catch(() => {
27
+ if (cached === entry) {
28
+ cached = undefined;
29
+ }
30
+ });
31
+ return entry.value;
32
+ };
33
+ };
34
+
35
+ type SnapshotResolverOptions = {
36
+ /** True under the dev server. */
37
+ dev: boolean;
38
+ /** SHAPIO_SNAPSHOT: when set, the site shows exactly this snapshot, in dev too. */
39
+ configured: number | undefined;
40
+ /** Reads Shapio's current publication snapshot. */
41
+ current: () => Promise<number>;
42
+ now?: () => number;
43
+ };
44
+
45
+ /** The snapshot every delivery read sends. */
46
+ export const createSnapshotResolver = ({
47
+ dev,
48
+ configured,
49
+ current,
50
+ now,
51
+ }: SnapshotResolverOptions): (() => Promise<number>) =>
52
+ configured === undefined ? cachedRead(current, cacheWindow(dev), now) : () => Promise.resolve(configured);
@@ -10,3 +10,9 @@ SHAPIO_DELIVERY_TOKEN=
10
10
  # PUBLIC_SHAPIO_URL=https://cms.example.com
11
11
  # Optional: this site's public origin; pages then carry a canonical URL and og:url.
12
12
  # SITE_URL=https://www.example.com
13
+ # Optional, development only: drafts mode. Every read shows saved drafts instead of published content, fresh
14
+ # on every request, with a "Drafts" badge on each page. Needs a delivery token whose role grants Read drafts:
15
+ # SHAPIO_DEV_DELIVERY_TOKEN (`npm run seed` writes one), else SHAPIO_DELIVERY_TOKEN. Never put that token or
16
+ # this flag in a production environment.
17
+ # SHAPIO_DRAFTS=true
18
+ # SHAPIO_DEV_DELIVERY_TOKEN=
@@ -28,6 +28,15 @@ one, read once when prerendering starts); `SHAPIO_SITE` picks the site on a mult
28
28
  | `vite.config.ts` | `frame-ancestors` for the servers and `build/_headers` |
29
29
  | `shapio/`, `scripts/seed.ts` | the models and the seed |
30
30
 
31
+ Drafts mode, for your development server: set `SHAPIO_DRAFTS=true` in `.env` and `npm run dev` shows saved
32
+ drafts instead of published content, fresh on every reload, with a **Drafts** badge on every page. Save in
33
+ Shapio, reload, and the change is there; publishing still rebuilds the live site. It reads with
34
+ `SHAPIO_DEV_DELIVERY_TOKEN`, the seed's `<site> dev` token, whose delivery role grants Read drafts (a token
35
+ without it fails with `DRAFTS_FORBIDDEN`). Never put that token or the flag in a production environment.
36
+ `npm run smoke:drafts` checks a running drafts-mode dev server
37
+ (http://localhost:5173; needs `SHAPIO_ADMIN_EMAIL`/`PASSWORD` to save a test change, which it puts back). Guide:
38
+ [drafts mode](https://github.com/mybrokengnome/shapio/blob/main/documentation/starters.md#drafts-mode).
39
+
31
40
  Preview: the seed creates a deployment connection named **Preview** that opens drafts at
32
41
  `http://localhost:5173/preview/` (`npm run dev`; set `SITE_URL` when seeding for another address). Add that
33
42
  origin to Shapio's `CORS_ORIGINS`. In Shapio's preview pane, clicking the title, body or cover focuses that
@@ -18,11 +18,13 @@
18
18
  "prepare": "svelte-kit sync || echo ''",
19
19
  "seed": "node --env-file-if-exists=.env --import tsx scripts/seed.ts --site-url http://localhost:5173",
20
20
  "smoke": "node --import tsx scripts/smokeHttp.ts --url http://localhost:4173",
21
- "typecheck": "svelte-kit sync && svelte-check --tsconfig ./tsconfig.json --fail-on-warnings"
21
+ "smoke:drafts": "node --env-file-if-exists=.env --import tsx scripts/smokeDrafts.ts --url http://localhost:5173",
22
+ "typecheck": "svelte-kit sync && svelte-check --tsconfig ./tsconfig.json --fail-on-warnings",
23
+ "test": "vitest run"
22
24
  },
23
25
  "dependencies": {
24
- "@shapio/client": "^0.4.1",
25
- "@shapio/visual": "^0.4.1"
26
+ "@shapio/client": "^0.5.1",
27
+ "@shapio/visual": "^0.5.1"
26
28
  },
27
29
  "devDependencies": {
28
30
  "@sveltejs/adapter-static": "4.0.0",
@@ -33,6 +35,7 @@
33
35
  "svelte-check": "4.7.6",
34
36
  "tsx": "4.23.15",
35
37
  "typescript": "6.0.3",
36
- "vite": "8.3.2"
38
+ "vite": "8.3.2",
39
+ "vitest": "5.0.3"
37
40
  }
38
41
  }
@@ -40,13 +40,16 @@ import { createPng } from './lib/png.js';
40
40
  * 5. with `--revalidate-path` (the Next.js starter's /api/revalidate), a webhook named "Site revalidation" that
41
41
  * sends publish, unpublish, delete, change set and schema events to SITE_URL + that path; its signing secret
42
42
  * (rotated on every run, as Shapio shows it only once) is written to .env as SHAPIO_WEBHOOK_SECRET;
43
- * 6. a delivery role and token for the build, written with SHAPIO_URL to .env in the current directory.
43
+ * 6. a delivery role and token for the build, written with SHAPIO_URL to .env in the current directory;
44
+ * 7. a second delivery role and token for development servers, "<site> dev", that may also read drafts (drafts
45
+ * mode: SHAPIO_DRAFTS=true), written to .env as SHAPIO_DEV_DELIVERY_TOKEN. Never put it in production.
44
46
  */
45
47
  const SCHEMA_DIR = resolve(import.meta.dirname, '..', 'shapio');
46
48
  const SCHEMA_KINDS = ['models', 'components'] as const;
47
49
  const SITE_MODELS = ['page', 'article', 'author', 'siteSettings'];
48
50
  const DELIVERY_ROLE_KEY = 'starter-delivery';
49
51
  const DELIVERY_TOKEN_NAME = 'starter build';
52
+ const DEV_ROLE_KEY = 'starter-dev';
50
53
  const PREVIEW_CONNECTION_NAME = 'Preview';
51
54
  /** Where Shapio opens a draft: the starter's /preview/ page, the token in the fragment (never sent to a server). */
52
55
  const PREVIEW_PATH = '/preview/?model={modelKey}&id={entryId}&locale={locale}#token={token}';
@@ -308,35 +311,71 @@ const upsertEntry = async (client: ShapioClient, spec: EntrySpec) => {
308
311
  log(`${spec.publish ? 'Published' : 'Saved draft'} ${spec.model} ${spec.slug} (en, fr)`);
309
312
  };
310
313
 
311
- /** A delivery role that reads the site's models, and a fresh token bound to it (older ones are revoked). */
312
- const createDeliveryToken = async (client: ShapioClient) => {
314
+ type DeliveryRoleSpec = {
315
+ key: string;
316
+ name: string;
317
+ description: string;
318
+ tokenName: string;
319
+ drafts: boolean;
320
+ };
321
+
322
+ /**
323
+ * A delivery role that reads the site's models (and, for development servers, their drafts), and a fresh token
324
+ * bound to it (older ones of the same name are revoked).
325
+ */
326
+ const createDeliveryToken = async (client: ShapioClient, spec: DeliveryRoleSpec) => {
313
327
  const summary = await client.admin.schema.summary();
314
328
  const modelIds = summary.definitions
315
329
  .filter((definition) => SITE_MODELS.includes(definition.apiKey))
316
330
  .map((definition) => definition.id);
317
- const permissions = modelIds.map((modelId) => ({
318
- action: 'read' as const,
319
- modelId,
320
- condition: null,
321
- fieldIds: null,
322
- }));
331
+ const grant = { condition: null, fieldIds: null };
332
+ const permissions = [
333
+ ...modelIds.map((modelId) => ({ action: 'read' as const, modelId, ...grant })),
334
+ // Read drafts always covers every model; the role still reads only the models above.
335
+ ...(spec.drafts ? [{ action: 'readDrafts' as const, modelId: null, ...grant }] : []),
336
+ ];
323
337
  const roles = await client.admin.roles.list();
324
- const existing = roles.find((role) => role.key === DELIVERY_ROLE_KEY);
338
+ const existing = roles.find((role) => role.key === spec.key);
325
339
  const role = existing
326
340
  ? await client.admin.roles.update(existing.id, { expectedVersion: existing.version, permissions })
327
341
  : await client.admin.roles.create({
328
- key: DELIVERY_ROLE_KEY,
329
- name: 'Starter site (delivery)',
330
- description: 'Reads published pages, articles, authors and the site settings for the starter build.',
342
+ key: spec.key,
343
+ name: spec.name,
344
+ description: spec.description,
331
345
  kind: 'delivery',
332
346
  permissions,
333
347
  });
334
348
  for (const token of await client.admin.tokens.list()) {
335
- if (token.name === DELIVERY_TOKEN_NAME && !token.revokedAt) {
349
+ if (token.name === spec.tokenName && !token.revokedAt) {
336
350
  await client.admin.tokens.revoke(token.id);
337
351
  }
338
352
  }
339
- return (await client.admin.tokens.create({ name: DELIVERY_TOKEN_NAME, roleId: role.id })).token;
353
+ return (await client.admin.tokens.create({ name: spec.tokenName, roleId: role.id })).token;
354
+ };
355
+
356
+ const BUILD_ROLE: DeliveryRoleSpec = {
357
+ key: DELIVERY_ROLE_KEY,
358
+ name: 'Starter site (delivery)',
359
+ description: 'Reads published pages, articles, authors and the site settings for the starter build.',
360
+ tokenName: DELIVERY_TOKEN_NAME,
361
+ drafts: false,
362
+ };
363
+
364
+ /** The development token's role: the build role plus Read drafts. Named after the site it reads. */
365
+ const devRole = (siteName: string): DeliveryRoleSpec => ({
366
+ key: DEV_ROLE_KEY,
367
+ name: 'Starter site dev (drafts)',
368
+ description:
369
+ 'Reads the starter models and their drafts, for development servers (drafts mode). Never use its tokens in production.',
370
+ tokenName: `${siteName} dev`,
371
+ drafts: true,
372
+ });
373
+
374
+ /** The name of the site the seed works on: SHAPIO_SITE, else the primary site. */
375
+ const siteNameOf = async (client: ShapioClient, siteKey: string | undefined) => {
376
+ const sites = await client.admin.sites.list();
377
+ const site = sites.find((candidate) => (siteKey ? candidate.key === siteKey : candidate.isPrimary));
378
+ return site?.name ?? siteKey ?? 'site';
340
379
  };
341
380
 
342
381
  /** The "Preview" connection, created once and pointed at this starter's /preview/ page on every run. */
@@ -402,24 +441,30 @@ const tryRevalidateWebhook = async (client: ShapioClient, siteUrl: string, path:
402
441
  }
403
442
  };
404
443
 
405
- const writeEnv = async (
406
- url: string,
407
- site: string | undefined,
408
- deliveryToken: string,
409
- webhookSecret: string | undefined,
410
- ) => {
444
+ type SeededEnv = {
445
+ url: string;
446
+ site: string | undefined;
447
+ deliveryToken: string;
448
+ devDeliveryToken: string;
449
+ webhookSecret: string | undefined;
450
+ };
451
+
452
+ const writeEnv = async ({ url, site, deliveryToken, devDeliveryToken, webhookSecret }: SeededEnv) => {
411
453
  const path = resolve('.env');
412
454
  const lines = [
413
- '# Written by the seed. The delivery token is read-only; keep this file out of git.',
455
+ '# Written by the seed. The delivery tokens are read-only; keep this file out of git.',
414
456
  `SHAPIO_URL=${url}`,
415
457
  `SHAPIO_DELIVERY_TOKEN=${deliveryToken}`,
458
+ '# Development only: drafts mode (SHAPIO_DRAFTS=true) reads with this token, which may read drafts.',
459
+ '# Never put it in a production environment.',
460
+ `SHAPIO_DEV_DELIVERY_TOKEN=${devDeliveryToken}`,
416
461
  // The site the content was seeded on: the starter reads the same one.
417
462
  ...(site ? [`SHAPIO_SITE=${site}`] : []),
418
463
  ...(webhookSecret ? [`SHAPIO_WEBHOOK_SECRET=${webhookSecret}`] : []),
419
464
  ];
420
465
  await writeFile(path, `${lines.join('\n')}\n`, { mode: 0o600 });
421
466
  log(
422
- `Wrote SHAPIO_URL, ${site ? 'SHAPIO_SITE, ' : ''}SHAPIO_DELIVERY_TOKEN${webhookSecret ? ' and SHAPIO_WEBHOOK_SECRET' : ''} to ${path}`,
467
+ `Wrote SHAPIO_URL, ${site ? 'SHAPIO_SITE, ' : ''}SHAPIO_DELIVERY_TOKEN, SHAPIO_DEV_DELIVERY_TOKEN${webhookSecret ? ' and SHAPIO_WEBHOOK_SECRET' : ''} to ${path}`,
423
468
  );
424
469
  };
425
470
 
@@ -444,7 +489,15 @@ const main = async () => {
444
489
  const webhookSecret = revalidatePath
445
490
  ? await tryRevalidateWebhook(admin.client, siteUrl, revalidatePath)
446
491
  : undefined;
447
- await writeEnv(admin.url, admin.site, await createDeliveryToken(admin.client), webhookSecret);
492
+ const siteName = await siteNameOf(admin.client, admin.site);
493
+ await writeEnv({
494
+ url: admin.url,
495
+ site: admin.site,
496
+ deliveryToken: await createDeliveryToken(admin.client, BUILD_ROLE),
497
+ devDeliveryToken: await createDeliveryToken(admin.client, devRole(siteName)),
498
+ webhookSecret,
499
+ });
500
+ log(`Created the "${siteName} dev" token for drafts mode (SHAPIO_DRAFTS=true); keep it off production.`);
448
501
  log('Seeded. Build the site with: npm run build');
449
502
  } finally {
450
503
  await admin.close();
@@ -0,0 +1,109 @@
1
+ import { parseArgs } from 'node:util';
2
+ import { connectAdmin } from './lib/admin.js';
3
+
4
+ /**
5
+ * `npm run smoke:drafts`, after `npm run seed` and starting the dev server with SHAPIO_DRAFTS=true (drafts mode,
6
+ * which reads with the seed's SHAPIO_DEV_DELIVERY_TOKEN): checks that the site shows drafts, that every page
7
+ * carries the "Drafts" badge, and that a change saved in Shapio (not published) shows on the next reload.
8
+ * The saved change is put back afterwards. Needs SHAPIO_ADMIN_EMAIL and SHAPIO_ADMIN_PASSWORD (or SHAPIO_TOKEN,
9
+ * an admin API token) to save the change, as the seed does.
10
+ *
11
+ * `--url` is the running dev server's origin (default: SMOKE_URL, then http://localhost:3000).
12
+ */
13
+ const DRAFT_TITLE = 'Coming soon: our winter projects';
14
+ const ARTICLE = 'modelling-without-a-deploy';
15
+ const BADGE = 'data-shapio-drafts';
16
+ /** How long a saved draft may take to show: the dev server reads fresh, so this only absorbs a slow render. */
17
+ const SHOW_WITHIN_MS = 30_000;
18
+ const POLL_EVERY_MS = 250;
19
+
20
+ const failures: string[] = [];
21
+ const check = (condition: boolean, message: string) => {
22
+ process.stdout.write(`${condition ? 'ok ' : 'FAIL'} ${message}\n`);
23
+ if (!condition) {
24
+ failures.push(message);
25
+ }
26
+ };
27
+
28
+ const fetchPage = async (origin: string, path: string) => {
29
+ const response = await fetch(new URL(path, origin));
30
+ return { status: response.status, html: await response.text() };
31
+ };
32
+
33
+ /** Reloads `path` until it contains `needle` (a real condition, not a fixed wait); false when it never does. */
34
+ const showsWithin = async (origin: string, path: string, needle: string): Promise<boolean> => {
35
+ const deadline = Date.now() + SHOW_WITHIN_MS;
36
+ while (Date.now() < deadline) {
37
+ if ((await fetchPage(origin, path)).html.includes(needle)) {
38
+ return true;
39
+ }
40
+ await new Promise((resolve) => setTimeout(resolve, POLL_EVERY_MS));
41
+ }
42
+ return false;
43
+ };
44
+
45
+ /** Saves (does not publish) the article's English title, and returns a function that puts the old one back. */
46
+ const saveTitle = async (client: Awaited<ReturnType<typeof connectAdmin>>['client'], title: string) => {
47
+ const [item] = (
48
+ await client.admin.content.list('article', { locale: 'en', filters: { slug: { $eq: ARTICLE } } })
49
+ ).items;
50
+ if (!item) {
51
+ throw new Error(`The seeded article "${ARTICLE}" is missing; run npm run seed first`);
52
+ }
53
+ const entry = await client.admin.content.get('article', item.id, { locale: 'en' });
54
+ const original = entry.data.title;
55
+ // A save sends only the fields it changes.
56
+ const saved = await client.admin.content.update('article', item.id, {
57
+ locale: 'en',
58
+ expectedVersion: entry.version,
59
+ data: { title },
60
+ });
61
+ return async () => {
62
+ await client.admin.content.update('article', item.id, {
63
+ locale: 'en',
64
+ expectedVersion: saved.version,
65
+ data: { title: original },
66
+ });
67
+ };
68
+ };
69
+
70
+ const main = async () => {
71
+ const { values } = parseArgs({ options: { url: { type: 'string' } } });
72
+ const origin = values.url ?? process.env.SMOKE_URL ?? 'http://localhost:3000';
73
+
74
+ const list = await fetchPage(origin, '/en/articles/');
75
+ check(list.status === 200, `/en/articles/: HTTP ${list.status}`);
76
+ check(list.html.includes(BADGE), '/en/articles/: the Drafts badge');
77
+ check(list.html.includes(DRAFT_TITLE), 'the unpublished draft is listed (drafts mode)');
78
+ const draft = await fetchPage(origin, '/en/articles/winter-projects/');
79
+ check(
80
+ draft.status === 200 && draft.html.includes(DRAFT_TITLE),
81
+ `the unpublished draft is served (HTTP ${draft.status})`,
82
+ );
83
+
84
+ const admin = await connectAdmin(process.env);
85
+ try {
86
+ const marker = `Saved, not published ${Date.now()}`;
87
+ const restore = await saveTitle(admin.client, marker);
88
+ try {
89
+ check(
90
+ await showsWithin(origin, `/en/articles/${ARTICLE}/`, marker),
91
+ 'a change saved in Shapio shows on reload, without publishing',
92
+ );
93
+ } finally {
94
+ await restore();
95
+ }
96
+ } finally {
97
+ await admin.close();
98
+ }
99
+
100
+ if (failures.length > 0) {
101
+ throw new Error(`${failures.length} check(s) failed against ${origin}`);
102
+ }
103
+ process.stdout.write(`Drafts smoke test passed against ${origin}.\n`);
104
+ };
105
+
106
+ main().catch((error: unknown) => {
107
+ process.stderr.write(`smoke:drafts: ${error instanceof Error ? error.message : String(error)}\n`);
108
+ process.exitCode = 1;
109
+ });
@@ -1,4 +1,6 @@
1
1
  import { createHmac } from 'node:crypto';
2
+ import { mkdir, writeFile } from 'node:fs/promises';
3
+ import { join } from 'node:path';
2
4
  import { parseArgs } from 'node:util';
3
5
 
4
6
  /**
@@ -13,6 +15,10 @@ import { parseArgs } from 'node:util';
13
15
  * `--url` is the running site's origin (default: SMOKE_URL, then http://localhost:4321). `--revalidate` (the
14
16
  * Next.js starter) also checks its on-demand revalidation route: an unsigned POST to /api/revalidate is refused,
15
17
  * and an event signed with SHAPIO_WEBHOOK_SECRET is answered 200 with the paths it revalidated.
18
+ *
19
+ * `--save <dir>` also writes each checked page's rendered markup to `<dir>` (scripts and the framework's own
20
+ * asset links removed: they name build-specific chunks), so two builds of a site can be compared with
21
+ * `diff -r`, e.g. the Next.js starter reading over HTTP and in process.
16
22
  */
17
23
  const DRAFT_TITLE = 'Coming soon: our winter projects';
18
24
  const ARTICLE = 'modelling-without-a-deploy';
@@ -85,6 +91,18 @@ const fetchPage = async (origin: string, path: string) => {
85
91
  return { status: response.status, html: await response.text() };
86
92
  };
87
93
 
94
+ /** The page as rendered: without scripts and framework asset links, which differ from build to build. */
95
+ const renderedMarkup = (html: string) =>
96
+ html
97
+ .replace(/<script\b[^>]*>[\s\S]*?<\/script>/g, '')
98
+ .replace(/<link\b[^>]*\/_next\/[^>]*>/g, '')
99
+ .replace(/<meta\b[^>]*name="next-size-adjust"[^>]*>/g, '');
100
+
101
+ const savePage = async (directory: string, path: string, html: string) => {
102
+ const name = path.replace(/^\/|\/$/g, '').replace(/\//g, '__') || 'index';
103
+ await writeFile(join(directory, `${name}.html`), `${renderedMarkup(html)}\n`);
104
+ };
105
+
88
106
  const REVALIDATE_PATH = '/api/revalidate/';
89
107
 
90
108
  const postEvent = (origin: string, body: string, headers: Record<string, string>) =>
@@ -137,12 +155,22 @@ const checkRevalidation = async (origin: string) => {
137
155
 
138
156
  const main = async () => {
139
157
  const { values } = parseArgs({
140
- options: { url: { type: 'string' }, revalidate: { type: 'boolean', default: false } },
158
+ options: {
159
+ url: { type: 'string' },
160
+ revalidate: { type: 'boolean', default: false },
161
+ save: { type: 'string' },
162
+ },
141
163
  });
142
164
  const origin = values.url ?? process.env.SMOKE_URL ?? 'http://localhost:4321';
165
+ if (values.save) {
166
+ await mkdir(values.save, { recursive: true });
167
+ }
143
168
  for (const page of PAGES) {
144
169
  const { status, html } = await fetchPage(origin, page.path);
145
170
  check(status === 200, `${page.path}: HTTP ${status}`);
171
+ if (values.save) {
172
+ await savePage(values.save, page.path, html);
173
+ }
146
174
  for (const [needle, label] of page.expect) {
147
175
  check(html.includes(needle), `${page.path}: ${label}`);
148
176
  }
@@ -10,6 +10,8 @@ const optional = (value: string | undefined) => (value === '' ? undefined : valu
10
10
  export const variables = defineEnvVars({
11
11
  SHAPIO_URL: { schema: optional },
12
12
  SHAPIO_DELIVERY_TOKEN: { schema: optional },
13
+ SHAPIO_DEV_DELIVERY_TOKEN: { schema: optional },
14
+ SHAPIO_DRAFTS: { schema: optional },
13
15
  SHAPIO_SNAPSHOT: { schema: optional },
14
16
  SHAPIO_SITE: { schema: optional },
15
17
  PUBLIC_SHAPIO_URL: { schema: optional },