Under construction mode (developer tool)
Site Settings -> Under construction: a switch, 'Pages still available' and notice texts. When on, only the homepage and the chosen pages open (every language); other addresses redirect to the homepage with the notice (createPageRoute redirect option), and <ConstructionNotice> cancels clicks on links to blocked pages and shows the notice. getConstructionMode(locale). Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
@@ -0,0 +1,85 @@
|
||||
'use client'
|
||||
|
||||
import { useCallback, useEffect, useId, useRef, useState } from 'react'
|
||||
|
||||
import type { ConstructionMode } from './types.js'
|
||||
|
||||
import { isPathAllowed } from './logic.js'
|
||||
import { CONSTRUCTION_NOTICE_PARAM, CONSTRUCTION_NOTICE_VALUE } from './types.js'
|
||||
|
||||
export type ConstructionNoticeClassNames = {
|
||||
button?: string
|
||||
dialog?: string
|
||||
message?: string
|
||||
overlay?: string
|
||||
title?: string
|
||||
}
|
||||
|
||||
/**
|
||||
* The "site under construction" notice (Site Settings → Under construction).
|
||||
* Opens after a redirect from a blocked address (?notice=construction, then
|
||||
* removed from the URL) and when a visitor clicks a link to a page that isn't
|
||||
* available — the click is cancelled. Closes with its button or Escape.
|
||||
* Renders nothing while the mode is off.
|
||||
*
|
||||
* Unstyled (classNames, like CookieBanner); texts come from the panel.
|
||||
*
|
||||
* // [locale]/layout.tsx
|
||||
* <ConstructionNotice mode={await getConstructionMode(locale)} classNames={{ overlay: '…', dialog: '…' }} />
|
||||
*/
|
||||
export function ConstructionNotice({ classNames: cn = {}, mode }: { classNames?: ConstructionNoticeClassNames; mode: ConstructionMode }) {
|
||||
const [open, setOpen] = useState(false)
|
||||
const buttonRef = useRef<HTMLButtonElement>(null)
|
||||
const titleId = useId()
|
||||
const close = useCallback(() => setOpen(false), [])
|
||||
const { allowedPaths, enabled } = mode
|
||||
|
||||
useEffect(() => {
|
||||
if (!enabled) {return}
|
||||
// After a redirect: show the notice once and clean the address bar
|
||||
const url = new URL(window.location.href)
|
||||
if (url.searchParams.get(CONSTRUCTION_NOTICE_PARAM) === CONSTRUCTION_NOTICE_VALUE) {
|
||||
setOpen(true)
|
||||
url.searchParams.delete(CONSTRUCTION_NOTICE_PARAM)
|
||||
window.history.replaceState(window.history.state, '', `${url.pathname}${url.search}${url.hash}`)
|
||||
}
|
||||
|
||||
// Capture phase, before Next's <Link> — a cancelled click doesn't navigate
|
||||
const onClick = (event: MouseEvent) => {
|
||||
if (event.defaultPrevented || event.button !== 0 || event.metaKey || event.ctrlKey || event.shiftKey || event.altKey) {return}
|
||||
const anchor = event.target instanceof Element ? event.target.closest('a[href]') : null
|
||||
if (!(anchor instanceof HTMLAnchorElement) || anchor.hasAttribute('download')) {return}
|
||||
if (anchor.target && anchor.target !== '_self') {return}
|
||||
const target = new URL(anchor.href, window.location.href)
|
||||
if (target.origin !== window.location.origin || isPathAllowed(target.pathname, allowedPaths)) {return}
|
||||
event.preventDefault()
|
||||
event.stopPropagation()
|
||||
setOpen(true)
|
||||
}
|
||||
document.addEventListener('click', onClick, true)
|
||||
return () => document.removeEventListener('click', onClick, true)
|
||||
}, [enabled, allowedPaths])
|
||||
|
||||
useEffect(() => {
|
||||
if (!open) {return}
|
||||
buttonRef.current?.focus()
|
||||
const onKey = (event: KeyboardEvent) => {
|
||||
if (event.key === 'Escape') {close()}
|
||||
}
|
||||
document.addEventListener('keydown', onKey)
|
||||
return () => document.removeEventListener('keydown', onKey)
|
||||
}, [open, close])
|
||||
|
||||
if (!enabled || !open) {return null}
|
||||
return (
|
||||
<div className={cn.overlay} data-ipal="construction-notice">
|
||||
<div aria-labelledby={mode.texts.title ? titleId : undefined} aria-modal="true" className={cn.dialog} role="dialog">
|
||||
{mode.texts.title ? <p className={cn.title} id={titleId}>{mode.texts.title}</p> : null}
|
||||
{mode.texts.message ? <p className={cn.message}>{mode.texts.message}</p> : null}
|
||||
<button className={cn.button} onClick={close} ref={buttonRef} type="button">
|
||||
{mode.texts.button ?? '×'}
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
import type { Field } from 'payload'
|
||||
|
||||
const whenEnabled = (_: unknown, siblingData: { enabled?: boolean } | undefined) => Boolean(siblingData?.enabled)
|
||||
|
||||
/**
|
||||
* Site Settings → "Under construction" tab: a developer tool for a site that
|
||||
* isn't finished. When enabled, only the homepage and the chosen pages are
|
||||
* reachable — any other address redirects to the homepage with a notice, and a
|
||||
* link to another page shows the notice instead of navigating. One rule for
|
||||
* every visitor (pages stay static, there is no per-user check).
|
||||
*/
|
||||
export function buildConstructionFields(pagesSlug: string): Field[] {
|
||||
return [
|
||||
{
|
||||
name: 'construction',
|
||||
type: 'group',
|
||||
admin: { hideGutter: true },
|
||||
fields: [
|
||||
{
|
||||
name: 'enabled',
|
||||
type: 'checkbox',
|
||||
admin: {
|
||||
description:
|
||||
'Only the homepage and the pages below can be opened. Other addresses redirect to the homepage with the notice; links to other pages show it instead of navigating.',
|
||||
},
|
||||
defaultValue: false,
|
||||
label: 'Site under construction',
|
||||
},
|
||||
{
|
||||
name: 'allowedPages',
|
||||
type: 'relationship',
|
||||
admin: { condition: whenEnabled, description: 'The homepage is always available.' },
|
||||
hasMany: true,
|
||||
label: 'Pages still available',
|
||||
relationTo: pagesSlug as never,
|
||||
},
|
||||
{ name: 'title', type: 'text', admin: { condition: whenEnabled }, label: 'Notice title', localized: true },
|
||||
{ name: 'message', type: 'textarea', admin: { condition: whenEnabled }, label: 'Notice text', localized: true },
|
||||
{ name: 'buttonLabel', type: 'text', admin: { condition: whenEnabled }, label: 'Close button label', localized: true },
|
||||
],
|
||||
label: false,
|
||||
},
|
||||
]
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
import { CONSTRUCTION_NOTICE_PARAM, CONSTRUCTION_NOTICE_VALUE } from './types.js'
|
||||
|
||||
/** `/pl/` → `/pl`, `` → `/` — paths compare without a trailing slash. */
|
||||
export const normalizePath = (path: string): string => {
|
||||
const trimmed = path.replace(/\/+$/, '')
|
||||
return trimmed === '' ? '/' : trimmed
|
||||
}
|
||||
|
||||
/** Is this pathname one of the allowed ones? Exact match, trailing slash ignored. */
|
||||
export function isPathAllowed(pathname: string, allowedPaths: readonly string[]): boolean {
|
||||
const path = normalizePath(pathname)
|
||||
return allowedPaths.some((allowed) => normalizePath(allowed) === path)
|
||||
}
|
||||
|
||||
/** Where a blocked address goes: the homepage with the notice parameter. */
|
||||
export const constructionNoticeHref = (homePath: string): string =>
|
||||
`${homePath}?${CONSTRUCTION_NOTICE_PARAM}=${CONSTRUCTION_NOTICE_VALUE}`
|
||||
@@ -0,0 +1,17 @@
|
||||
/**
|
||||
* "Site under construction" (Site Settings → Under construction) as the
|
||||
* frontend needs it: getConstructionMode(locale) in createContentHelpers.
|
||||
*/
|
||||
export type ConstructionMode = {
|
||||
/** Paths a visitor may open, in every language: the homepage and "Pages still available". */
|
||||
allowedPaths: string[]
|
||||
enabled: boolean
|
||||
/** Homepage in the current language — blocked addresses redirect here (with the notice). */
|
||||
homePath: string
|
||||
/** Notice texts from the panel, in the current language. Empty = not set. */
|
||||
texts: { button?: string; message?: string; title?: string }
|
||||
}
|
||||
|
||||
/** Query parameter that opens the notice after a redirect: /pl?notice=construction */
|
||||
export const CONSTRUCTION_NOTICE_PARAM = 'notice'
|
||||
export const CONSTRUCTION_NOTICE_VALUE = 'construction'
|
||||
Reference in New Issue
Block a user