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,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
|
+
})();
|
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
---
|
|
2
2
|
/**
|
|
3
3
|
* The shell around every page: the head from the page's SEO data (canonical links on this front end's address), the
|
|
4
|
-
* brand, the block stylesheet, the main and footer menus and, in a preview, the preview bar.
|
|
4
|
+
* brand, the block stylesheet, the main and footer menus, the language switcher and, in a preview, the preview bar.
|
|
5
5
|
*/
|
|
6
6
|
import SpillwayStyles from '@spillway-cms/astro/components/SpillwayStyles.astro';
|
|
7
7
|
import { renderHead, renderMenu } from '@spillway-cms/astro/render';
|
|
8
|
-
import type { PreviewState, PublicRender } from '@spillway-cms/client';
|
|
8
|
+
import type { LanguageLink, PreviewState, PublicRender } from '@spillway-cms/client';
|
|
9
9
|
import { loadShell, withSiteUrls } from '../lib/spillway';
|
|
10
10
|
// The brand as it was at build time (scripts/brand.mjs, run before every build): the --sw-* variables and fonts.
|
|
11
11
|
import builtCss from '../styles/spillway-brand.css?raw';
|
|
@@ -16,25 +16,31 @@ interface Props {
|
|
|
16
16
|
page: PublicRender | null;
|
|
17
17
|
preview?: PreviewState | null;
|
|
18
18
|
title?: string;
|
|
19
|
+
/** The language of a page that is not a Spillway page (the search page, a 404). */
|
|
20
|
+
locale?: string;
|
|
21
|
+
/** One link per site locale; empty for a site with one locale. */
|
|
22
|
+
languages?: LanguageLink[];
|
|
19
23
|
}
|
|
20
24
|
|
|
21
|
-
const { page, preview = null, title } = Astro.props;
|
|
22
|
-
const
|
|
25
|
+
const { page, preview = null, title, locale, languages = [] } = Astro.props;
|
|
26
|
+
const lang = page?.locale ?? locale;
|
|
27
|
+
const { site, brand, main, footer } = await loadShell(preview, lang);
|
|
23
28
|
// A brand published since the last build arrives with the brand.published webhook; use it until the next build writes it.
|
|
24
29
|
const brandCss = brand && brand.version !== builtBrand.version ? brand.css : builtCss;
|
|
25
30
|
const siteName = site?.name ?? page?.site.name ?? 'Spillway site';
|
|
26
|
-
// The page's own menus mark the current item; pages
|
|
31
|
+
// The page's own menus mark the current item; other pages (search, a 404) use the menus read for the shell.
|
|
27
32
|
const mainItems = page?.menus.main ?? main?.items;
|
|
28
33
|
const footerItems = page?.menus.footer ?? footer?.items;
|
|
29
34
|
---
|
|
30
35
|
<!doctype html>
|
|
31
|
-
<html lang={
|
|
36
|
+
<html lang={lang ?? site?.defaultLocale ?? 'en'}>
|
|
32
37
|
<head>
|
|
33
38
|
<meta charset="utf-8" />
|
|
34
39
|
<meta name="viewport" content="width=device-width, initial-scale=1" />
|
|
35
40
|
{page ? <Fragment set:html={renderHead(withSiteUrls(page))} /> : <title>{title ?? `Page not found | ${siteName}`}</title>}
|
|
36
41
|
<Fragment set:html={`<style id="spillway-brand">${brandCss}</style>`} />
|
|
37
42
|
<SpillwayStyles />
|
|
43
|
+
{site?.searchSuggestions !== false && <script is:inline src="/spillway-suggest.js" defer></script>}
|
|
38
44
|
</head>
|
|
39
45
|
<body>
|
|
40
46
|
<a class="skip-link" href="#main">Skip to content</a>
|
|
@@ -47,6 +53,15 @@ const footerItems = page?.menus.footer ?? footer?.items;
|
|
|
47
53
|
<header class="site-header">
|
|
48
54
|
<a class="site-name" href="/">{siteName}</a>
|
|
49
55
|
<nav class="site-nav" aria-label="Main"><Fragment set:html={renderMenu(mainItems)} /></nav>
|
|
56
|
+
{languages.length > 0 && (
|
|
57
|
+
<nav class="language-switcher" aria-label="Language">
|
|
58
|
+
<ul>
|
|
59
|
+
{languages.map((l) => (
|
|
60
|
+
<li><a href={l.href} hreflang={l.locale} lang={l.locale} aria-current={l.current ? 'true' : undefined}>{l.label}</a></li>
|
|
61
|
+
))}
|
|
62
|
+
</ul>
|
|
63
|
+
</nav>
|
|
64
|
+
)}
|
|
50
65
|
</header>
|
|
51
66
|
<main id="main"><slot /></main>
|
|
52
67
|
<footer class="site-footer">
|
|
@@ -6,7 +6,7 @@
|
|
|
6
6
|
import type { AstroCookies } from 'astro';
|
|
7
7
|
import { client } from 'spillway:client';
|
|
8
8
|
import { SpillwayError, previewFromRequest, type PreviewState } from '@spillway-cms/astro';
|
|
9
|
-
import type
|
|
9
|
+
import { languageLinks, localizedPath, splitLocalePath, type LanguageLink, type PublicBrand, type PublicMenu, type PublicRedirect, type PublicRender, type PublicSite } from '@spillway-cms/client';
|
|
10
10
|
import { cached } from './cache';
|
|
11
11
|
|
|
12
12
|
export { client };
|
|
@@ -17,38 +17,111 @@ export const SITE_URL = (process.env.SITE_URL || 'http://localhost:4321').replac
|
|
|
17
17
|
/** Tag of the entries every webhook purges: the site settings, the brand, the menus, the redirects and the path list. */
|
|
18
18
|
export const SHELL_TAG = 'shell';
|
|
19
19
|
|
|
20
|
-
/**
|
|
21
|
-
|
|
22
|
-
|
|
20
|
+
/** Query parameters that open a preview rather than reach the page's blocks. */
|
|
21
|
+
const PREVIEW_PARAMS = new Set(['release', 'workstream', 'token', 'preview', 'secret']);
|
|
22
|
+
|
|
23
|
+
/** The query parameters a page's blocks read (exposed filters, a pager, a calendar month). */
|
|
24
|
+
export function blockParams(url: URL): Record<string, string> {
|
|
25
|
+
return Object.fromEntries([...url.searchParams].filter(([k]) => !PREVIEW_PARAMS.has(k)));
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/** The page at `path`, or null when Spillway has none. A preview skips the cache; so does a page whose blocks read the query. */
|
|
29
|
+
export async function loadPage(path: string, preview: PreviewState | null = null, params: Record<string, string> = {}): Promise<PublicRender | null> {
|
|
30
|
+
const fetchPage = async (query: Record<string, string>) => {
|
|
23
31
|
try {
|
|
24
|
-
return await client.render(path, { previewState: preview });
|
|
32
|
+
return await client.render(path, { previewState: preview, params: query });
|
|
25
33
|
} catch (err) {
|
|
26
|
-
|
|
34
|
+
// Not found, or members-only (signing members in on this front end is still to come; see README.md, Members).
|
|
35
|
+
if (err instanceof SpillwayError && (err.status === 404 || err.code === 'members_only')) return null;
|
|
27
36
|
throw err;
|
|
28
37
|
}
|
|
29
38
|
};
|
|
30
|
-
if (preview) return fetchPage();
|
|
31
|
-
|
|
32
|
-
const
|
|
39
|
+
if (preview) return fetchPage(params);
|
|
40
|
+
const page = await cached(`render:${path}`, async () => {
|
|
41
|
+
const value = await fetchPage({});
|
|
33
42
|
// A 404 is not kept: the page may be published any moment.
|
|
34
|
-
return { value
|
|
43
|
+
return { value, tags: value ? [...value.cacheTags, `path:${path}`] : [], keep: value !== null };
|
|
35
44
|
});
|
|
45
|
+
// Exposed filters, a pager or a calendar read the query string: render this request on its own (Spillway caches it briefly).
|
|
46
|
+
return page?.queryDependent && Object.keys(params).length ? fetchPage(params) : page;
|
|
36
47
|
}
|
|
37
48
|
|
|
38
|
-
/** The site settings,
|
|
39
|
-
export
|
|
49
|
+
/** The site settings (name, locales, search), cached; null when Spillway does not answer. */
|
|
50
|
+
export function loadSite(preview: PreviewState | null = null): Promise<PublicSite | null> {
|
|
51
|
+
const load = () => client.withPreview(preview).site();
|
|
52
|
+
return (preview ? load() : cached('site', async () => ({ value: await load(), tags: [SHELL_TAG] }))).catch(() => null);
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/** The site settings, the brand and the main and footer menus in `locale`, each null when Spillway does not answer it. */
|
|
56
|
+
export async function loadShell(preview: PreviewState | null = null, locale?: string): Promise<{ site: PublicSite | null; brand: PublicBrand | null; main: PublicMenu | null; footer: PublicMenu | null }> {
|
|
40
57
|
const c = preview ? client.withPreview(preview) : client;
|
|
41
58
|
const read = <T>(key: string, load: () => Promise<T>): Promise<T | null> =>
|
|
42
59
|
(preview ? load() : cached(key, async () => ({ value: await load(), tags: [SHELL_TAG] }))).catch(() => null);
|
|
60
|
+
const opts = locale ? { locale } : {};
|
|
43
61
|
const [site, brand, main, footer] = await Promise.all([
|
|
44
|
-
|
|
62
|
+
loadSite(preview),
|
|
45
63
|
read('brand', () => c.brand()),
|
|
46
|
-
read(
|
|
47
|
-
read(
|
|
64
|
+
read(`menu:main:${locale ?? ''}`, () => c.menu('main', opts)),
|
|
65
|
+
read(`menu:footer:${locale ?? ''}`, () => c.menu('footer', opts)),
|
|
48
66
|
]);
|
|
49
67
|
return { site, brand, main, footer };
|
|
50
68
|
}
|
|
51
69
|
|
|
70
|
+
// ---------- Locales and search ----------
|
|
71
|
+
|
|
72
|
+
/** The locale a path is in, and whether it is that locale's search page (/search, /es/search). */
|
|
73
|
+
export function routeOf(site: PublicSite | null, path: string): { locale: string; search: boolean } {
|
|
74
|
+
const local = site ? splitLocalePath(site, path) : { locale: 'en', path };
|
|
75
|
+
return { locale: local.locale, search: local.path === '/search' };
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/** The language switcher for a page: its translations, or each language's home page. Empty for a site with one locale. */
|
|
79
|
+
export function pageLanguages(site: PublicSite | null, page: PublicRender | null, locale: string): LanguageLink[] {
|
|
80
|
+
if (!site) return [];
|
|
81
|
+
return page ? languageLinks(site, { locale: page.locale, path: page.path, alternates: page.seo.alternates }) : languageLinks(site, { locale, alternates: [] });
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
/** The language switcher on the search page: the search page of each language, with the same words. */
|
|
85
|
+
export function searchLanguages(site: PublicSite | null, locale: string, q: string): LanguageLink[] {
|
|
86
|
+
if (!site) return [];
|
|
87
|
+
return languageLinks(site, { locale, alternates: [] }).map((l) => ({ ...l, href: `${localizedPath(site, l.locale, '/search')}${q ? `?q=${encodeURIComponent(q)}` : ''}`, translated: true }));
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
/** The search page in a locale. */
|
|
91
|
+
export function searchPath(site: PublicSite | null, locale: string): string {
|
|
92
|
+
return site ? localizedPath(site, locale, '/search') : '/search';
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
/** Search and suggestions read as a visitor: without the token, which would also find drafts. */
|
|
96
|
+
export const visitor = client.with({ token: undefined, site: undefined });
|
|
97
|
+
|
|
98
|
+
/** One search result: the page and the passage that matched, with the matched words marked. */
|
|
99
|
+
export interface SearchHit {
|
|
100
|
+
id: string;
|
|
101
|
+
title: string;
|
|
102
|
+
href: string;
|
|
103
|
+
snippet: { text: string; hit: boolean }[];
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
/** Spillway marks the matched words in a search snippet «like this»; the parts, for a <mark> around each match. */
|
|
107
|
+
export function snippetParts(snippet: string): SearchHit['snippet'] {
|
|
108
|
+
const parts: SearchHit['snippet'] = [];
|
|
109
|
+
for (const [i, text] of snippet.split(/[«»]/).entries()) if (text) parts.push({ text, hit: i % 2 === 1 });
|
|
110
|
+
return parts;
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
/** The published pages matching `q` in `locale`, ten a page; `error` when Spillway could not answer. */
|
|
114
|
+
export async function searchSite(q: string, locale: string, page: number): Promise<{ results: SearchHit[]; total: number; pages: number; error?: string }> {
|
|
115
|
+
if (!q) return { results: [], total: 0, pages: 0 };
|
|
116
|
+
try {
|
|
117
|
+
const found = await visitor.search(q, { locale, page, limit: 10 });
|
|
118
|
+
const results = found.items.filter((r) => r.path).map((r) => ({ id: r.id, title: r.title, href: r.path!, snippet: snippetParts(r.snippet) }));
|
|
119
|
+
return { results, total: found.total, pages: Math.ceil(found.total / (found.limit || 10)) };
|
|
120
|
+
} catch (err) {
|
|
121
|
+
return { results: [], total: 0, pages: 0, error: err instanceof Error ? err.message : String(err) };
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
|
|
52
125
|
/** Redirect matching as Spillway does it: case-insensitive, without a trailing slash or a query. */
|
|
53
126
|
export function normalizePath(raw: string): string {
|
|
54
127
|
let p = raw;
|
|
@@ -1,22 +1,74 @@
|
|
|
1
1
|
---
|
|
2
2
|
/**
|
|
3
|
-
* Every path: the published page Spillway has there, rendered with the core block library
|
|
4
|
-
* happens here in the frontmatter, before the layout starts streaming, so the 404
|
|
3
|
+
* Every path: the published page Spillway has there, rendered with the core block library; the search page (/search,
|
|
4
|
+
* /es/search); or a 404. The fetch happens here in the frontmatter, before the layout starts streaming, so the 404
|
|
5
|
+
* status still reaches the browser.
|
|
5
6
|
*/
|
|
6
7
|
import { pathFromParams } from '@spillway-cms/astro';
|
|
7
8
|
import { renderPage } from '@spillway-cms/astro/render';
|
|
8
9
|
import Layout from '../layouts/Layout.astro';
|
|
9
|
-
import { loadPage, readPreview } from '../lib/spillway';
|
|
10
|
+
import { blockParams, loadPage, loadSite, pageLanguages, readPreview, routeOf, searchLanguages, searchPath, searchSite } from '../lib/spillway';
|
|
10
11
|
|
|
11
12
|
const path = pathFromParams(Astro.params);
|
|
12
13
|
const preview = readPreview(Astro.cookies);
|
|
13
|
-
const
|
|
14
|
-
|
|
14
|
+
const site = await loadSite(preview);
|
|
15
|
+
const route = routeOf(site, path);
|
|
16
|
+
const page = route.search ? null : await loadPage(path, preview, blockParams(Astro.url));
|
|
17
|
+
if (!page && !route.search) Astro.response.status = 404;
|
|
15
18
|
if (preview) Astro.response.headers.set('cache-control', 'private, no-store');
|
|
19
|
+
|
|
20
|
+
// The search page: a GET form, the published pages that match (read as a visitor, so drafts never show) and a pager.
|
|
21
|
+
// With suggestions on in Spillway (Site settings → Search), public/spillway-suggest.js suggests pages while visitors type.
|
|
22
|
+
const q = route.search ? (Astro.url.searchParams.get('q') ?? '').trim().slice(0, 200) : '';
|
|
23
|
+
const pageNo = Math.max(1, Math.min(100, Number.parseInt(Astro.url.searchParams.get('page') ?? '1', 10) || 1));
|
|
24
|
+
const found = route.search ? await searchSite(q, route.locale, pageNo) : null;
|
|
25
|
+
const action = searchPath(site, route.locale);
|
|
26
|
+
const pageHref = (n: number) => `${action}?q=${encodeURIComponent(q)}${n > 1 ? `&page=${n}` : ''}`;
|
|
27
|
+
const suggest = site?.searchSuggestions !== false;
|
|
28
|
+
const siteName = site?.name ?? 'Spillway site';
|
|
16
29
|
---
|
|
17
|
-
<Layout
|
|
30
|
+
<Layout
|
|
31
|
+
page={page}
|
|
32
|
+
preview={preview}
|
|
33
|
+
locale={route.locale}
|
|
34
|
+
languages={route.search ? searchLanguages(site, route.locale, q) : pageLanguages(site, page, route.locale)}
|
|
35
|
+
title={route.search ? (q ? `Search: ${q} | ${siteName}` : `Search | ${siteName}`) : undefined}
|
|
36
|
+
>
|
|
18
37
|
{page ? (
|
|
19
38
|
<Fragment set:html={renderPage(page)} />
|
|
39
|
+
) : found ? (
|
|
40
|
+
<section class="search-page">
|
|
41
|
+
<h1>Search</h1>
|
|
42
|
+
<form class="search-form" role="search" method="get" action={action}>
|
|
43
|
+
<label class="sw-visually-hidden" for="search-q">Search this site</label>
|
|
44
|
+
<input id="search-q" type="search" name="q" value={q} placeholder="Search" enterkeyhint="search" data-sw-suggest={suggest ? '' : undefined} data-sw-locale={suggest ? route.locale : undefined} />
|
|
45
|
+
<button type="submit">Search</button>
|
|
46
|
+
</form>
|
|
47
|
+
{found.error ? (
|
|
48
|
+
<p role="alert">Search is not available right now. Please try again later.</p>
|
|
49
|
+
) : q && (
|
|
50
|
+
<>
|
|
51
|
+
<p role="status">{found.total === 1 ? '1 result' : `${found.total} results`} for “{q}”</p>
|
|
52
|
+
{found.results.length > 0 && (
|
|
53
|
+
<ol class="search-results">
|
|
54
|
+
{found.results.map((r) => (
|
|
55
|
+
<li>
|
|
56
|
+
<h2><a href={r.href}>{r.title}</a></h2>
|
|
57
|
+
{r.snippet.length > 0 && <p>{r.snippet.map((part) => (part.hit ? <mark>{part.text}</mark> : part.text))}</p>}
|
|
58
|
+
</li>
|
|
59
|
+
))}
|
|
60
|
+
</ol>
|
|
61
|
+
)}
|
|
62
|
+
{found.pages > 1 && (
|
|
63
|
+
<nav class="search-pager" aria-label="Result pages">
|
|
64
|
+
{pageNo > 1 && <a href={pageHref(pageNo - 1)} rel="prev">Previous</a>}
|
|
65
|
+
<span>Page {pageNo} of {found.pages}</span>
|
|
66
|
+
{pageNo < found.pages && <a href={pageHref(pageNo + 1)} rel="next">Next</a>}
|
|
67
|
+
</nav>
|
|
68
|
+
)}
|
|
69
|
+
</>
|
|
70
|
+
)}
|
|
71
|
+
</section>
|
|
20
72
|
) : (
|
|
21
73
|
<section class="not-found">
|
|
22
74
|
<h1>Page not found</h1>
|
|
@@ -0,0 +1,16 @@
|
|
|
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 type { APIRoute } from 'astro';
|
|
6
|
+
import { visitor } from '../../../lib/spillway';
|
|
7
|
+
|
|
8
|
+
export const GET: APIRoute = async ({ url }) => {
|
|
9
|
+
const q = (url.searchParams.get('q') ?? '').slice(0, 100);
|
|
10
|
+
const locale = url.searchParams.get('locale');
|
|
11
|
+
try {
|
|
12
|
+
return Response.json(await visitor.suggest(q, locale ? { locale } : {}), { headers: { 'cache-control': 'public, max-age=60' } });
|
|
13
|
+
} catch {
|
|
14
|
+
return Response.json({ q, items: [] });
|
|
15
|
+
}
|
|
16
|
+
};
|
|
@@ -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
|
+
}
|
package/templates/next/README.md
CHANGED
|
@@ -12,7 +12,7 @@ A Next.js (App Router) front end on a [Spillway CMS](https://spillwaycms.com) si
|
|
|
12
12
|
```
|
|
13
13
|
|
|
14
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.
|
|
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. If you gave `create-spillway` the address this front end will have (`--deploy-url`) and a token with `site.settings`, it added these rules for you; check them there.
|
|
16
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
17
|
|
|
18
18
|
## What is in it
|
|
@@ -21,13 +21,29 @@ A Next.js (App Router) front end on a [Spillway CMS](https://spillwaycms.com) si
|
|
|
21
21
|
| --- | --- |
|
|
22
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
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/
|
|
24
|
+
| `app/search/page.tsx` | The search page at `/search`, rendered for each request; `next.config.ts` rewrites `/es/search` and the other languages' search pages to it |
|
|
25
|
+
| `components/SearchPage.tsx`, `components/LanguageSwitcher.tsx` | The search page, and the language switcher |
|
|
26
|
+
| `app/api/search/suggest/route.ts`, `public/spillway-suggest.js` | Search suggestions while visitors type |
|
|
27
|
+
| `app/layout.tsx` | The document: the block stylesheet, the brand, search suggestions and, in a preview, the preview bar |
|
|
28
|
+
| `components/SiteShell.tsx` | The header and footer each page renders in its language: the site name, the `main` and `footer` menus (Structure → Menus), the language switcher |
|
|
25
29
|
| `app/api/revalidate/route.ts` | The webhook target: verifies `X-Spillway-Signature` and revalidates the paths and tags the event touched (`affectedByWebhook`) |
|
|
26
30
|
| `app/api/preview/route.ts`, `exit/route.ts` | Draft mode from a release share link or `?preview=1&secret=`, and back |
|
|
27
31
|
| `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 |
|
|
32
|
+
| `next.config.ts` | Rewrites `/api/forms/*` and `/media/*` to Spillway, so the Form block and media work from this origin, and each language's search page to `app/search` |
|
|
29
33
|
| `scripts/brand.mjs` | Writes the brand stylesheet (`app/spillway-brand.css`) with `@spillway-cms/brand`; runs before every build |
|
|
30
34
|
|
|
35
|
+
## Search
|
|
36
|
+
|
|
37
|
+
`/search` (and `/es/search` and so on, on a site with several languages) is the search page: the published pages that match, ten a page, read from Spillway's search without `SPILLWAY_TOKEN`, so drafts never show. Spillway's Search block sends visitors there. While visitors type, `public/spillway-suggest.js` suggests pages from this site's `/api/search/suggest`, unless suggestions are off under **Site settings → Search**.
|
|
38
|
+
|
|
39
|
+
## Languages
|
|
40
|
+
|
|
41
|
+
On a site with more than one language (**Site settings → Languages**), every language but the default lives under its prefix (`/es/about`), as on Spillway, and the header has a language switcher: each language links to the translation of the page, or to that language's home page when the page has none. The menus and the search page follow the page's language, which the header and the page carry as `lang`; `<html lang>` is the site's default language, since app/layout.tsx does not know the page. The links come from `languageLinks` in `@spillway-cms/client` (`lib/spillway.ts`).
|
|
42
|
+
|
|
43
|
+
## Members
|
|
44
|
+
|
|
45
|
+
Signing members in on this front end (sign up, log in, the account page and the pages only members see) is coming: Spillway will be an OpenID Connect provider, so the starter signs members in with the library its framework already uses. Until then, members-only pages answer 404 here; members read them on Spillway's own pages.
|
|
46
|
+
|
|
31
47
|
## Previews
|
|
32
48
|
|
|
33
49
|
- **A release share link**: `/api/preview?path=/about&release=<release id>&token=<share token>`. Spillway checks the token.
|
|
@@ -4,7 +4,8 @@ import { cache } from 'react';
|
|
|
4
4
|
import { SpillwayError, pathFromParams, spillwayStaticParams } from '@spillway-cms/next';
|
|
5
5
|
import { SpillwayBlocks, SpillwayJsonLd, fetchPage, metadataFor } from '@spillway-cms/next/page';
|
|
6
6
|
import type { PublicRender } from '@spillway-cms/client';
|
|
7
|
-
import {
|
|
7
|
+
import { SiteShell } from '../../components/SiteShell';
|
|
8
|
+
import { pageLanguages, redirectFor, spillway, withSiteUrls } from '../../lib/spillway';
|
|
8
9
|
|
|
9
10
|
type Props = { params: Promise<{ slug?: string[] }> };
|
|
10
11
|
|
|
@@ -25,7 +26,8 @@ const loadPage = cache(async (path: string): Promise<PublicRender | null> => {
|
|
|
25
26
|
try {
|
|
26
27
|
return await fetchPage(spillway, path);
|
|
27
28
|
} catch (err) {
|
|
28
|
-
|
|
29
|
+
// Not found, or members-only (signing members in on this front end is still to come; see README.md, Members).
|
|
30
|
+
if (err instanceof SpillwayError && (err.status === 404 || err.code === 'members_only')) return null;
|
|
29
31
|
throw err;
|
|
30
32
|
}
|
|
31
33
|
});
|
|
@@ -37,6 +39,7 @@ export async function generateMetadata({ params }: Props): Promise<Metadata> {
|
|
|
37
39
|
|
|
38
40
|
export default async function Page({ params }: Props) {
|
|
39
41
|
const path = pathFromParams(await params);
|
|
42
|
+
const site = await spillway.site().catch(() => null);
|
|
40
43
|
|
|
41
44
|
// Redirects in Spillway win over pages, as on a Spillway-rendered site. 301 becomes 308 and 302 becomes 307.
|
|
42
45
|
const moved = await redirectFor(path);
|
|
@@ -48,9 +51,9 @@ export default async function Page({ params }: Props) {
|
|
|
48
51
|
const page = await loadPage(path);
|
|
49
52
|
if (!page) notFound();
|
|
50
53
|
return (
|
|
51
|
-
|
|
54
|
+
<SiteShell locale={page.locale} menus={page.menus} languages={pageLanguages(site, page)}>
|
|
52
55
|
<SpillwayJsonLd page={page} />
|
|
53
56
|
<SpillwayBlocks page={page} />
|
|
54
|
-
|
|
57
|
+
</SiteShell>
|
|
55
58
|
);
|
|
56
59
|
}
|