Pages
Error page
The site's error page: what shows when a page fails to render, the error head alone on Black (the title in one line, what happened in a plain sentence, the White Try again and the quiet way home), with no header or footer.
Install
React apps get the React files; every other stack gets the web component.
npx spyglass-ds add error-page<script type="module" src="/spyglass-ds/elements/error-page.js"></script>Something went wrong
This page did not load. Try again, or start from the homepage.
Usage
An app's or a website's error boundary (Next's error.tsx and global-error.tsx). Try again calls onRetry (React) or fires retry (web component).
Never
- An error code, a stack trace or a joke as the title
- More than one White button in a view
- The header and footer: the page that failed may be what draws them
sections lists the sections to show, in order (head); a key left out is not shown.
Adds with it: creative-card, page-head
Props
The web component takes the same as attributes or properties (<spyglass-error-page>); title is heading there.
<ErrorPage>
| Prop | Type | Default |
|---|---|---|
content | ErrorContent | |
onRetry | () => void | |
sections | readonly ErrorSection[] The sections to show, in order; all of them in the page order by default (leave one out to omit it). | |
className | string |
Examples
The same example in React and as web components; both render identically.
'use client'
import { ErrorPage } from '@/react/components/pages/ErrorPage'
// The error page as the site sets it: Try again would render the page again.
export default function Page() {
return <ErrorPage onRetry={() => {}} />
}<!-- The error page as the site sets it: Try again would render the page again. -->
<spyglass-error-page id="error-page"></spyglass-error-page>
<script type="module">
document.getElementById('error-page').addEventListener('retry', () => {})
</script>Source
import { ErrorHead } from '@/react/components/site/ErrorHead'
import { errorContent, type ErrorContent, errorSections, type ErrorSection } from '@/lib/pages/error'
import { cn } from '@/react/lib/cn'
import { pageSections } from '@/lib/image'
import { Fragment, type ReactNode } from 'react'
// The site's error page (app/error.tsx and global-error.tsx): what shows when a page fails to render, so neither the
// header nor the footer is there, only the error head on Black: the title in one line, the plain sentence, the White
// "Try again" (`onRetry`) and the quiet way home. As the page's last section it keeps the site's section padding under
// it (56px, 76 at 768, 84 at 1024), in a box that takes back what the page head keeps below. `content` replaces the
// copy (lib/pages/error.ts).
export interface ErrorPageProps {
content?: ErrorContent
onRetry?: () => void
/** The sections to show, in order; all of them in the page order by default (leave one out to omit it). */
sections?: readonly ErrorSection[]
className?: string
}
export function ErrorPage({ content: c = errorContent, onRetry, sections, className }: ErrorPageProps) {
const parts: Record<ErrorSection, ReactNode> = {
head: (
<div className="-mb-6 md:-mb-5 lg:-mb-3">
<ErrorHead content={c} onRetry={onRetry} />
</div>
),
}
return (
<div data-ds="error-page" className={cn('min-h-svh bg-surface text-fg', className)}>
<main id="main">
{pageSections(errorSections, sections).map((k) => <Fragment key={k}>{parts[k]}</Fragment>)}
</main>
</div>
)
}
export default ErrorPage// The error page's copy (site content/pages/errors.ts): the title, what happened in a plain sentence, try again and the
// way home. Both flavors read this file.
export const errorContent = {
lines: ['Something went wrong'],
lead: 'This page did not load. Try again, or start from the homepage.',
retry: 'Try again',
home: { label: 'Go to the homepage', href: '/' },
}
export type ErrorContent = typeof errorContent
/** The error page's sections in order; pass `sections` to reorder them or leave some out. */
export const errorSections = ['head'] as const
export type ErrorSection = (typeof errorSections)[number]// Joins class names, skipping falsy ones: cn('a', on && 'b', ['c', undefined]). It does not merge conflicting
// utilities (no tailwind-merge), as Vue's class binding does not: the library's classes are written not to compete,
// and a className from the caller is appended after them, as Vue appends a fallthrough class.
export type ClassValue = string | false | null | undefined | 0 | ClassValue[]
export function cn(...values: ClassValue[]): string {
const out: string[] = []
for (const v of values) {
if (!v) continue
if (Array.isArray(v)) {
const s = cn(...v)
if (s) out.push(s)
}
else out.push(v)
}
return out.join(' ')
}import { html } from 'lit'
import { DsElement, define, HostLayoutController } from '@/elements/lib/element'
import { errorContent, type ErrorContent, errorSections, type ErrorSection } from '@/lib/pages/error'
import '@/elements/components/site/error-head'
import styles from 'virtual:tw'
import { pageSections } from '@/lib/image'
// ErrorPage.tsx as <spyglass-error-page>: what shows when a page fails to render, the error head alone on Black (the
// title, the plain sentence, Try again, which fires `retry`, and the way home). `content` replaces the copy.
/**
* @summary The error page.
* @fires retry - When Try again is pressed.
*/
export class ErrorPage extends DsElement {
static styles = [DsElement.styles, styles]
protected readonly layout = new HostLayoutController(this, () => this.renderRoot.querySelector<HTMLElement>('[data-ds="error-page"]'))
static properties = { content: { type: Object }, sections: { type: Array } }
/** The page's copy (lib/pages/error.ts by default). */
content: ErrorContent = errorContent
/** The sections to show, in order (a list of names); all of them in the page order by default. */
sections?: ErrorSection[]
render() {
const c = this.content ?? errorContent
const parts: Record<ErrorSection, unknown> = {
head: html`<div class="-mb-6 md:-mb-5 lg:-mb-3"><spyglass-error-head .content=${c} @retry=${(e: Event) => { e.stopPropagation(); this.emit('retry') }}></spyglass-error-head></div>`,
}
return html`<div data-ds="error-page" class="min-h-svh bg-surface text-fg"><main id="main">${pageSections(errorSections, this.sections).map((k) => parts[k])}</main></div>`
}
}
define('error-page', ErrorPage)Reference: Site app/error.tsx, app/global-error.tsx, pages/PageHead/ErrorHead.tsx and content/pages/errors.ts: the page head without the drift, the White retry beside the quiet homepage link; main's last section keeps --section-y under it.