create-spillway 0.0.0-stage → 0.1.11
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/LICENSE +21 -0
- package/README.md +40 -2
- package/dist/cli.d.ts +23 -0
- package/dist/cli.js +133 -0
- package/dist/cli.js.map +1 -0
- package/dist/index.d.ts +116 -0
- package/dist/index.js +291 -0
- package/dist/index.js.map +1 -0
- package/package.json +69 -3
- package/templates/astro/.env.example +27 -0
- package/templates/astro/README.md +57 -0
- package/templates/astro/_gitignore +11 -0
- package/templates/astro/astro.config.mjs +21 -0
- package/templates/astro/package.json +34 -0
- package/templates/astro/scripts/brand.mjs +40 -0
- package/templates/astro/src/env.d.ts +2 -0
- package/templates/astro/src/layouts/Layout.astro +57 -0
- package/templates/astro/src/lib/cache.ts +52 -0
- package/templates/astro/src/lib/spillway.ts +106 -0
- package/templates/astro/src/middleware.ts +18 -0
- package/templates/astro/src/pages/[...path].astro +26 -0
- package/templates/astro/src/pages/api/preview/exit.ts +4 -0
- package/templates/astro/src/pages/api/preview/index.ts +15 -0
- package/templates/astro/src/pages/api/revalidate.ts +34 -0
- package/templates/astro/src/pages/robots.txt.ts +6 -0
- package/templates/astro/src/pages/sitemap.xml.ts +19 -0
- package/templates/astro/src/styles/site.css +83 -0
- package/templates/astro/src/styles/spillway-brand.css +1 -0
- package/templates/astro/src/styles/spillway-brand.json +3 -0
- package/templates/astro/tsconfig.json +6 -0
- package/templates/next/.env.example +24 -0
- package/templates/next/README.md +48 -0
- package/templates/next/_gitignore +12 -0
- package/templates/next/app/[[...slug]]/page.tsx +56 -0
- package/templates/next/app/api/preview/exit/route.ts +6 -0
- package/templates/next/app/api/preview/route.ts +16 -0
- package/templates/next/app/api/revalidate/route.ts +41 -0
- package/templates/next/app/globals.css +83 -0
- package/templates/next/app/layout.tsx +71 -0
- package/templates/next/app/not-found.tsx +10 -0
- package/templates/next/app/robots.ts +10 -0
- package/templates/next/app/sitemap.ts +12 -0
- package/templates/next/app/spillway-brand.css +1 -0
- package/templates/next/app/spillway-brand.json +3 -0
- package/templates/next/components/Nav.tsx +30 -0
- package/templates/next/lib/spillway.ts +71 -0
- package/templates/next/next.config.ts +14 -0
- package/templates/next/package.json +34 -0
- package/templates/next/scripts/brand.mjs +40 -0
- package/templates/next/tsconfig.json +21 -0
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import type { APIRoute } from 'astro';
|
|
2
|
+
import type { PublicPath } from '@spillway-cms/client';
|
|
3
|
+
import { cached } from '../lib/cache';
|
|
4
|
+
import { SHELL_TAG, SITE_URL, client } from '../lib/spillway';
|
|
5
|
+
|
|
6
|
+
const esc = (s: string) => s.replace(/&/g, '&').replace(/</g, '<').replace(/>/g, '>');
|
|
7
|
+
|
|
8
|
+
/** /sitemap.xml from every published path in Spillway, on this front end's address. */
|
|
9
|
+
export const GET: APIRoute = async () => {
|
|
10
|
+
let paths: PublicPath[] = [];
|
|
11
|
+
try {
|
|
12
|
+
paths = await cached('paths', async () => ({ value: await client.paths(), tags: [SHELL_TAG] }));
|
|
13
|
+
} catch {
|
|
14
|
+
// Spillway is not reachable: an empty sitemap rather than an error
|
|
15
|
+
}
|
|
16
|
+
const urls = paths.map((p) => ` <url><loc>${esc(`${SITE_URL}${p.path === '/' ? '' : p.path}`)}</loc><lastmod>${esc(p.updatedAt)}</lastmod></url>`);
|
|
17
|
+
const body = `<?xml version="1.0" encoding="UTF-8"?>\n<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">\n${urls.join('\n')}\n</urlset>\n`;
|
|
18
|
+
return new Response(body, { headers: { 'content-type': 'application/xml; charset=utf-8' } });
|
|
19
|
+
};
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* The shell around Spillway's blocks. Everything reads the brand's variables (spillway-brand.css), so a brand change in
|
|
3
|
+
* Spillway reaches the header and footer too. The fallbacks apply before the first brand fetch.
|
|
4
|
+
*/
|
|
5
|
+
body {
|
|
6
|
+
margin: 0;
|
|
7
|
+
font-family: var(--sw-font-body, system-ui, sans-serif);
|
|
8
|
+
color: var(--sw-color-text, #111827);
|
|
9
|
+
background: var(--sw-color-bg, #ffffff);
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
.skip-link {
|
|
13
|
+
position: absolute;
|
|
14
|
+
left: -999px;
|
|
15
|
+
}
|
|
16
|
+
.skip-link:focus {
|
|
17
|
+
left: 1rem;
|
|
18
|
+
top: 1rem;
|
|
19
|
+
z-index: 10;
|
|
20
|
+
padding: 0.5rem 1rem;
|
|
21
|
+
background: var(--sw-color-bg, #ffffff);
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
.preview-bar {
|
|
25
|
+
display: flex;
|
|
26
|
+
justify-content: center;
|
|
27
|
+
gap: 1rem;
|
|
28
|
+
padding: 0.5rem 1rem;
|
|
29
|
+
background: var(--sw-color-warning, #fbbf24);
|
|
30
|
+
color: #111827;
|
|
31
|
+
font-size: 0.875rem;
|
|
32
|
+
}
|
|
33
|
+
.preview-bar a {
|
|
34
|
+
color: inherit;
|
|
35
|
+
font-weight: 600;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
.site-header,
|
|
39
|
+
.site-footer {
|
|
40
|
+
display: flex;
|
|
41
|
+
flex-wrap: wrap;
|
|
42
|
+
align-items: center;
|
|
43
|
+
justify-content: space-between;
|
|
44
|
+
gap: 1rem;
|
|
45
|
+
max-width: var(--sw-max-width, 1120px);
|
|
46
|
+
margin: 0 auto;
|
|
47
|
+
padding: 1rem;
|
|
48
|
+
}
|
|
49
|
+
.site-footer {
|
|
50
|
+
border-top: 1px solid var(--sw-color-neutral-200, #e5e7eb);
|
|
51
|
+
color: var(--sw-color-neutral-600, #4b5563);
|
|
52
|
+
font-size: 0.875rem;
|
|
53
|
+
}
|
|
54
|
+
.site-name {
|
|
55
|
+
font-family: var(--sw-font-heading, inherit);
|
|
56
|
+
font-weight: 700;
|
|
57
|
+
color: inherit;
|
|
58
|
+
text-decoration: none;
|
|
59
|
+
}
|
|
60
|
+
.site-nav ul {
|
|
61
|
+
display: flex;
|
|
62
|
+
flex-wrap: wrap;
|
|
63
|
+
gap: 1rem;
|
|
64
|
+
margin: 0;
|
|
65
|
+
padding: 0;
|
|
66
|
+
list-style: none;
|
|
67
|
+
}
|
|
68
|
+
.site-nav ul ul {
|
|
69
|
+
display: none;
|
|
70
|
+
}
|
|
71
|
+
.site-nav a {
|
|
72
|
+
color: inherit;
|
|
73
|
+
}
|
|
74
|
+
.site-nav a[aria-current='page'] {
|
|
75
|
+
color: var(--sw-color-primary, #1d4ed8);
|
|
76
|
+
font-weight: 600;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
.not-found {
|
|
80
|
+
max-width: var(--sw-max-width, 1120px);
|
|
81
|
+
margin: 4rem auto;
|
|
82
|
+
padding: 0 1rem;
|
|
83
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
/* The brand of your Spillway site. A placeholder until scripts/brand.mjs fetches it (npm run brand, and before every build). */
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
# Copy to .env.local (create-spillway wrote one for you) and fill in. Never commit .env.local.
|
|
2
|
+
|
|
3
|
+
# The Spillway site this front end reads, its public address.
|
|
4
|
+
SPILLWAY_URL=https://cms.example.org
|
|
5
|
+
|
|
6
|
+
# Optional. An API token (spw_..., made under the account menu → API tokens) with content.view_drafts, for draft
|
|
7
|
+
# previews through /api/preview?preview=1, and to read one site of a multisite instance with SPILLWAY_SITE. Server only.
|
|
8
|
+
SPILLWAY_TOKEN=
|
|
9
|
+
# Optional. The site's key on a multisite instance, when SPILLWAY_URL is not the site's own domain (needs SPILLWAY_TOKEN).
|
|
10
|
+
SPILLWAY_SITE=
|
|
11
|
+
|
|
12
|
+
# The secret of the webhook rule in Spillway (Site settings → Notifications) that posts to /api/revalidate.
|
|
13
|
+
SPILLWAY_WEBHOOK_SECRET=
|
|
14
|
+
|
|
15
|
+
# Guards /api/preview?preview=1 (latest drafts through SPILLWAY_TOKEN) and signs its cookie. Release share links do not
|
|
16
|
+
# need it.
|
|
17
|
+
SPILLWAY_PREVIEW_SECRET=
|
|
18
|
+
|
|
19
|
+
# Optional. A deploy hook (Vercel, Netlify, ...) the revalidation route calls on brand.published, so the next build
|
|
20
|
+
# refreshes the brand stylesheet. Pages already pick up a new brand without it.
|
|
21
|
+
DEPLOY_HOOK_URL=
|
|
22
|
+
|
|
23
|
+
# This front end's own address: canonical links, the sitemap and robots.txt use it.
|
|
24
|
+
SITE_URL=http://localhost:3001
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
# __PROJECT_NAME__
|
|
2
|
+
|
|
3
|
+
A Next.js (App Router) front end on a [Spillway CMS](https://spillwaycms.com) site, made with `create-spillway`. Editors keep working in Spillway's admin; this app renders the published pages with Spillway's own block library, and updates within seconds of a publish.
|
|
4
|
+
|
|
5
|
+
## Set up
|
|
6
|
+
|
|
7
|
+
1. Install and start it:
|
|
8
|
+
|
|
9
|
+
```bash
|
|
10
|
+
npm install
|
|
11
|
+
npm run dev # http://localhost:3001
|
|
12
|
+
```
|
|
13
|
+
|
|
14
|
+
2. Check `.env.local` (written for you; `.env.example` explains every value). `SPILLWAY_URL` is your Spillway site's address. The webhook and preview secrets were generated for you; keep the file out of version control.
|
|
15
|
+
3. In Spillway, under **Site settings → Notifications**, add a rule with the channel **Webhook** for each of the events published, unpublished, deleted, menu changed, redirects changed, site settings changed and brand published (`entity.published`, `entity.unpublished`, `entity.deleted`, `menu.updated`, `redirects.updated`, `site.updated`, `brand.published`), all pointing at `https://<this front end>/api/revalidate` with the secret `SPILLWAY_WEBHOOK_SECRET`. **Test send** on a rule should show as delivered in the **Outbox** below the rules.
|
|
16
|
+
4. For draft previews, create an API token under the account menu → **API tokens** with `content.view_drafts` and set it as `SPILLWAY_TOKEN`. Release share links work without it.
|
|
17
|
+
|
|
18
|
+
## What is in it
|
|
19
|
+
|
|
20
|
+
| File | What it does |
|
|
21
|
+
| --- | --- |
|
|
22
|
+
| `lib/spillway.ts` | The server-side client (`createSpillwayClient`): every read is tagged in Next's cache. Redirect matching and canonical links on `SITE_URL` |
|
|
23
|
+
| `app/[[...slug]]/page.tsx` | Every published path, prerendered at build time (`generateStaticParams`), new paths rendered on demand; the blocks, JSON-LD and metadata (title, description, canonical, hreflang, Open Graph) from Spillway; Spillway's redirects first; a 404 for unknown paths |
|
|
24
|
+
| `app/layout.tsx` | The shell: the site name, the `main` and `footer` menus (Structure → Menus), the block stylesheet, the brand and, in a preview, the preview bar |
|
|
25
|
+
| `app/api/revalidate/route.ts` | The webhook target: verifies `X-Spillway-Signature` and revalidates the paths and tags the event touched (`affectedByWebhook`) |
|
|
26
|
+
| `app/api/preview/route.ts`, `exit/route.ts` | Draft mode from a release share link or `?preview=1&secret=`, and back |
|
|
27
|
+
| `app/sitemap.ts`, `app/robots.ts` | `/sitemap.xml` from the published paths, `/robots.txt` |
|
|
28
|
+
| `next.config.ts` | Rewrites `/api/forms/*` and `/media/*` to Spillway, so the Form block and media work from this origin |
|
|
29
|
+
| `scripts/brand.mjs` | Writes the brand stylesheet (`app/spillway-brand.css`) with `@spillway-cms/brand`; runs before every build |
|
|
30
|
+
|
|
31
|
+
## Previews
|
|
32
|
+
|
|
33
|
+
- **A release share link**: `/api/preview?path=/about&release=<release id>&token=<share token>`. Spillway checks the token.
|
|
34
|
+
- **The latest drafts**: `/api/preview?path=/about&preview=1&secret=<SPILLWAY_PREVIEW_SECRET>`, read through `SPILLWAY_TOKEN`.
|
|
35
|
+
|
|
36
|
+
Both turn on draft mode and show the preview bar; **Exit preview** (`/api/preview/exit`) turns it off.
|
|
37
|
+
|
|
38
|
+
## The brand
|
|
39
|
+
|
|
40
|
+
`app/spillway-brand.css` holds the brand as Spillway publishes it (colors, fonts, type scale, spacing as `--sw-*` variables). `npm run brand` refreshes it, and every build does. When someone publishes a brand change, the `brand.published` webhook revalidates the pages and the layout inlines the new brand until the next build writes it; set `DEPLOY_HOOK_URL` to have that webhook start the build.
|
|
41
|
+
|
|
42
|
+
With Tailwind v4, install `tailwindcss` and `@tailwindcss/postcss`, write `brandStylesheet(brand, { tailwind: 'v4' })` in `scripts/brand.mjs` and import `spillway-brand.css` after `@import "tailwindcss";`: `bg-primary`, `text-primary-700`, `font-heading` and `rounded-brand` then follow the brand.
|
|
43
|
+
|
|
44
|
+
## Deploy
|
|
45
|
+
|
|
46
|
+
Any host that runs Next.js with a server works (Vercel, Netlify, a container with `npm run build && npm start`). Set the variables from `.env.example` there, with `SITE_URL` the public address, then point the webhook rules at it. Spillway serves the media and receives the form posts; this app proxies both.
|
|
47
|
+
|
|
48
|
+
See [the headless docs](https://github.com/kwall-llc/spillway-cms/blob/main/docs/adapters.md) for the API and the adapters.
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import type { Metadata } from 'next';
|
|
2
|
+
import { notFound, permanentRedirect, redirect } from 'next/navigation';
|
|
3
|
+
import { cache } from 'react';
|
|
4
|
+
import { SpillwayError, pathFromParams, spillwayStaticParams } from '@spillway-cms/next';
|
|
5
|
+
import { SpillwayBlocks, SpillwayJsonLd, fetchPage, metadataFor } from '@spillway-cms/next/page';
|
|
6
|
+
import type { PublicRender } from '@spillway-cms/client';
|
|
7
|
+
import { redirectFor, spillway, withSiteUrls } from '../../lib/spillway';
|
|
8
|
+
|
|
9
|
+
type Props = { params: Promise<{ slug?: string[] }> };
|
|
10
|
+
|
|
11
|
+
// Every published path is prerendered at build time; a path published later renders on its first request and is then
|
|
12
|
+
// cached like the rest. When Spillway cannot be reached at build time, every page renders on demand instead.
|
|
13
|
+
export const dynamicParams = true;
|
|
14
|
+
export async function generateStaticParams() {
|
|
15
|
+
return spillwayStaticParams(spillway, {
|
|
16
|
+
onError: (err) => {
|
|
17
|
+
console.warn(`[spillway] could not list the published paths (${err instanceof Error ? err.message : String(err)}); pages render on demand`);
|
|
18
|
+
return [];
|
|
19
|
+
},
|
|
20
|
+
});
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/** The page at a path, or null when Spillway has none. Draft mode (a preview) is honored. */
|
|
24
|
+
const loadPage = cache(async (path: string): Promise<PublicRender | null> => {
|
|
25
|
+
try {
|
|
26
|
+
return await fetchPage(spillway, path);
|
|
27
|
+
} catch (err) {
|
|
28
|
+
if (err instanceof SpillwayError && err.status === 404) return null;
|
|
29
|
+
throw err;
|
|
30
|
+
}
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
export async function generateMetadata({ params }: Props): Promise<Metadata> {
|
|
34
|
+
const page = await loadPage(pathFromParams(await params)).catch(() => null);
|
|
35
|
+
return page ? (metadataFor(withSiteUrls(page)) as Metadata) : {};
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
export default async function Page({ params }: Props) {
|
|
39
|
+
const path = pathFromParams(await params);
|
|
40
|
+
|
|
41
|
+
// Redirects in Spillway win over pages, as on a Spillway-rendered site. 301 becomes 308 and 302 becomes 307.
|
|
42
|
+
const moved = await redirectFor(path);
|
|
43
|
+
if (moved) {
|
|
44
|
+
if (moved.status === 301) permanentRedirect(moved.to);
|
|
45
|
+
redirect(moved.to);
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
const page = await loadPage(path);
|
|
49
|
+
if (!page) notFound();
|
|
50
|
+
return (
|
|
51
|
+
<>
|
|
52
|
+
<SpillwayJsonLd page={page} />
|
|
53
|
+
<SpillwayBlocks page={page} />
|
|
54
|
+
</>
|
|
55
|
+
);
|
|
56
|
+
}
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import { createPreviewExitHandler } from '@spillway-cms/next/server';
|
|
2
|
+
|
|
3
|
+
export const dynamic = 'force-dynamic';
|
|
4
|
+
|
|
5
|
+
/** GET /api/preview/exit: turns draft mode off, forgets the preview credential and goes back to the live home page. */
|
|
6
|
+
export const GET = createPreviewExitHandler('/');
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* GET /api/preview: turns on draft mode and shows a page as it will be.
|
|
3
|
+
*
|
|
4
|
+
* - A release share link: /api/preview?path=/about&release=<release id>&token=<share token>. Spillway checks the token;
|
|
5
|
+
* the editor shares the link from the release in the admin.
|
|
6
|
+
* - The latest drafts through this front end's token: /api/preview?path=/about&preview=1&secret=<SPILLWAY_PREVIEW_SECRET>.
|
|
7
|
+
* Needs SPILLWAY_TOKEN with content.view_drafts.
|
|
8
|
+
*
|
|
9
|
+
* /api/preview/exit turns it off again.
|
|
10
|
+
*/
|
|
11
|
+
import { createPreviewHandler } from '@spillway-cms/next/server';
|
|
12
|
+
import { spillway } from '../../../lib/spillway';
|
|
13
|
+
|
|
14
|
+
export const dynamic = 'force-dynamic';
|
|
15
|
+
|
|
16
|
+
export const GET = createPreviewHandler({ client: spillway }); // the secret is the client's previewSecret
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* POST /api/revalidate: the target of the webhook rules in Spillway (Site settings → Notifications). Point one rule per
|
|
3
|
+
* event at it (entity.published, entity.unpublished, entity.deleted, menu.updated, redirects.updated, site.updated,
|
|
4
|
+
* brand.published) with the secret SPILLWAY_WEBHOOK_SECRET.
|
|
5
|
+
*
|
|
6
|
+
* The handler checks the X-Spillway-Signature HMAC over the raw body (401 when it does not match), works out what the
|
|
7
|
+
* event touched with affectedByWebhook (the page's path and Spillway's cache tags) and revalidates those paths and tags,
|
|
8
|
+
* plus the global `spillway` tag so lists and menus that show the change refresh too. A test send is acknowledged and
|
|
9
|
+
* changes nothing.
|
|
10
|
+
*/
|
|
11
|
+
import { createRevalidateHandler, type WebhookPayload } from '@spillway-cms/next/server';
|
|
12
|
+
|
|
13
|
+
export const dynamic = 'force-dynamic';
|
|
14
|
+
|
|
15
|
+
export async function POST(request: Request): Promise<Response> {
|
|
16
|
+
const secret = process.env.SPILLWAY_WEBHOOK_SECRET;
|
|
17
|
+
if (!secret) return Response.json({ error: 'SPILLWAY_WEBHOOK_SECRET is not set' }, { status: 503 });
|
|
18
|
+
const handler = createRevalidateHandler({
|
|
19
|
+
secret,
|
|
20
|
+
onEvent: async (payload, affected) => {
|
|
21
|
+
console.log(`[spillway] ${payload.event}: paths ${affected.paths.join(', ') || '-'}; tags ${affected.tags.join(', ') || '-'}`);
|
|
22
|
+
if (payload.event === 'brand.published') await rebuildForBrand(payload);
|
|
23
|
+
},
|
|
24
|
+
});
|
|
25
|
+
return handler(request);
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* Pages show a newly published brand at once (app/layout.tsx inlines it); a deploy hook makes the next build write it
|
|
30
|
+
* into spillway-brand.css as well. Optional: set DEPLOY_HOOK_URL to your host's deploy hook.
|
|
31
|
+
*/
|
|
32
|
+
async function rebuildForBrand(payload: WebhookPayload): Promise<void> {
|
|
33
|
+
const hook = process.env.DEPLOY_HOOK_URL;
|
|
34
|
+
if (!hook) return;
|
|
35
|
+
try {
|
|
36
|
+
const res = await fetch(hook, { method: 'POST', headers: { 'content-type': 'application/json' }, body: JSON.stringify({ source: 'spillway', event: payload.event, at: payload.at }) });
|
|
37
|
+
if (!res.ok) console.warn(`[spillway] the deploy hook answered ${res.status}`);
|
|
38
|
+
} catch (err) {
|
|
39
|
+
console.warn(`[spillway] the deploy hook failed: ${err instanceof Error ? err.message : String(err)}`);
|
|
40
|
+
}
|
|
41
|
+
}
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* The shell around Spillway's blocks. Everything reads the brand's variables (spillway-brand.css), so a brand change in
|
|
3
|
+
* Spillway reaches the header and footer too. The fallbacks apply before the first brand fetch.
|
|
4
|
+
*/
|
|
5
|
+
body {
|
|
6
|
+
margin: 0;
|
|
7
|
+
font-family: var(--sw-font-body, system-ui, sans-serif);
|
|
8
|
+
color: var(--sw-color-text, #111827);
|
|
9
|
+
background: var(--sw-color-bg, #ffffff);
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
.skip-link {
|
|
13
|
+
position: absolute;
|
|
14
|
+
left: -999px;
|
|
15
|
+
}
|
|
16
|
+
.skip-link:focus {
|
|
17
|
+
left: 1rem;
|
|
18
|
+
top: 1rem;
|
|
19
|
+
z-index: 10;
|
|
20
|
+
padding: 0.5rem 1rem;
|
|
21
|
+
background: var(--sw-color-bg, #ffffff);
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
.preview-bar {
|
|
25
|
+
display: flex;
|
|
26
|
+
justify-content: center;
|
|
27
|
+
gap: 1rem;
|
|
28
|
+
padding: 0.5rem 1rem;
|
|
29
|
+
background: var(--sw-color-warning, #fbbf24);
|
|
30
|
+
color: #111827;
|
|
31
|
+
font-size: 0.875rem;
|
|
32
|
+
}
|
|
33
|
+
.preview-bar a {
|
|
34
|
+
color: inherit;
|
|
35
|
+
font-weight: 600;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
.site-header,
|
|
39
|
+
.site-footer {
|
|
40
|
+
display: flex;
|
|
41
|
+
flex-wrap: wrap;
|
|
42
|
+
align-items: center;
|
|
43
|
+
justify-content: space-between;
|
|
44
|
+
gap: 1rem;
|
|
45
|
+
max-width: var(--sw-max-width, 1120px);
|
|
46
|
+
margin: 0 auto;
|
|
47
|
+
padding: 1rem;
|
|
48
|
+
}
|
|
49
|
+
.site-footer {
|
|
50
|
+
border-top: 1px solid var(--sw-color-neutral-200, #e5e7eb);
|
|
51
|
+
color: var(--sw-color-neutral-600, #4b5563);
|
|
52
|
+
font-size: 0.875rem;
|
|
53
|
+
}
|
|
54
|
+
.site-name {
|
|
55
|
+
font-family: var(--sw-font-heading, inherit);
|
|
56
|
+
font-weight: 700;
|
|
57
|
+
color: inherit;
|
|
58
|
+
text-decoration: none;
|
|
59
|
+
}
|
|
60
|
+
.site-nav ul {
|
|
61
|
+
display: flex;
|
|
62
|
+
flex-wrap: wrap;
|
|
63
|
+
gap: 1rem;
|
|
64
|
+
margin: 0;
|
|
65
|
+
padding: 0;
|
|
66
|
+
list-style: none;
|
|
67
|
+
}
|
|
68
|
+
.site-nav ul ul {
|
|
69
|
+
display: none;
|
|
70
|
+
}
|
|
71
|
+
.site-nav a {
|
|
72
|
+
color: inherit;
|
|
73
|
+
}
|
|
74
|
+
.site-nav a[aria-current='page'] {
|
|
75
|
+
color: var(--sw-color-primary, #1d4ed8);
|
|
76
|
+
font-weight: 600;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
.not-found {
|
|
80
|
+
max-width: var(--sw-max-width, 1120px);
|
|
81
|
+
margin: 4rem auto;
|
|
82
|
+
padding: 0 1rem;
|
|
83
|
+
}
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
import type { Metadata } from 'next';
|
|
2
|
+
import type { ReactNode } from 'react';
|
|
3
|
+
import { SpillwayStyles } from '@spillway-cms/next/page';
|
|
4
|
+
import { currentPreview } from '@spillway-cms/next/server';
|
|
5
|
+
import { Nav } from '../components/Nav';
|
|
6
|
+
import { SITE_URL, spillway } from '../lib/spillway';
|
|
7
|
+
import builtBrand from './spillway-brand.json';
|
|
8
|
+
// The brand as it was at build time (scripts/brand.mjs, run before every build): colors, fonts, type scale and spacing as
|
|
9
|
+
// the --sw-* variables the blocks use. Add Tailwind on top of it as README.md describes.
|
|
10
|
+
import './spillway-brand.css';
|
|
11
|
+
import './globals.css';
|
|
12
|
+
|
|
13
|
+
export async function generateMetadata(): Promise<Metadata> {
|
|
14
|
+
const site = await spillway.site().catch(() => null);
|
|
15
|
+
return {
|
|
16
|
+
metadataBase: new URL(SITE_URL),
|
|
17
|
+
title: site?.name ?? 'Spillway site',
|
|
18
|
+
...(site?.seo.defaultDescription ? { description: site.seo.defaultDescription } : {}),
|
|
19
|
+
};
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/** The site name, the brand and the menus, read through the preview when there is one. */
|
|
23
|
+
async function loadShell() {
|
|
24
|
+
const preview = await currentPreview({ secret: spillway.options.previewSecret });
|
|
25
|
+
const client = preview ? spillway.withPreview(preview) : spillway;
|
|
26
|
+
const [site, brand, main, footer] = await Promise.all([
|
|
27
|
+
client.site().catch(() => null),
|
|
28
|
+
client.brand().catch(() => null),
|
|
29
|
+
client.menu('main').catch(() => null),
|
|
30
|
+
client.menu('footer').catch(() => null),
|
|
31
|
+
]);
|
|
32
|
+
return { preview, site, brand, main, footer };
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
export default async function RootLayout({ children }: { children: ReactNode }) {
|
|
36
|
+
const { preview, site, brand, main, footer } = await loadShell();
|
|
37
|
+
// A brand published since the last build arrives with the brand.published webhook; inline it until the next build
|
|
38
|
+
// writes it into spillway-brand.css.
|
|
39
|
+
const newerBrand = brand && brand.version !== builtBrand.version ? brand.css : null;
|
|
40
|
+
return (
|
|
41
|
+
<html lang={site?.defaultLocale ?? 'en'}>
|
|
42
|
+
<head>
|
|
43
|
+
{/* The block stylesheet, so Spillway's blocks look as they do on the Spillway site. */}
|
|
44
|
+
<SpillwayStyles />
|
|
45
|
+
{newerBrand ? <style id="spillway-brand" dangerouslySetInnerHTML={{ __html: newerBrand }} /> : null}
|
|
46
|
+
</head>
|
|
47
|
+
<body>
|
|
48
|
+
<a className="skip-link" href="#main">
|
|
49
|
+
Skip to content
|
|
50
|
+
</a>
|
|
51
|
+
{preview ? (
|
|
52
|
+
<div className="preview-bar" role="status">
|
|
53
|
+
<span>Preview: you are seeing unpublished content.</span>
|
|
54
|
+
<a href="/api/preview/exit">Exit preview</a>
|
|
55
|
+
</div>
|
|
56
|
+
) : null}
|
|
57
|
+
<header className="site-header">
|
|
58
|
+
<a className="site-name" href="/">
|
|
59
|
+
{site?.name ?? 'Spillway site'}
|
|
60
|
+
</a>
|
|
61
|
+
<Nav items={main?.items ?? []} label="Main" />
|
|
62
|
+
</header>
|
|
63
|
+
<main id="main">{children}</main>
|
|
64
|
+
<footer className="site-footer">
|
|
65
|
+
<Nav items={footer?.items ?? []} label="Footer" />
|
|
66
|
+
{site?.footerNote?.split('\n').map((line, i) => <p key={i}>{line}</p>)}
|
|
67
|
+
</footer>
|
|
68
|
+
</body>
|
|
69
|
+
</html>
|
|
70
|
+
);
|
|
71
|
+
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import type { MetadataRoute } from 'next';
|
|
2
|
+
import { SITE_URL } from '../lib/spillway';
|
|
3
|
+
|
|
4
|
+
/** /robots.txt: everything may be crawled except the API routes; the sitemap is on this front end's address. */
|
|
5
|
+
export default function robots(): MetadataRoute.Robots {
|
|
6
|
+
return {
|
|
7
|
+
rules: { userAgent: '*', allow: '/', disallow: '/api/' },
|
|
8
|
+
sitemap: `${SITE_URL}/sitemap.xml`,
|
|
9
|
+
};
|
|
10
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import type { MetadataRoute } from 'next';
|
|
2
|
+
import { SITE_URL, spillway } from '../lib/spillway';
|
|
3
|
+
|
|
4
|
+
/** /sitemap.xml from every published path in Spillway, on this front end's address. Revalidated with the pages. */
|
|
5
|
+
export default async function sitemap(): Promise<MetadataRoute.Sitemap> {
|
|
6
|
+
try {
|
|
7
|
+
const paths = await spillway.paths();
|
|
8
|
+
return paths.map((p) => ({ url: `${SITE_URL}${p.path === '/' ? '' : p.path}`, lastModified: p.updatedAt }));
|
|
9
|
+
} catch {
|
|
10
|
+
return [];
|
|
11
|
+
}
|
|
12
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
/* The brand of your Spillway site. A placeholder until scripts/brand.mjs fetches it (npm run brand, and before every build). */
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
'use client';
|
|
2
|
+
|
|
3
|
+
import { usePathname } from 'next/navigation';
|
|
4
|
+
import type { PublicMenuItem } from '@spillway-cms/client';
|
|
5
|
+
|
|
6
|
+
/** A Spillway menu (Structure → Menus) as a nested list; the item for the current page is marked. */
|
|
7
|
+
export function Nav({ items, label }: { items: PublicMenuItem[]; label: string }) {
|
|
8
|
+
const pathname = usePathname();
|
|
9
|
+
if (!items.length) return null;
|
|
10
|
+
const list = (entries: PublicMenuItem[]) => (
|
|
11
|
+
<ul>
|
|
12
|
+
{entries.map((item) => {
|
|
13
|
+
const current = item.href === pathname;
|
|
14
|
+
return (
|
|
15
|
+
<li key={`${item.href}:${item.label}`}>
|
|
16
|
+
<a href={item.href} aria-current={current ? 'page' : undefined} {...(item.newTab ? { target: '_blank', rel: 'noopener' } : {})}>
|
|
17
|
+
{item.label}
|
|
18
|
+
</a>
|
|
19
|
+
{item.children?.length ? list(item.children) : null}
|
|
20
|
+
</li>
|
|
21
|
+
);
|
|
22
|
+
})}
|
|
23
|
+
</ul>
|
|
24
|
+
);
|
|
25
|
+
return (
|
|
26
|
+
<nav className="site-nav" aria-label={label}>
|
|
27
|
+
{list(items)}
|
|
28
|
+
</nav>
|
|
29
|
+
);
|
|
30
|
+
}
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
import { createSpillwayClient } from '@spillway-cms/next';
|
|
2
|
+
import type { PublicRedirect, PublicRender } from '@spillway-cms/client';
|
|
3
|
+
|
|
4
|
+
/** The Spillway site (SPILLWAY_URL in .env.local). */
|
|
5
|
+
export const SPILLWAY_URL = (process.env.SPILLWAY_URL || 'http://localhost:3000').replace(/\/+$/, '');
|
|
6
|
+
|
|
7
|
+
/** This front end's own address, for canonical links, the sitemap and robots.txt. */
|
|
8
|
+
export const SITE_URL = (process.env.SITE_URL || 'http://localhost:3001').replace(/\/+$/, '');
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* One server-side client for the whole app. Every read goes through Next's data cache under tags (`spillway`,
|
|
12
|
+
* `spillway:path:/about`, `spillway:menu:main`, ...) that app/api/revalidate clears when Spillway says something changed.
|
|
13
|
+
* The token stays on the server: never import this file from a client component.
|
|
14
|
+
*/
|
|
15
|
+
export const spillway = createSpillwayClient({
|
|
16
|
+
baseUrl: SPILLWAY_URL,
|
|
17
|
+
token: process.env.SPILLWAY_TOKEN || undefined,
|
|
18
|
+
site: process.env.SPILLWAY_SITE || undefined,
|
|
19
|
+
// Checks /api/preview?preview=1 and signs the draft preview cookie, which the pages verify with it. Server only.
|
|
20
|
+
previewSecret: process.env.SPILLWAY_PREVIEW_SECRET || undefined,
|
|
21
|
+
});
|
|
22
|
+
|
|
23
|
+
/** Redirect matching as Spillway does it: case-insensitive, without a trailing slash or a query. */
|
|
24
|
+
export function normalizePath(raw: string): string {
|
|
25
|
+
let p = raw;
|
|
26
|
+
try {
|
|
27
|
+
p = decodeURIComponent(p);
|
|
28
|
+
} catch {
|
|
29
|
+
// keep it as it is; a malformed escape never matches a stored path
|
|
30
|
+
}
|
|
31
|
+
const q = p.indexOf('?');
|
|
32
|
+
if (q >= 0) p = p.slice(0, q);
|
|
33
|
+
p = p.replace(/\/{2,}/g, '/').toLowerCase();
|
|
34
|
+
if (!p.startsWith('/')) p = `/${p}`;
|
|
35
|
+
if (p.length > 1) p = p.replace(/\/+$/, '');
|
|
36
|
+
return p;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/** The redirect from `path` in Spillway's list (Marketing → Redirects), if there is one. */
|
|
40
|
+
export function matchRedirect(list: PublicRedirect[], path: string): PublicRedirect | undefined {
|
|
41
|
+
const want = normalizePath(path);
|
|
42
|
+
return list.find((r) => normalizePath(r.from) === want);
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/** Spillway's redirects, cached and revalidated like everything else. An older Spillway without the list gives none. */
|
|
46
|
+
export async function redirectFor(path: string): Promise<PublicRedirect | undefined> {
|
|
47
|
+
try {
|
|
48
|
+
return matchRedirect(await spillway.redirects(), path);
|
|
49
|
+
} catch {
|
|
50
|
+
return undefined;
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/** A URL on the Spillway site rewritten onto this front end (canonical links and alternates name the site's pages). */
|
|
55
|
+
export function onSite(href: string | undefined, origin: string): string | undefined {
|
|
56
|
+
if (!href || !origin) return href;
|
|
57
|
+
return href === origin || href.startsWith(`${origin}/`) ? SITE_URL + href.slice(origin.length) : href;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/** The page with its canonical link and hreflang alternates pointing at this front end instead of Spillway. */
|
|
61
|
+
export function withSiteUrls(page: PublicRender): PublicRender {
|
|
62
|
+
const origin = page.site.origin;
|
|
63
|
+
return {
|
|
64
|
+
...page,
|
|
65
|
+
seo: {
|
|
66
|
+
...page.seo,
|
|
67
|
+
...(page.seo.canonical ? { canonical: onSite(page.seo.canonical, origin) } : {}),
|
|
68
|
+
alternates: page.seo.alternates.map((a) => ({ ...a, href: onSite(a.href, origin) ?? a.href })),
|
|
69
|
+
},
|
|
70
|
+
};
|
|
71
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import type { NextConfig } from 'next';
|
|
2
|
+
import { spillwayRewrites } from '@spillway-cms/next';
|
|
3
|
+
|
|
4
|
+
const SPILLWAY_URL = process.env.SPILLWAY_URL || 'http://localhost:3000';
|
|
5
|
+
|
|
6
|
+
const config: NextConfig = {
|
|
7
|
+
// Form posts (/api/forms/*) and media (/media/*) go to Spillway through this origin, so the blocks' relative URLs work.
|
|
8
|
+
// Do not list @puckeditor/core or @spillway-cms/blocks in serverExternalPackages: Next must bundle them with its React.
|
|
9
|
+
async rewrites() {
|
|
10
|
+
return spillwayRewrites(SPILLWAY_URL);
|
|
11
|
+
},
|
|
12
|
+
};
|
|
13
|
+
|
|
14
|
+
export default config;
|