create-spillway 0.1.22 → 0.1.24
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/README.md +11 -7
- package/dist/cli.d.ts +5 -1
- package/dist/cli.js +47 -4
- package/dist/cli.js.map +1 -1
- package/dist/index.d.ts +44 -5
- package/dist/index.js +109 -11
- package/dist/index.js.map +1 -1
- package/package.json +11 -7
- package/templates/astro/README.md +16 -3
- package/templates/astro/public/spillway-suggest.js +133 -0
- package/templates/astro/src/layouts/Layout.astro +21 -6
- package/templates/astro/src/lib/spillway.ts +88 -15
- package/templates/astro/src/pages/[...path].astro +58 -6
- package/templates/astro/src/pages/api/search/suggest.ts +16 -0
- package/templates/astro/src/styles/site.css +121 -0
- package/templates/next/README.md +19 -3
- package/templates/next/app/[[...slug]]/page.tsx +7 -4
- package/templates/next/app/api/search/suggest/route.ts +18 -0
- package/templates/next/app/globals.css +121 -0
- package/templates/next/app/layout.tsx +11 -21
- package/templates/next/app/not-found.tsx +11 -6
- package/templates/next/app/search/page.tsx +29 -0
- package/templates/next/components/LanguageSwitcher.tsx +19 -0
- package/templates/next/components/SearchPage.tsx +67 -0
- package/templates/next/components/SiteShell.tsx +38 -0
- package/templates/next/lib/spillway.ts +49 -1
- package/templates/next/next.config.ts +2 -1
- package/templates/next/public/spillway-suggest.js +133 -0
- package/templates/nuxt/.env.example +27 -0
- package/templates/nuxt/README.md +75 -0
- package/templates/nuxt/_gitignore +15 -0
- package/templates/nuxt/app/app.vue +3 -0
- package/templates/nuxt/app/assets/site.css +217 -0
- package/templates/nuxt/app/assets/spillway-brand.css +1 -0
- package/templates/nuxt/app/assets/spillway-brand.json +3 -0
- package/templates/nuxt/app/components/SearchPage.vue +36 -0
- package/templates/nuxt/app/components/SiteShell.vue +30 -0
- package/templates/nuxt/app/components/SpillwayMenu.vue +15 -0
- package/templates/nuxt/app/pages/[...slug].vue +54 -0
- package/templates/nuxt/nuxt.config.ts +18 -0
- package/templates/nuxt/package.json +32 -0
- package/templates/nuxt/public/spillway-suggest.js +133 -0
- package/templates/nuxt/scripts/brand.mjs +40 -0
- package/templates/nuxt/server/api/_spillway/view.get.ts +14 -0
- package/templates/nuxt/server/api/preview/exit.ts +13 -0
- package/templates/nuxt/server/api/preview/index.get.ts +31 -0
- package/templates/nuxt/server/api/revalidate.post.ts +45 -0
- package/templates/nuxt/server/api/search/suggest.get.ts +17 -0
- package/templates/nuxt/server/middleware/redirects.ts +17 -0
- package/templates/nuxt/server/routes/robots.txt.ts +7 -0
- package/templates/nuxt/server/routes/sitemap.xml.ts +18 -0
- package/templates/nuxt/server/utils/cache.ts +52 -0
- package/templates/nuxt/server/utils/preview.ts +6 -0
- package/templates/nuxt/server/utils/spillway.ts +211 -0
- package/templates/nuxt/shared/spillway.ts +60 -0
- package/templates/nuxt/tsconfig.json +9 -0
- package/templates/sveltekit/.env.example +27 -0
- package/templates/sveltekit/README.md +75 -0
- package/templates/sveltekit/_gitignore +11 -0
- package/templates/sveltekit/package.json +34 -0
- package/templates/sveltekit/scripts/brand.mjs +40 -0
- package/templates/sveltekit/src/app.d.ts +14 -0
- package/templates/sveltekit/src/app.html +11 -0
- package/templates/sveltekit/src/env.ts +20 -0
- package/templates/sveltekit/src/hooks.server.ts +26 -0
- package/templates/sveltekit/src/lib/components/SearchPage.svelte +41 -0
- package/templates/sveltekit/src/lib/components/SiteShell.svelte +36 -0
- package/templates/sveltekit/src/lib/components/SpillwayMenu.svelte +18 -0
- package/templates/sveltekit/src/lib/server/cache.ts +54 -0
- package/templates/sveltekit/src/lib/server/preview.ts +6 -0
- package/templates/sveltekit/src/lib/server/spillway.ts +203 -0
- package/templates/sveltekit/src/lib/site.css +204 -0
- package/templates/sveltekit/src/lib/spillway-brand.css +1 -0
- package/templates/sveltekit/src/lib/spillway-brand.json +3 -0
- package/templates/sveltekit/src/lib/view.ts +60 -0
- package/templates/sveltekit/src/routes/+layout.server.ts +4 -0
- package/templates/sveltekit/src/routes/+layout.svelte +8 -0
- package/templates/sveltekit/src/routes/[...path]/+page.server.ts +15 -0
- package/templates/sveltekit/src/routes/[...path]/+page.svelte +54 -0
- package/templates/sveltekit/src/routes/api/preview/+server.ts +30 -0
- package/templates/sveltekit/src/routes/api/preview/exit/+server.ts +10 -0
- package/templates/sveltekit/src/routes/api/revalidate/+server.ts +45 -0
- package/templates/sveltekit/src/routes/api/search/suggest/+server.ts +16 -0
- package/templates/sveltekit/src/routes/robots.txt/+server.ts +6 -0
- package/templates/sveltekit/src/routes/sitemap.xml/+server.ts +19 -0
- package/templates/sveltekit/static/spillway-suggest.js +133 -0
- package/templates/sveltekit/tsconfig.json +8 -0
- package/templates/sveltekit/vite.config.ts +17 -0
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* GET /api/search/suggest?q=&locale=: up to six published pages for what a visitor has typed, for the suggestions under
|
|
3
|
+
* search fields (public/spillway-suggest.js). Read as a visitor, so drafts never show.
|
|
4
|
+
*/
|
|
5
|
+
import { visitor } from '../../../../lib/spillway';
|
|
6
|
+
|
|
7
|
+
export const dynamic = 'force-dynamic';
|
|
8
|
+
|
|
9
|
+
export async function GET(request: Request): Promise<Response> {
|
|
10
|
+
const url = new URL(request.url);
|
|
11
|
+
const q = (url.searchParams.get('q') ?? '').slice(0, 100);
|
|
12
|
+
const locale = url.searchParams.get('locale');
|
|
13
|
+
try {
|
|
14
|
+
return Response.json(await visitor.suggest(q, locale ? { locale } : {}), { headers: { 'cache-control': 'public, max-age=60' } });
|
|
15
|
+
} catch {
|
|
16
|
+
return Response.json({ q, items: [] });
|
|
17
|
+
}
|
|
18
|
+
}
|
|
@@ -81,3 +81,124 @@ body {
|
|
|
81
81
|
margin: 4rem auto;
|
|
82
82
|
padding: 0 1rem;
|
|
83
83
|
}
|
|
84
|
+
|
|
85
|
+
.language-switcher ul {
|
|
86
|
+
display: flex;
|
|
87
|
+
flex-wrap: wrap;
|
|
88
|
+
gap: 0.75rem;
|
|
89
|
+
margin: 0;
|
|
90
|
+
padding: 0;
|
|
91
|
+
list-style: none;
|
|
92
|
+
font-size: 0.875rem;
|
|
93
|
+
}
|
|
94
|
+
.language-switcher a {
|
|
95
|
+
color: inherit;
|
|
96
|
+
}
|
|
97
|
+
.language-switcher a[aria-current='true'] {
|
|
98
|
+
font-weight: 600;
|
|
99
|
+
text-decoration: none;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
.search-page {
|
|
103
|
+
max-width: var(--sw-max-width, 1120px);
|
|
104
|
+
margin: 2rem auto 4rem;
|
|
105
|
+
padding: 0 1rem;
|
|
106
|
+
}
|
|
107
|
+
.search-form {
|
|
108
|
+
display: flex;
|
|
109
|
+
gap: 0.5rem;
|
|
110
|
+
max-width: 36rem;
|
|
111
|
+
}
|
|
112
|
+
.search-form input {
|
|
113
|
+
flex: 1 1 auto;
|
|
114
|
+
min-width: 0;
|
|
115
|
+
min-height: 2.75rem;
|
|
116
|
+
padding: 0.6rem 0.85rem;
|
|
117
|
+
font: inherit;
|
|
118
|
+
color: inherit;
|
|
119
|
+
background: var(--sw-color-bg, #ffffff);
|
|
120
|
+
border: 1px solid color-mix(in srgb, currentColor 28%, transparent);
|
|
121
|
+
border-radius: var(--sw-radius, 8px);
|
|
122
|
+
}
|
|
123
|
+
.search-form button {
|
|
124
|
+
min-height: 2.75rem;
|
|
125
|
+
padding: 0.6rem 1.1rem;
|
|
126
|
+
font: inherit;
|
|
127
|
+
font-weight: 600;
|
|
128
|
+
color: var(--sw-color-bg, #ffffff);
|
|
129
|
+
background: var(--sw-color-primary, #1d4ed8);
|
|
130
|
+
border: 0;
|
|
131
|
+
border-radius: var(--sw-radius, 8px);
|
|
132
|
+
cursor: pointer;
|
|
133
|
+
}
|
|
134
|
+
.search-results {
|
|
135
|
+
margin: 1.5rem 0 0;
|
|
136
|
+
padding: 0;
|
|
137
|
+
list-style: none;
|
|
138
|
+
}
|
|
139
|
+
.search-results li {
|
|
140
|
+
padding: 1rem 0;
|
|
141
|
+
border-bottom: 1px solid var(--sw-color-neutral-200, #e5e7eb);
|
|
142
|
+
}
|
|
143
|
+
.search-results h2 {
|
|
144
|
+
margin: 0 0 0.25rem;
|
|
145
|
+
font-size: 1.125rem;
|
|
146
|
+
}
|
|
147
|
+
.search-results p {
|
|
148
|
+
margin: 0;
|
|
149
|
+
color: var(--sw-color-neutral-600, #4b5563);
|
|
150
|
+
}
|
|
151
|
+
.search-pager {
|
|
152
|
+
display: flex;
|
|
153
|
+
gap: 1rem;
|
|
154
|
+
margin-top: 1.5rem;
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
/* Suggestions under a search field (public/spillway-suggest.js). */
|
|
158
|
+
.sw-suggest-host {
|
|
159
|
+
position: relative;
|
|
160
|
+
}
|
|
161
|
+
.sw-suggest {
|
|
162
|
+
position: absolute;
|
|
163
|
+
top: calc(100% + 0.25rem);
|
|
164
|
+
left: 0;
|
|
165
|
+
right: 0;
|
|
166
|
+
z-index: 40;
|
|
167
|
+
margin: 0;
|
|
168
|
+
padding: 0.3rem;
|
|
169
|
+
list-style: none;
|
|
170
|
+
max-height: 22rem;
|
|
171
|
+
overflow: auto;
|
|
172
|
+
background: var(--sw-color-bg, #ffffff);
|
|
173
|
+
color: var(--sw-color-text, inherit);
|
|
174
|
+
border: 1px solid color-mix(in srgb, currentColor 18%, transparent);
|
|
175
|
+
border-radius: var(--sw-radius, 8px);
|
|
176
|
+
box-shadow: 0 8px 24px rgb(0 0 0 / 12%);
|
|
177
|
+
}
|
|
178
|
+
.sw-suggest__item a {
|
|
179
|
+
display: flex;
|
|
180
|
+
justify-content: space-between;
|
|
181
|
+
align-items: baseline;
|
|
182
|
+
gap: 1rem;
|
|
183
|
+
padding: 0.5rem 0.65rem;
|
|
184
|
+
color: inherit;
|
|
185
|
+
text-decoration: none;
|
|
186
|
+
}
|
|
187
|
+
.sw-suggest__item[aria-selected='true'] a {
|
|
188
|
+
background: color-mix(in srgb, var(--sw-color-primary, #1d4ed8) 12%, transparent);
|
|
189
|
+
}
|
|
190
|
+
.sw-suggest__type {
|
|
191
|
+
flex: none;
|
|
192
|
+
font-size: 0.8125rem;
|
|
193
|
+
opacity: 0.7;
|
|
194
|
+
}
|
|
195
|
+
.sw-visually-hidden {
|
|
196
|
+
position: absolute;
|
|
197
|
+
width: 1px;
|
|
198
|
+
height: 1px;
|
|
199
|
+
margin: -1px;
|
|
200
|
+
overflow: hidden;
|
|
201
|
+
clip: rect(0, 0, 0, 0);
|
|
202
|
+
white-space: nowrap;
|
|
203
|
+
border: 0;
|
|
204
|
+
}
|
|
@@ -2,7 +2,6 @@ import type { Metadata } from 'next';
|
|
|
2
2
|
import type { ReactNode } from 'react';
|
|
3
3
|
import { SpillwayStyles } from '@spillway-cms/next/page';
|
|
4
4
|
import { currentPreview } from '@spillway-cms/next/server';
|
|
5
|
-
import { Nav } from '../components/Nav';
|
|
6
5
|
import { SITE_URL, spillway } from '../lib/spillway';
|
|
7
6
|
import builtBrand from './spillway-brand.json';
|
|
8
7
|
// The brand as it was at build time (scripts/brand.mjs, run before every build): colors, fonts, type scale and spacing as
|
|
@@ -19,21 +18,20 @@ export async function generateMetadata(): Promise<Metadata> {
|
|
|
19
18
|
};
|
|
20
19
|
}
|
|
21
20
|
|
|
22
|
-
/** The site
|
|
21
|
+
/** The site settings and the brand, read through the preview when there is one. */
|
|
23
22
|
async function loadShell() {
|
|
24
23
|
const preview = await currentPreview({ secret: spillway.options.previewSecret });
|
|
25
24
|
const client = preview ? spillway.withPreview(preview) : spillway;
|
|
26
|
-
const [site, brand
|
|
27
|
-
|
|
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 };
|
|
25
|
+
const [site, brand] = await Promise.all([client.site().catch(() => null), client.brand().catch(() => null)]);
|
|
26
|
+
return { preview, site, brand };
|
|
33
27
|
}
|
|
34
28
|
|
|
29
|
+
/**
|
|
30
|
+
* The document around every page: the block stylesheet, the brand, search suggestions and, in a preview, the preview
|
|
31
|
+
* bar. The header and footer are components/SiteShell.tsx, which each page renders in its own language.
|
|
32
|
+
*/
|
|
35
33
|
export default async function RootLayout({ children }: { children: ReactNode }) {
|
|
36
|
-
const { preview, site, brand
|
|
34
|
+
const { preview, site, brand } = await loadShell();
|
|
37
35
|
// A brand published since the last build arrives with the brand.published webhook; inline it until the next build
|
|
38
36
|
// writes it into spillway-brand.css.
|
|
39
37
|
const newerBrand = brand && brand.version !== builtBrand.version ? brand.css : null;
|
|
@@ -43,6 +41,8 @@ export default async function RootLayout({ children }: { children: ReactNode })
|
|
|
43
41
|
{/* The block stylesheet, so Spillway's blocks look as they do on the Spillway site. */}
|
|
44
42
|
<SpillwayStyles />
|
|
45
43
|
{newerBrand ? <style id="spillway-brand" dangerouslySetInnerHTML={{ __html: newerBrand }} /> : null}
|
|
44
|
+
{/* Search suggestions while visitors type (Site settings → Search), for the search page and the Search blocks. */}
|
|
45
|
+
{site?.searchSuggestions !== false ? <script src="/spillway-suggest.js" defer /> : null}
|
|
46
46
|
</head>
|
|
47
47
|
<body>
|
|
48
48
|
<a className="skip-link" href="#main">
|
|
@@ -54,17 +54,7 @@ export default async function RootLayout({ children }: { children: ReactNode })
|
|
|
54
54
|
<a href="/api/preview/exit">Exit preview</a>
|
|
55
55
|
</div>
|
|
56
56
|
) : null}
|
|
57
|
-
|
|
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>
|
|
57
|
+
{children}
|
|
68
58
|
</body>
|
|
69
59
|
</html>
|
|
70
60
|
);
|
|
@@ -1,10 +1,15 @@
|
|
|
1
|
+
import { SiteShell } from '../components/SiteShell';
|
|
2
|
+
|
|
3
|
+
/** The 404 page, in the site's default language. */
|
|
1
4
|
export default function NotFound() {
|
|
2
5
|
return (
|
|
3
|
-
<
|
|
4
|
-
<
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
6
|
+
<SiteShell>
|
|
7
|
+
<section className="not-found">
|
|
8
|
+
<h1>Page not found</h1>
|
|
9
|
+
<p>
|
|
10
|
+
There is no published page at this address. <a href="/">Go to the home page</a>.
|
|
11
|
+
</p>
|
|
12
|
+
</section>
|
|
13
|
+
</SiteShell>
|
|
9
14
|
);
|
|
10
15
|
}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import type { Metadata } from 'next';
|
|
2
|
+
import { notFound } from 'next/navigation';
|
|
3
|
+
import { siteLocales } from '@spillway-cms/client';
|
|
4
|
+
import { SearchPage } from '../../components/SearchPage';
|
|
5
|
+
import { spillway } from '../../lib/spillway';
|
|
6
|
+
|
|
7
|
+
type Props = { searchParams: Promise<Record<string, string | string[] | undefined>> };
|
|
8
|
+
|
|
9
|
+
const first = (v: string | string[] | undefined) => (Array.isArray(v) ? v[0] : v) ?? '';
|
|
10
|
+
|
|
11
|
+
// Rendered for each request: the results depend on the query string.
|
|
12
|
+
export const dynamic = 'force-dynamic';
|
|
13
|
+
|
|
14
|
+
export async function generateMetadata(): Promise<Metadata> {
|
|
15
|
+
const site = await spillway.site().catch(() => null);
|
|
16
|
+
return { title: `Search | ${site?.name ?? 'Spillway site'}`, robots: { index: false } };
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
/** /search, and /es/search and so on through the rewrite in next.config.ts (`?locale=es`). */
|
|
20
|
+
export default async function Search({ searchParams }: Props) {
|
|
21
|
+
const query = await searchParams;
|
|
22
|
+
const site = await spillway.site().catch(() => null);
|
|
23
|
+
const asked = first(query.locale);
|
|
24
|
+
const locale = asked ? site && siteLocales(site).find((l) => l.toLowerCase() === asked.toLowerCase()) : (site?.defaultLocale ?? 'en');
|
|
25
|
+
if (!locale) notFound(); // /xx/search where xx is not one of the site's languages
|
|
26
|
+
const q = first(query.q).trim().slice(0, 200);
|
|
27
|
+
const page = Math.max(1, Math.min(100, Number.parseInt(first(query.page) || '1', 10) || 1));
|
|
28
|
+
return <SearchPage site={site} locale={locale} q={q} page={page} />;
|
|
29
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import type { LanguageLink } from '@spillway-cms/client';
|
|
2
|
+
|
|
3
|
+
/** One link per site language: the page's translation, or that language's home page. Nothing for a site with one locale. */
|
|
4
|
+
export function LanguageSwitcher({ links }: { links: LanguageLink[] }) {
|
|
5
|
+
if (!links.length) return null;
|
|
6
|
+
return (
|
|
7
|
+
<nav className="language-switcher" aria-label="Language">
|
|
8
|
+
<ul>
|
|
9
|
+
{links.map((l) => (
|
|
10
|
+
<li key={l.locale}>
|
|
11
|
+
<a href={l.href} hrefLang={l.locale} lang={l.locale} aria-current={l.current ? 'true' : undefined}>
|
|
12
|
+
{l.label}
|
|
13
|
+
</a>
|
|
14
|
+
</li>
|
|
15
|
+
))}
|
|
16
|
+
</ul>
|
|
17
|
+
</nav>
|
|
18
|
+
);
|
|
19
|
+
}
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
import type { PublicSite } from '@spillway-cms/client';
|
|
2
|
+
import { searchLanguages, searchPath, searchSite } from '../lib/spillway';
|
|
3
|
+
import { SiteShell } from './SiteShell';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* The search page (/search, /es/search): a GET form, the published pages that match (read as a visitor, so drafts never
|
|
7
|
+
* show) and a pager. With suggestions on in Spillway (Site settings → Search), public/spillway-suggest.js suggests pages
|
|
8
|
+
* while visitors type.
|
|
9
|
+
*/
|
|
10
|
+
export async function SearchPage({ site, locale, q, page }: { site: PublicSite | null; locale: string; q: string; page: number }) {
|
|
11
|
+
const found = await searchSite(q, locale, page);
|
|
12
|
+
const action = searchPath(site, locale);
|
|
13
|
+
const suggest = site?.searchSuggestions !== false;
|
|
14
|
+
const pageHref = (n: number) => `${action}?q=${encodeURIComponent(q)}${n > 1 ? `&page=${n}` : ''}`;
|
|
15
|
+
return (
|
|
16
|
+
<SiteShell locale={locale} languages={searchLanguages(site, locale, q)}>
|
|
17
|
+
<section className="search-page">
|
|
18
|
+
<h1>Search</h1>
|
|
19
|
+
<form className="search-form" role="search" method="get" action={action}>
|
|
20
|
+
<label className="sw-visually-hidden" htmlFor="search-q">
|
|
21
|
+
Search this site
|
|
22
|
+
</label>
|
|
23
|
+
<input id="search-q" type="search" name="q" defaultValue={q} placeholder="Search" enterKeyHint="search" {...(suggest ? { 'data-sw-suggest': '', 'data-sw-locale': locale } : {})} />
|
|
24
|
+
<button type="submit">Search</button>
|
|
25
|
+
</form>
|
|
26
|
+
{found.error ? (
|
|
27
|
+
<p role="alert">Search is not available right now. Please try again later.</p>
|
|
28
|
+
) : q ? (
|
|
29
|
+
<>
|
|
30
|
+
<p role="status">
|
|
31
|
+
{found.total === 1 ? '1 result' : `${found.total} results`} for “{q}”
|
|
32
|
+
</p>
|
|
33
|
+
{found.results.length ? (
|
|
34
|
+
<ol className="search-results">
|
|
35
|
+
{found.results.map((r) => (
|
|
36
|
+
<li key={r.id}>
|
|
37
|
+
<h2>
|
|
38
|
+
<a href={r.href}>{r.title}</a>
|
|
39
|
+
</h2>
|
|
40
|
+
{r.snippet.length ? <p>{r.snippet.map((part, i) => (part.hit ? <mark key={i}>{part.text}</mark> : <span key={i}>{part.text}</span>))}</p> : null}
|
|
41
|
+
</li>
|
|
42
|
+
))}
|
|
43
|
+
</ol>
|
|
44
|
+
) : null}
|
|
45
|
+
{found.pages > 1 ? (
|
|
46
|
+
<nav className="search-pager" aria-label="Result pages">
|
|
47
|
+
{page > 1 ? (
|
|
48
|
+
<a href={pageHref(page - 1)} rel="prev">
|
|
49
|
+
Previous
|
|
50
|
+
</a>
|
|
51
|
+
) : null}
|
|
52
|
+
<span>
|
|
53
|
+
Page {page} of {found.pages}
|
|
54
|
+
</span>
|
|
55
|
+
{page < found.pages ? (
|
|
56
|
+
<a href={pageHref(page + 1)} rel="next">
|
|
57
|
+
Next
|
|
58
|
+
</a>
|
|
59
|
+
) : null}
|
|
60
|
+
</nav>
|
|
61
|
+
) : null}
|
|
62
|
+
</>
|
|
63
|
+
) : null}
|
|
64
|
+
</section>
|
|
65
|
+
</SiteShell>
|
|
66
|
+
);
|
|
67
|
+
}
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import type { ReactNode } from 'react';
|
|
2
|
+
import type { LanguageLink, PublicMenuItem } from '@spillway-cms/client';
|
|
3
|
+
import { currentPreview } from '@spillway-cms/next/server';
|
|
4
|
+
import { spillway } from '../lib/spillway';
|
|
5
|
+
import { LanguageSwitcher } from './LanguageSwitcher';
|
|
6
|
+
import { Nav } from './Nav';
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* The header and footer around a page: the site name, the `main` and `footer` menus (Structure → Menus) and the language
|
|
10
|
+
* switcher, in the page's language. A page passes the menus Spillway rendered with it; the search page and the 404 read
|
|
11
|
+
* them for their language. It lives in the pages rather than in app/layout.tsx because a layout does not know its page.
|
|
12
|
+
*/
|
|
13
|
+
export async function SiteShell({ locale, menus, languages = [], children }: { locale?: string; menus?: { main?: PublicMenuItem[]; footer?: PublicMenuItem[] }; languages?: LanguageLink[]; children: ReactNode }) {
|
|
14
|
+
const preview = await currentPreview({ secret: spillway.options.previewSecret });
|
|
15
|
+
const client = preview ? spillway.withPreview(preview) : spillway;
|
|
16
|
+
const opts = locale ? { locale } : {};
|
|
17
|
+
const [site, main, footer] = await Promise.all([
|
|
18
|
+
client.site().catch(() => null),
|
|
19
|
+
menus?.main ? null : client.menu('main', opts).catch(() => null),
|
|
20
|
+
menus?.footer ? null : client.menu('footer', opts).catch(() => null),
|
|
21
|
+
]);
|
|
22
|
+
return (
|
|
23
|
+
<div lang={locale ?? site?.defaultLocale}>
|
|
24
|
+
<header className="site-header">
|
|
25
|
+
<a className="site-name" href="/">
|
|
26
|
+
{site?.name ?? 'Spillway site'}
|
|
27
|
+
</a>
|
|
28
|
+
<Nav items={menus?.main ?? main?.items ?? []} label="Main" />
|
|
29
|
+
<LanguageSwitcher links={languages} />
|
|
30
|
+
</header>
|
|
31
|
+
<main id="main">{children}</main>
|
|
32
|
+
<footer className="site-footer">
|
|
33
|
+
<Nav items={menus?.footer ?? footer?.items ?? []} label="Footer" />
|
|
34
|
+
{site?.footerNote?.split('\n').map((line, i) => <p key={i}>{line}</p>)}
|
|
35
|
+
</footer>
|
|
36
|
+
</div>
|
|
37
|
+
);
|
|
38
|
+
}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { createSpillwayClient } from '@spillway-cms/next';
|
|
2
|
-
import type
|
|
2
|
+
import { languageLinks, localizedPath, type LanguageLink, type PublicRedirect, type PublicRender, type PublicSite } from '@spillway-cms/client';
|
|
3
3
|
|
|
4
4
|
/** The Spillway site (SPILLWAY_URL in .env.local). */
|
|
5
5
|
export const SPILLWAY_URL = (process.env.SPILLWAY_URL || 'http://localhost:3000').replace(/\/+$/, '');
|
|
@@ -69,3 +69,51 @@ export function withSiteUrls(page: PublicRender): PublicRender {
|
|
|
69
69
|
},
|
|
70
70
|
};
|
|
71
71
|
}
|
|
72
|
+
|
|
73
|
+
// ---------- Locales and search ----------
|
|
74
|
+
|
|
75
|
+
/** The language switcher for a page: its translations, or each language's home page. Empty for a site with one locale. */
|
|
76
|
+
export function pageLanguages(site: PublicSite | null, page: PublicRender): LanguageLink[] {
|
|
77
|
+
return site ? languageLinks(site, { locale: page.locale, path: page.path, alternates: page.seo.alternates }) : [];
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
/** The language switcher on the search page: the search page of each language, with the same words. */
|
|
81
|
+
export function searchLanguages(site: PublicSite | null, locale: string, q: string): LanguageLink[] {
|
|
82
|
+
if (!site) return [];
|
|
83
|
+
return languageLinks(site, { locale, alternates: [] }).map((l) => ({ ...l, href: `${localizedPath(site, l.locale, '/search')}${q ? `?q=${encodeURIComponent(q)}` : ''}`, translated: true }));
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
/** The search page in a locale. */
|
|
87
|
+
export function searchPath(site: PublicSite | null, locale: string): string {
|
|
88
|
+
return site ? localizedPath(site, locale, '/search') : '/search';
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/** Search and suggestions read as a visitor: without the token, which would also find drafts. */
|
|
92
|
+
export const visitor = spillway.with({ token: undefined, site: undefined });
|
|
93
|
+
|
|
94
|
+
/** One search result: the page and the passage that matched, with the matched words marked. */
|
|
95
|
+
export interface SearchHit {
|
|
96
|
+
id: string;
|
|
97
|
+
title: string;
|
|
98
|
+
href: string;
|
|
99
|
+
snippet: { text: string; hit: boolean }[];
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
/** Spillway marks the matched words in a search snippet «like this»; the parts, for a <mark> around each match. */
|
|
103
|
+
export function snippetParts(snippet: string): SearchHit['snippet'] {
|
|
104
|
+
const parts: SearchHit['snippet'] = [];
|
|
105
|
+
for (const [i, text] of snippet.split(/[«»]/).entries()) if (text) parts.push({ text, hit: i % 2 === 1 });
|
|
106
|
+
return parts;
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
/** The published pages matching `q` in `locale`, ten a page; `error` when Spillway could not answer. */
|
|
110
|
+
export async function searchSite(q: string, locale: string, page: number): Promise<{ results: SearchHit[]; total: number; pages: number; error?: string }> {
|
|
111
|
+
if (!q) return { results: [], total: 0, pages: 0 };
|
|
112
|
+
try {
|
|
113
|
+
const found = await visitor.search(q, { locale, page, limit: 10 });
|
|
114
|
+
const results = found.items.filter((r) => r.path).map((r) => ({ id: r.id, title: r.title, href: r.path!, snippet: snippetParts(r.snippet) }));
|
|
115
|
+
return { results, total: found.total, pages: Math.ceil(found.total / (found.limit || 10)) };
|
|
116
|
+
} catch (err) {
|
|
117
|
+
return { results: [], total: 0, pages: 0, error: err instanceof Error ? err.message : String(err) };
|
|
118
|
+
}
|
|
119
|
+
}
|
|
@@ -5,9 +5,10 @@ const SPILLWAY_URL = process.env.SPILLWAY_URL || 'http://localhost:3000';
|
|
|
5
5
|
|
|
6
6
|
const config: NextConfig = {
|
|
7
7
|
// Form posts (/api/forms/*) and media (/media/*) go to Spillway through this origin, so the blocks' relative URLs work.
|
|
8
|
+
// The search page in each language (/es/search) is app/search with ?locale=es.
|
|
8
9
|
// Do not list @puckeditor/core or @spillway-cms/blocks in serverExternalPackages: Next must bundle them with its React.
|
|
9
10
|
async rewrites() {
|
|
10
|
-
return spillwayRewrites(SPILLWAY_URL);
|
|
11
|
+
return [{ source: '/:locale([a-zA-Z]{2,3}-[a-zA-Z0-9]{2,8}|[a-zA-Z]{2,3})/search', destination: '/search?locale=:locale' }, ...spillwayRewrites(SPILLWAY_URL)];
|
|
11
12
|
},
|
|
12
13
|
};
|
|
13
14
|
|
|
@@ -0,0 +1,133 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Search suggestions while visitors type, for every search field marked data-sw-suggest: the field on the search page
|
|
3
|
+
* and Spillway's Search blocks (Spillway marks them when Site settings → Search has suggestions on). It asks this front
|
|
4
|
+
* end's /api/search/suggest, which reads Spillway's published pages, and shows them as a list under the field: the arrow
|
|
5
|
+
* keys move through it, Enter opens the highlighted page, Escape closes it. Without this script the field is a plain
|
|
6
|
+
* search form. Text is set with textContent, never parsed as HTML.
|
|
7
|
+
*/
|
|
8
|
+
(function () {
|
|
9
|
+
var d = document;
|
|
10
|
+
var count = 0;
|
|
11
|
+
function bind(input) {
|
|
12
|
+
if (input.hasAttribute('data-sw-bound') || !input.form) return;
|
|
13
|
+
input.setAttribute('data-sw-bound', '');
|
|
14
|
+
var form = input.form;
|
|
15
|
+
var id = 'sw-suggest-' + ++count;
|
|
16
|
+
var locale = input.getAttribute('data-sw-locale') || '';
|
|
17
|
+
var list = d.createElement('ul');
|
|
18
|
+
var live = d.createElement('div');
|
|
19
|
+
var timer = 0;
|
|
20
|
+
var active = -1;
|
|
21
|
+
var asked = '';
|
|
22
|
+
list.id = id;
|
|
23
|
+
list.className = 'sw-suggest';
|
|
24
|
+
list.setAttribute('role', 'listbox');
|
|
25
|
+
list.setAttribute('aria-label', 'Suggestions');
|
|
26
|
+
list.hidden = true;
|
|
27
|
+
live.className = 'sw-visually-hidden';
|
|
28
|
+
live.setAttribute('aria-live', 'polite');
|
|
29
|
+
form.classList.add('sw-suggest-host');
|
|
30
|
+
form.appendChild(list);
|
|
31
|
+
form.appendChild(live);
|
|
32
|
+
input.setAttribute('role', 'combobox');
|
|
33
|
+
input.setAttribute('aria-autocomplete', 'list');
|
|
34
|
+
input.setAttribute('aria-expanded', 'false');
|
|
35
|
+
input.setAttribute('aria-controls', id);
|
|
36
|
+
input.setAttribute('autocomplete', 'off');
|
|
37
|
+
|
|
38
|
+
function close() {
|
|
39
|
+
list.hidden = true;
|
|
40
|
+
active = -1;
|
|
41
|
+
input.setAttribute('aria-expanded', 'false');
|
|
42
|
+
input.removeAttribute('aria-activedescendant');
|
|
43
|
+
}
|
|
44
|
+
function mark(i) {
|
|
45
|
+
var options = list.children;
|
|
46
|
+
for (var k = 0; k < options.length; k++) options[k].setAttribute('aria-selected', k === i ? 'true' : 'false');
|
|
47
|
+
active = i;
|
|
48
|
+
if (i < 0) input.removeAttribute('aria-activedescendant');
|
|
49
|
+
else input.setAttribute('aria-activedescendant', options[i].id);
|
|
50
|
+
}
|
|
51
|
+
function show(items) {
|
|
52
|
+
list.textContent = '';
|
|
53
|
+
active = -1;
|
|
54
|
+
if (!items.length) {
|
|
55
|
+
close();
|
|
56
|
+
live.textContent = '';
|
|
57
|
+
return;
|
|
58
|
+
}
|
|
59
|
+
items.forEach(function (item, i) {
|
|
60
|
+
var li = d.createElement('li');
|
|
61
|
+
var a = d.createElement('a');
|
|
62
|
+
var title = d.createElement('span');
|
|
63
|
+
var type = d.createElement('span');
|
|
64
|
+
li.id = id + '-' + i;
|
|
65
|
+
li.className = 'sw-suggest__item';
|
|
66
|
+
li.setAttribute('role', 'option');
|
|
67
|
+
li.setAttribute('aria-selected', 'false');
|
|
68
|
+
a.href = item.path;
|
|
69
|
+
a.tabIndex = -1;
|
|
70
|
+
title.className = 'sw-suggest__title';
|
|
71
|
+
title.textContent = item.title;
|
|
72
|
+
type.className = 'sw-suggest__type';
|
|
73
|
+
type.textContent = item.typeLabel;
|
|
74
|
+
a.appendChild(title);
|
|
75
|
+
a.appendChild(type);
|
|
76
|
+
li.appendChild(a);
|
|
77
|
+
li.addEventListener('mousedown', function (e) {
|
|
78
|
+
e.preventDefault();
|
|
79
|
+
});
|
|
80
|
+
list.appendChild(li);
|
|
81
|
+
});
|
|
82
|
+
list.hidden = false;
|
|
83
|
+
input.setAttribute('aria-expanded', 'true');
|
|
84
|
+
live.textContent = items.length + (items.length === 1 ? ' suggestion' : ' suggestions') + ', use the up and down arrows to choose';
|
|
85
|
+
}
|
|
86
|
+
function ask() {
|
|
87
|
+
var q = input.value.trim();
|
|
88
|
+
if (q === asked) return;
|
|
89
|
+
asked = q;
|
|
90
|
+
if (q.length < 2) return show([]);
|
|
91
|
+
var url = '/api/search/suggest?q=' + encodeURIComponent(q) + (locale ? '&locale=' + encodeURIComponent(locale) : '');
|
|
92
|
+
fetch(url, { headers: { accept: 'application/json' } })
|
|
93
|
+
.then(function (res) {
|
|
94
|
+
return res.ok ? res.json() : { items: [] };
|
|
95
|
+
})
|
|
96
|
+
.then(function (body) {
|
|
97
|
+
if (input.value.trim() === q) show((body && body.items) || []);
|
|
98
|
+
})
|
|
99
|
+
.catch(function () {
|
|
100
|
+
show([]);
|
|
101
|
+
});
|
|
102
|
+
}
|
|
103
|
+
input.addEventListener('input', function () {
|
|
104
|
+
clearTimeout(timer);
|
|
105
|
+
timer = setTimeout(ask, 150);
|
|
106
|
+
});
|
|
107
|
+
input.addEventListener('keydown', function (e) {
|
|
108
|
+
var n = list.children.length;
|
|
109
|
+
if (e.key === 'ArrowDown' && n) {
|
|
110
|
+
e.preventDefault();
|
|
111
|
+
list.hidden = false;
|
|
112
|
+
input.setAttribute('aria-expanded', 'true');
|
|
113
|
+
mark((active + 1) % n);
|
|
114
|
+
} else if (e.key === 'ArrowUp' && n) {
|
|
115
|
+
e.preventDefault();
|
|
116
|
+
mark(active <= 0 ? n - 1 : active - 1);
|
|
117
|
+
} else if (e.key === 'Enter' && active >= 0 && !list.hidden) {
|
|
118
|
+
e.preventDefault();
|
|
119
|
+
location.href = list.children[active].querySelector('a').href;
|
|
120
|
+
} else if (e.key === 'Escape' && !list.hidden) {
|
|
121
|
+
e.preventDefault();
|
|
122
|
+
close();
|
|
123
|
+
}
|
|
124
|
+
});
|
|
125
|
+
input.addEventListener('blur', close);
|
|
126
|
+
}
|
|
127
|
+
function scan() {
|
|
128
|
+
var fields = d.querySelectorAll('form [data-sw-suggest]');
|
|
129
|
+
for (var i = 0; i < fields.length; i++) bind(fields[i]);
|
|
130
|
+
}
|
|
131
|
+
if (d.readyState === 'loading') d.addEventListener('DOMContentLoaded', scan);
|
|
132
|
+
else scan();
|
|
133
|
+
})();
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
# Copy to .env (create-spillway wrote one for you) and fill in. Never commit .env.
|
|
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. Without it, only release
|
|
16
|
+
# share links open a preview.
|
|
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:3002
|
|
25
|
+
|
|
26
|
+
# How long a page read from Spillway stays in the in-memory cache when no webhook purges it, in seconds; 0 turns it off.
|
|
27
|
+
SPILLWAY_CACHE_SECONDS=300
|