feat(site-builder): pre-publish checklist and canonical link

Publish now runs client-side checks (image alt, meta description, title
length, H1, empty links, contrast, noindex landing, empty page) over the
current state and shows a grouped checklist with Fix links and
"Publish anyway". Adds ExportOptions.canonicalUrl -> <link rel="canonical">
after the favicon line in both Preview and export.

Refs #54
This commit is contained in:
shadowdao committed 2026-10-07 12:05:28 -07:00
1 parent 5430f13877
commit c0fd183c11
12 files changed
+913 -13

No files matched your search

@@ -0,0 +1,107 @@
import { describe, test, expect, vi, afterEach } from 'vitest';
import React, { useEffect } from 'react';
import { renderEditorHarness, EditorHarness } from '../../test-utils/editorHarness';
import { PageProvider, usePages } from '../../state/PageContext';
import { PublishChecklistModal } from './PublishChecklistModal';
import { runPublishChecks } from '../../utils/publish-checks';
import { DEFAULT_SITE_DESIGN } from '../../state/SiteDesignContext';
/** Real <Editor>/<Frame> + the real PageProvider: Fix must switch the page via
* the real switchPage (deserialize on a timeout) and then select a node that
* only exists after that deserialize. */
const stateWith = (nodes: Record<string, any>) => JSON.stringify({
ROOT: { type: { resolvedName: 'Container' }, isCanvas: true, props: { style: {}, tag: 'div' }, displayName: 'Container', custom: {}, hidden: false, nodes: Object.keys(nodes), linkedNodes: {} },
...Object.fromEntries(Object.entries(nodes).map(([id, n]) => [id, { displayName: id, custom: {}, hidden: false, isCanvas: false, nodes: [], linkedNodes: {}, parent: 'ROOT', ...n }])),
});
const H1 = { type: { resolvedName: 'Heading' }, props: { level: 'h1', text: 'Hi', style: {} } };
const IMG = { type: { resolvedName: 'ImageBlock' }, props: { src: 'assets/a.png', alt: '' } };
const HOME = stateWith({ h: H1, homeImg: IMG });
const ABOUT = stateWith({ h2: H1, aboutImg: IMG });
const SEO = { metaDescription: 'd' };
const PAGES = [
{ id: 'home', name: 'Home', slug: 'index', craftState: HOME, seo: SEO },
{ id: 'about', name: 'About', slug: 'about', craftState: ABOUT, seo: SEO },
];
let h: EditorHarness | null = null;
afterEach(() => { h?.unmount(); h = null; });
const Seed: React.FC<{ onConfirm: () => void; onCancel: () => void }> = ({ onConfirm, onCancel }) => {
const { setPagesCraftState, pages, activePageId } = usePages();
const seeded = pages.length === 2;
useEffect(() => { if (!seeded) setPagesCraftState(PAGES); }, [seeded, setPagesCraftState]);
if (!seeded) return null;
const checks = runPublishChecks({ pages, design: DEFAULT_SITE_DESIGN });
return (
<>
<span data-testid="active">{activePageId}</span>
<PublishChecklistModal checks={checks} onConfirm={onConfirm} onCancel={onCancel} />
</>
);
};
const tick = async () => { await h!.act(async () => { await new Promise((r) => setTimeout(r, 30)); }); };
const q = (id: string, i = 0) => h!.container.ownerDocument.querySelectorAll<HTMLElement>(`[data-testid="${id}"]`)[i];
const click = async (el: HTMLElement) => { await h!.act(async () => { el.dispatchEvent(new MouseEvent('click', { bubbles: true })); }); };
const selected = () => h!.query.getEvent('selected').first();
function mount(onConfirm = vi.fn(), onCancel = vi.fn()) {
h = renderEditorHarness({ initialState: HOME });
h.mountChild(<PageProvider><Seed onConfirm={onConfirm} onCancel={onCancel} /></PageProvider>);
return { onConfirm, onCancel };
}
describe('PublishChecklistModal', () => {
test('groups items under their page names', async () => {
mount();
await tick();
const heads = Array.from(h!.container.ownerDocument.querySelectorAll('.publish-checklist-group h4')).map((e) => e.textContent);
expect(heads).toEqual(['Home', 'About']);
expect(h!.container.ownerDocument.querySelectorAll('[data-testid="publish-check"]')).toHaveLength(2);
});
test('Fix on the alt warning of ANOTHER page switches to it and selects the image node', async () => {
const { onCancel } = mount();
await tick();
expect(q('active').textContent).toBe('home');
const aboutFix = h!.container.ownerDocument.querySelector<HTMLElement>('[data-check-id="alt:about:aboutImg"] [data-testid="publish-check-fix"]')!;
await click(aboutFix);
await tick();
expect(q('active').textContent).toBe('about');
expect(h!.query.node('aboutImg').get()).toBeTruthy();
expect(selected()).toBe('aboutImg');
expect(onCancel).toHaveBeenCalled();
});
test('Fix on the active page selects immediately, without switching', async () => {
mount();
await tick();
const homeFix = h!.container.ownerDocument.querySelector<HTMLElement>('[data-check-id="alt:home:homeImg"] [data-testid="publish-check-fix"]')!;
await click(homeFix);
expect(q('active').textContent).toBe('home');
expect(selected()).toBe('homeImg');
});
test('Publish anyway calls onConfirm; Cancel calls onCancel', async () => {
const { onConfirm, onCancel } = mount();
await tick();
await click(q('publish-checklist-confirm'));
expect(onConfirm).toHaveBeenCalledTimes(1);
expect(onCancel).not.toHaveBeenCalled();
await click(q('publish-checklist-cancel'));
expect(onCancel).toHaveBeenCalledTimes(1);
});
test('items with no fix target (an empty page) render without a Fix button', async () => {
h = renderEditorHarness({ initialState: HOME });
h.mountChild(
<PageProvider>
<PublishChecklistModal checks={[{ id: 'empty:x', severity: 'warn', message: 'X is empty' }]} onConfirm={() => {}} onCancel={() => {}} />
</PageProvider>,
);
expect(h.container.ownerDocument.querySelectorAll('[data-testid="publish-check-fix"]')).toHaveLength(0);
expect(h.container.ownerDocument.body.textContent).toContain('X is empty');
});
});
@@ -0,0 +1,89 @@
import React, { useRef, useEffect } from 'react';
import { useEditor } from '@craftjs/core';
import { usePages } from '../../state/PageContext';
import type { Check } from '../../utils/publish-checks';
export interface PublishChecklistModalProps {
checks: Check[];
/** "Publish anyway": the caller publishes with the checks skipped. */
/** Backend has reported sitemap.xml / robots.txt generation on a past publish. */
showGeneratedNote?: boolean;
onConfirm: () => void;
onCancel: () => void;
}
/** Pre-publish checklist (#54). Every item is a warning or a note, so
* publishing is always one click away; "Fix" jumps to the page/node and
* closes the list so the customer can edit. */
export const PublishChecklistModal: React.FC<PublishChecklistModalProps> = ({ checks, showGeneratedNote, onConfirm, onCancel }) => {
const { actions } = useEditor();
const { pages, headerPage, footerPage, activePageId, switchPage } = usePages();
const timer = useRef<ReturnType<typeof setTimeout> | null>(null);
useEffect(() => () => { if (timer.current) clearTimeout(timer.current); }, []);
const nameOf = (pageId: string | undefined) => {
if (!pageId) return 'Site';
return [...pages, headerPage, footerPage].find((p) => p.id === pageId)?.name ?? 'Site';
};
const groups: { pageId: string | undefined; items: Check[] }[] = [];
for (const c of checks) {
const g = groups.find((x) => x.pageId === c.pageId);
if (g) g.items.push(c); else groups.push({ pageId: c.pageId, items: [c] });
}
const fix = (c: Check) => {
if (c.pageId && c.pageId !== activePageId) {
switchPage(c.pageId);
// switchPage deserializes the target on a setTimeout(0) (PageContext
// loadState); queued after it, this runs once the node exists.
if (c.nodeId) {
const nodeId = c.nodeId;
timer.current = setTimeout(() => { try { actions.selectNode(nodeId); } catch { /* node gone */ } }, 0);
}
} else if (c.nodeId) {
try { actions.selectNode(c.nodeId); } catch { /* node gone */ }
}
// Close so the modal doesn't cover the node about to be edited. Detach the
// pending select from the unmount cleanup above: it must still fire.
timer.current = null;
onCancel();
};
const canFix = (c: Check) => !!c.pageId && (c.fix === 'select' || c.fix === 'page-settings');
return (
<div className="conflict-backdrop" data-testid="publish-checklist">
<div className="conflict-modal publish-checklist" role="dialog" aria-modal="true" aria-labelledby="publish-checklist-title">
<h3 id="publish-checklist-title">Before you publish</h3>
<p>{checks.length === 1 ? '1 thing' : `${checks.length} things`} worth a look. None of them stop you from publishing.</p>
<div className="publish-checklist-body">
{groups.map((g) => (
<section key={g.pageId ?? 'site'} className="publish-checklist-group">
<h4>{nameOf(g.pageId)}</h4>
<ul>
{g.items.map((c) => (
<li key={c.id} className={`publish-check publish-check-${c.severity}`} data-testid="publish-check" data-check-id={c.id}>
<span className="publish-check-msg">{c.message}</span>
{canFix(c) && (
<button type="button" className="publish-check-fix" onClick={() => fix(c)} data-testid="publish-check-fix">
Fix
</button>
)}
</li>
))}
</ul>
</section>
))}
</div>
{showGeneratedNote && (
<p className="publish-checklist-note" data-testid="publish-checklist-generated">sitemap.xml and robots.txt will be generated.</p>
)}
<div className="conflict-modal-actions">
<button type="button" onClick={onCancel} data-testid="publish-checklist-cancel">Cancel</button>
<button type="button" onClick={onConfirm} data-testid="publish-checklist-confirm">Publish anyway</button>
</div>
</div>
</div>
);
};
+102 -1
View File
@@ -32,10 +32,23 @@ import { act } from 'react-dom/test-utils';
* needs more than that.
*/
// #54: what the pre-publish checks see. Default (no pages) means no checks, so
// every pre-existing test publishes directly.
const checkFixture: { pages: any[]; liveState: string; activePageId: string } = { pages: [], liveState: '{}', activePageId: 'home' };
vi.mock('../../state/PageContext', () => ({
usePages: () => ({
pages: checkFixture.pages,
headerPage: { id: '__header__', name: 'Header', slug: '__header__', craftState: null },
footerPage: { id: '__footer__', name: 'Footer', slug: '__footer__', craftState: null },
activePageId: checkFixture.activePageId,
switchPage: vi.fn(),
}),
}));
vi.mock('@craftjs/core', () => ({
useEditor: (collector?: (state: unknown, query: unknown) => Record<string, unknown>) => {
const query = {
serialize: () => '{}',
serialize: () => checkFixture.liveState,
history: { canUndo: () => false, canRedo: () => false },
};
const actions = { history: { undo: vi.fn(), redo: vi.fn() } };
@@ -114,6 +127,9 @@ afterEach(() => {
}
publishMock.mockReset();
vi.useRealTimers();
checkFixture.pages = [];
checkFixture.liveState = '{}';
checkFixture.activePageId = 'home';
});
describe('TopBar publish-warnings wiring', () => {
@@ -214,3 +230,88 @@ describe('TopBar device switcher reads/writes DeviceContext (W0-breakpoints)', (
expect(tablet.getAttribute('aria-pressed')).toBe('true');
});
});
/** #54: Publish runs the checklist first; the modal gates publish() until confirmed. */
describe('TopBar pre-publish checklist', () => {
const stateWith = (nodes: Record<string, any>) => JSON.stringify({
ROOT: { type: { resolvedName: 'Container' }, props: {}, nodes: Object.keys(nodes), linkedNodes: {}, parent: null },
...Object.fromEntries(Object.entries(nodes).map(([id, n]) => [id, { nodes: [], linkedNodes: {}, parent: 'ROOT', ...n }])),
});
const H1 = { type: { resolvedName: 'Heading' }, props: { level: 'h1', text: 'Hi', style: {} } };
const BAD_IMG = { type: { resolvedName: 'ImageBlock' }, props: { src: 'assets/a.png', alt: '' } };
const page = (craftState: string) => ({ id: 'p1', name: 'Home', slug: 'index', seo: { metaDescription: 'd' }, craftState });
const modal = () => container.querySelector('[data-testid="publish-checklist"]');
const q = (id: string) => container.querySelector<HTMLButtonElement>(`[data-testid="${id}"]`)!;
const click = async (el: Element) => {
await act(async () => {
el.dispatchEvent(new MouseEvent('click', { bubbles: true }));
await Promise.resolve();
await Promise.resolve();
});
};
test('with warnings: modal opens and publish() is NOT called until Publish anyway', async () => {
publishMock.mockResolvedValue({ success: true });
checkFixture.pages = [page(stateWith({ h: H1, img: BAD_IMG }))];
checkFixture.activePageId = 'other';
render();
await clickPublish();
expect(modal()).not.toBeNull();
expect(container.querySelectorAll('[data-testid="publish-check"]')).toHaveLength(1);
expect(publishMock).not.toHaveBeenCalled();
await click(q('publish-checklist-confirm'));
expect(publishMock).toHaveBeenCalledTimes(1);
expect(modal()).toBeNull();
});
test('Cancel closes the modal and never publishes', async () => {
checkFixture.pages = [page(stateWith({ h: H1, img: BAD_IMG }))];
checkFixture.activePageId = 'other';
render();
await clickPublish();
await click(q('publish-checklist-cancel'));
expect(modal()).toBeNull();
expect(publishMock).not.toHaveBeenCalled();
});
test('with nothing to flag: publishes directly, no modal', async () => {
publishMock.mockResolvedValue({ success: true });
checkFixture.pages = [page(stateWith({ h: H1 }))];
checkFixture.activePageId = 'other';
render();
await clickPublish();
expect(modal()).toBeNull();
expect(publishMock).toHaveBeenCalledTimes(1);
});
test('checks the LIVE canvas for the active page, not its stale stored state', async () => {
publishMock.mockResolvedValue({ success: true });
checkFixture.pages = [page(stateWith({ h: H1 }))]; // stored: clean
checkFixture.activePageId = 'p1';
checkFixture.liveState = stateWith({ h: H1, img: BAD_IMG }); // canvas: unsaved bad image
render();
await clickPublish();
expect(modal()).not.toBeNull();
expect(publishMock).not.toHaveBeenCalled();
});
test('a failed save after confirming still surfaces as a publish failure (gate does not swallow it)', async () => {
publishMock.mockResolvedValue({ success: false, failedStep: 'save', message: 'Publish cancelled: the save before publishing did not complete.' });
checkFixture.pages = [page(stateWith({ h: H1, img: BAD_IMG }))];
checkFixture.activePageId = 'other';
render();
await clickPublish();
await click(q('publish-checklist-confirm'));
expect(publishMock).toHaveBeenCalledTimes(1);
expect(container.textContent).toContain('did not complete');
});
test('response.generated adds an info line to the post-publish banner', async () => {
publishMock.mockResolvedValue({ success: true, generated: ['sitemap.xml', 'robots.txt'] });
render();
await clickPublish();
expect(container.querySelector('[data-testid="publish-warnings"]')!.textContent).toContain('Generated sitemap.xml and robots.txt.');
});
});
+59 -6
View File
@@ -16,6 +16,8 @@ import { PublishWarnings } from './PublishWarnings';
import { EmptySaveBanner } from './EmptySaveBanner';
import { ConflictModal } from './ConflictModal';
import { ErrorNotice } from './ErrorNotice';
import { PublishChecklistModal } from './PublishChecklistModal';
import { runPublishChecks, Check } from '../../utils/publish-checks';
import { SaveStatusPill, SaveStatus } from './SaveStatusPill';
import { useSaveGuard } from '../../state/SaveGuardContext';
import { SitesmithButton } from '../sitesmith/SitesmithButton';
@@ -53,6 +55,10 @@ export const TopBar: React.FC<TopBarProps> = ({ showGuides, onToggleGuides }) =>
const [errorNotice, setErrorNotice] = useState<string | null>(null);
const [publishWarnings, setPublishWarnings] = useState<string[]>([]);
const [isDraft, setIsDraft] = useState(false);
// Pre-publish checklist (#54): non-null while the modal is up.
const [pendingChecks, setPendingChecks] = useState<Check[] | null>(null);
// Set once a publish response reports sitemap/robots generation (backend support).
const [backendGenerates, setBackendGenerates] = useState(false);
// Mobile-A2: lifted from private useState into MobileChromeContext so
// opening a mobile sheet can close these modals (item 3) -- behavior is
// otherwise identical for both the desktop and mobile branches below.
@@ -143,8 +149,37 @@ export const TopBar: React.FC<TopBarProps> = ({ showGuides, onToggleGuides }) =>
const handleSave = runSave;
const handlePublish = useCallback(async () => {
/** Checks over the CURRENT state: the active page/zone is read live from the
* canvas, everything else from its stored craftState. Never throws -- a
* checker bug must not stand between a customer and Publish. */
const computeChecks = useCallback((): Check[] => {
try {
const live = query.serialize();
const pick = (id: string, stored: string | null) => (id === activePageId ? live : stored);
return runPublishChecks({
pages: pages.map((p) => ({ id: p.id, name: p.name, slug: p.slug, seo: p.seo, craftState: pick(p.id, p.craftState) })),
zones: [headerPage, footerPage].map((z) => ({ id: z.id, name: z.name, craftState: pick(z.id, z.craftState) })),
design,
});
} catch (e) {
console.warn('Publish checks failed:', e);
return [];
}
}, [query, pages, headerPage, footerPage, activePageId, design]);
// `opts` is read as `=== true` because this is also an onClick handler (the
// first argument is then a MouseEvent).
const handlePublish = useCallback(async (opts?: { skipChecks?: boolean }) => {
if (!canSave) return;
if (opts?.skipChecks !== true) {
if (pendingChecks) return;
const checks = computeChecks();
if (checks.length > 0) {
setPendingChecks(checks);
return;
}
}
setPendingChecks(null);
// Anything edited after this point is not in what we are about to publish.
const versionAtStart = getVersion();
publishingRef.current = true;
@@ -167,7 +202,10 @@ export const TopBar: React.FC<TopBarProps> = ({ showGuides, onToggleGuides }) =>
// The site published; these are fixable problems, not failures. Held
// independently of the status pill so a later edit doesn't take the
// warning down with it.
setPublishWarnings(Array.isArray(result.warnings) ? result.warnings : []);
const warnings = Array.isArray(result.warnings) ? result.warnings : [];
const generated = Array.isArray(result.generated) ? result.generated.filter((g): g is string => typeof g === 'string') : [];
setBackendGenerates(generated.includes('sitemap.xml'));
setPublishWarnings(generated.length ? [...warnings, `Generated ${generated.join(' and ')}.`] : warnings);
} else if (result) {
// The save before the publish failed: that is a save failure, and
// nothing went live.
@@ -181,7 +219,7 @@ export const TopBar: React.FC<TopBarProps> = ({ showGuides, onToggleGuides }) =>
publishingRef.current = false;
setPublishing(false);
}
}, [publish, canSave, getVersion, setEmptyRefusal]);
}, [publish, canSave, getVersion, setEmptyRefusal, pendingChecks, computeChecks]);
// "Keep mine": arm force_overwrite so it survives an empty-overwrite refusal
// followed by "Save anyway", then re-save. runSave closes the modal on success.
@@ -200,6 +238,15 @@ export const TopBar: React.FC<TopBarProps> = ({ showGuides, onToggleGuides }) =>
/>
) : null;
const checklistModal = pendingChecks ? (
<PublishChecklistModal
checks={pendingChecks}
showGeneratedNote={backendGenerates}
onConfirm={() => { void handlePublish({ skipChecks: true }); }}
onCancel={() => setPendingChecks(null)}
/>
) : null;
const emptyBanner = emptyRefusal ? (
<EmptySaveBanner
onSaveAnyway={() => { void handleSave({ forceEmpty: true }); }}
@@ -271,6 +318,10 @@ export const TopBar: React.FC<TopBarProps> = ({ showGuides, onToggleGuides }) =>
twitterCard: seo?.twitterCard,
noindex: seo?.noindex,
favicon: design.favicon,
// Same URL the backend emits at publish, so Preview head === published head.
canonicalUrl: isWHP && whpConfig?.siteDomain && activePage
? `https://${whpConfig.siteDomain}/${activePage.slug === 'index' ? '' : activePage.slug}`
: undefined,
design,
});
@@ -295,7 +346,7 @@ export const TopBar: React.FC<TopBarProps> = ({ showGuides, onToggleGuides }) =>
} catch (e) {
console.error('Preview failed:', e);
}
}, [query, headerPage, footerPage, whpConfig, design, pages, activePageId]);
}, [query, headerPage, footerPage, whpConfig, isWHP, design, pages, activePageId]);
// Site-level commands: the keyboard layer (Cmd+S / Cmd+Shift+P / Cmd+P), the
// "..." command menu and the shortcuts cheat sheet all read these. Re-registered
@@ -322,6 +373,7 @@ export const TopBar: React.FC<TopBarProps> = ({ showGuides, onToggleGuides }) =>
<PublishWarnings warnings={publishWarnings} onDismiss={() => setPublishWarnings([])} />
{emptyBanner}
{conflictModal}
{checklistModal}
<ErrorNotice message={errorNotice} onDismiss={() => setErrorNotice(null)} />
<div className="topbar-left">
{isWHP && (
@@ -369,7 +421,7 @@ export const TopBar: React.FC<TopBarProps> = ({ showGuides, onToggleGuides }) =>
{isWHP && (
<button
className="topbar-btn icon-only publish"
onClick={handlePublish}
onClick={() => { void handlePublish(); }}
disabled={publishing || saving || !canSave}
aria-label="Publish to live site"
data-tooltip="Publish"
@@ -415,6 +467,7 @@ export const TopBar: React.FC<TopBarProps> = ({ showGuides, onToggleGuides }) =>
<PublishWarnings warnings={publishWarnings} onDismiss={() => setPublishWarnings([])} />
{emptyBanner}
{conflictModal}
{checklistModal}
<ErrorNotice message={errorNotice} onDismiss={() => setErrorNotice(null)} />
<div className="topbar-left">
{isWHP && (
@@ -517,7 +570,7 @@ export const TopBar: React.FC<TopBarProps> = ({ showGuides, onToggleGuides }) =>
{isWHP && (
<button
className="topbar-btn publish"
onClick={handlePublish}
onClick={() => { void handlePublish(); }}
disabled={publishing || saving || !canSave}
title="Publish to live site"
aria-label="Publish to live site"