Client: no 'use client' on barrels, lazy lightbox viewer, rAF table of contents, Turnstile polling stops, reduced motion, Media eager, 404 placeholder option
Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
co-authored by
Claude Opus 5.5
parent
1d6108765c
commit
21073d98d0
@@ -1,4 +1,5 @@
|
||||
'use client'
|
||||
// No 'use client' on this barrel (1.18.0): every component behind it carries its own. A barrel with the
|
||||
// directive is one client boundary, so a page that imported one export shipped ALL of them (docs/performance.md).
|
||||
// Role predicates for client components (the main entry reaches 'server-only' modules and does not build
|
||||
// there with webpack). Concrete file, not the access barrel. On the server import them from the main entry.
|
||||
export { hasMinimumRole, isAdmin, isEditor } from '../modules/access/predicates.js'
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
'use client'
|
||||
// No 'use client' on this barrel (1.18.0): every component behind it carries its own. A barrel with the
|
||||
// directive is one client boundary, so a page that imported one export shipped ALL of them (docs/performance.md).
|
||||
export { AccessibilityProvider, useAccessibility } from './AccessibilityProvider.js'
|
||||
export { A11Y_DEFAULT_TEXTS, AccessibilityWidget } from './AccessibilityWidget.js'
|
||||
export type { A11yClassNames, A11yTexts } from './AccessibilityWidget.js'
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
'use client'
|
||||
// No 'use client' on this barrel (1.18.0): every component behind it carries its own. A barrel with the
|
||||
// directive is one client boundary, so a page that imported one export shipped ALL of them (docs/performance.md).
|
||||
export { Analytics } from './Analytics.js'
|
||||
export type { AnalyticsProps } from './Analytics.js'
|
||||
// Low-level pixel loaders — exported for manual conversion events if a project
|
||||
|
||||
@@ -12,6 +12,8 @@ export type MediaProps = {
|
||||
/** Override the document's alt text — `''` for a decorative image. */
|
||||
alt?: string
|
||||
className?: string
|
||||
/** Above the fold, not the LCP image (a logo): eager, without fetchPriority="high" or a preload. */
|
||||
eager?: boolean
|
||||
poster?: MediaValue
|
||||
/** Only the first visible image of the page (the LCP image). */
|
||||
priority?: boolean
|
||||
@@ -25,14 +27,14 @@ export type MediaProps = {
|
||||
* Every image or video of the library blocks. With `Image` (next/image, bound by createBlockRegistry)
|
||||
* images are optimized with blur-up; without it a plain <img> with width, height, lazy loading and focal point.
|
||||
*/
|
||||
export function Media({ alt, className, Image, poster, priority = false, resource, sizes = MEDIA_SIZES.full, videoMode = 'player' }: { Image?: ImageComponent } & MediaProps) {
|
||||
export function Media({ alt, className, eager = false, Image, poster, priority = false, resource, sizes = MEDIA_SIZES.full, videoMode = 'player' }: { Image?: ImageComponent } & MediaProps) {
|
||||
const video = videoProps(resource, { mode: videoMode, poster })
|
||||
if (video) {
|
||||
// Captions are not stored with media uploads; a project that needs them swaps Media (ui option).
|
||||
// eslint-disable-next-line jsx-a11y/media-has-caption
|
||||
return <video {...video} aria-hidden={videoMode === 'background' ? true : undefined} className={className} />
|
||||
}
|
||||
const img = mediaImageProps(resource, { priority, sizes, ...(alt !== undefined ? { alt } : {}) })
|
||||
const img = mediaImageProps(resource, { eager, priority, sizes, ...(alt !== undefined ? { alt } : {}) })
|
||||
if (!img) {return null}
|
||||
if (Image) {return <Image {...img} className={className} />}
|
||||
const { blurDataURL: _blur, placeholder: _placeholder, preload: _preload, ...plain } = img
|
||||
|
||||
@@ -136,7 +136,7 @@ const defaults: Required<CookieBannerClassNames> = {
|
||||
closeButton: `shrink-0 ${radius} p-1 ${mutedText} hover:bg-[var(--ipal-hover,light-dark(#f5f5f5,var(--ipal-hover-dark,#262626)))]`,
|
||||
content: 'flex flex-col gap-4 sm:flex-row sm:items-center sm:justify-between',
|
||||
control: 'mt-1 h-4 w-4 shrink-0 accent-[var(--ipal-primary,#2563eb)]',
|
||||
controlThumb: 'pointer-events-none inline-block size-5 rounded-full bg-white shadow transition-transform translate-x-0.5 data-[checked=true]:translate-x-[1.375rem]',
|
||||
controlThumb: 'pointer-events-none inline-block size-5 rounded-full bg-white shadow transition-transform motion-reduce:transition-none translate-x-0.5 data-[checked=true]:translate-x-[1.375rem]',
|
||||
cookie: `flex flex-col gap-1 border-t pt-2 first:border-t-0 first:pt-0 ${border}`,
|
||||
cookieList: 'mt-3 flex flex-col gap-2',
|
||||
cookieMeta: `grid grid-cols-[auto_1fr] gap-x-3 gap-y-0.5 text-xs ${mutedText}`,
|
||||
@@ -168,7 +168,7 @@ const defaults: Required<CookieBannerClassNames> = {
|
||||
const DETAILED_DIALOG = `relative max-h-[90vh] w-full max-w-4xl overflow-y-auto ${radius} border p-6 shadow-2xl sm:p-8 ${surface} ${border}`
|
||||
|
||||
const SWITCH_TRACK =
|
||||
'relative inline-flex h-6 w-11 shrink-0 cursor-pointer items-center rounded-full bg-[var(--ipal-border,#d4d4d4)] transition-colors data-[checked=true]:bg-[var(--ipal-primary,#2563eb)] disabled:cursor-default disabled:opacity-60 focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-[var(--ipal-primary,#2563eb)]'
|
||||
'relative inline-flex h-6 w-11 shrink-0 cursor-pointer items-center rounded-full bg-[var(--ipal-border,#d4d4d4)] transition-colors motion-reduce:transition-none data-[checked=true]:bg-[var(--ipal-primary,#2563eb)] disabled:cursor-default disabled:opacity-60 focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-[var(--ipal-primary,#2563eb)]'
|
||||
|
||||
type CookieBannerProps = {
|
||||
classNames?: CookieBannerClassNames
|
||||
@@ -358,7 +358,7 @@ export function CookieBanner({
|
||||
return (
|
||||
<details className={cn('cookies')} data-ipal="cookies">
|
||||
<summary className={cn('cookiesToggle')}>
|
||||
<svg aria-hidden="true" className="transition-transform group-open:rotate-90" fill="none" height="14" stroke="currentColor" strokeLinecap="round" strokeWidth="2" viewBox="0 0 24 24" width="14">
|
||||
<svg aria-hidden="true" className="transition-transform motion-reduce:transition-none group-open:rotate-90" fill="none" height="14" stroke="currentColor" strokeLinecap="round" strokeWidth="2" viewBox="0 0 24 24" width="14">
|
||||
<path d="m9 18 6-6-6-6" />
|
||||
</svg>
|
||||
{labels.cookies} ({rows.length})
|
||||
|
||||
@@ -79,7 +79,7 @@ export function CookieButton({ avoid, className, reserve = COOKIE_BUTTON_RESERVE
|
||||
|
||||
const cls =
|
||||
className ??
|
||||
'flex h-11 w-11 items-center justify-center rounded-full border shadow-md transition-[background-color,transform] ' +
|
||||
'flex h-11 w-11 items-center justify-center rounded-full border shadow-md transition-[background-color,transform] motion-reduce:transition-none ' +
|
||||
'bg-[var(--ipal-surface,light-dark(#fff,var(--ipal-surface-dark,#171717)))] border-[var(--ipal-border,light-dark(#e5e5e5,var(--ipal-border-dark,#262626)))] hover:bg-[var(--ipal-hover,light-dark(#f5f5f5,var(--ipal-hover-dark,#262626)))]'
|
||||
|
||||
const position: CSSProperties = className
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
'use client'
|
||||
// No 'use client' on this barrel (1.18.0): every component behind it carries its own. A barrel with the
|
||||
// directive is one client boundary, so a page that imported one export shipped ALL of them (docs/performance.md).
|
||||
export { ConsentProvider, useConsentContext } from './ConsentContext.js'
|
||||
export { ConsentScript } from './ConsentScript.js'
|
||||
export { readEffectiveConsent } from './consentStore.js'
|
||||
|
||||
@@ -174,6 +174,11 @@ export type NotFoundPageOptions =
|
||||
* carries the whole 404 page in every locale.
|
||||
*/
|
||||
load: (locale: string) => Promise<ReactNode>
|
||||
/**
|
||||
* What the lazy 404 shows until its content arrives (default nothing). A block as tall as the site's 404
|
||||
* content keeps the footer from moving when the content comes in (no layout shift, docs/performance.md).
|
||||
*/
|
||||
placeholder?: ReactNode
|
||||
}
|
||||
| ({ load?: undefined } & NotFoundViewOptions)
|
||||
|
||||
@@ -230,7 +235,7 @@ export function createNotFoundPage(helpers: NotFoundHelpers, options: NotFoundPa
|
||||
const locales = await localesOf(helpers)
|
||||
const defaultLocale = helpers.i18n?.defaultLocale ?? locales[0]
|
||||
if (options.load) {
|
||||
return <NotFoundLoader defaultLocale={defaultLocale} load={options.load} locales={locales} />
|
||||
return <NotFoundLoader defaultLocale={defaultLocale} load={options.load} locales={locales} placeholder={options.placeholder} />
|
||||
}
|
||||
const eager = options
|
||||
const views = Object.fromEntries(
|
||||
|
||||
@@ -2,14 +2,17 @@
|
||||
|
||||
import type { CSSProperties, ReactNode } from 'react'
|
||||
|
||||
import { createContext, use, useCallback, useId, useMemo, useRef, useState } from 'react'
|
||||
import { createContext, lazy, Suspense, use, useCallback, useId, useMemo, useRef, useState } from 'react'
|
||||
|
||||
import type { LightboxDialogProps } from './LightboxDialog.js'
|
||||
import type { LightboxClassNames, LightboxImage, LightboxRenderImage, LightboxTexts } from './types.js'
|
||||
|
||||
import { LightboxDialog } from './LightboxDialog.js'
|
||||
import { lightboxIndex, resolveLightboxTexts } from './texts.js'
|
||||
|
||||
// The viewer (dialog, swipe, keyboard, icons) is loaded the first time an image is opened, not with every page:
|
||||
// most visitors never open one. The triggers and this provider stay small.
|
||||
const LightboxDialog = lazy(() => import('./LightboxDialog.js').then((m) => ({ default: m.LightboxDialog })))
|
||||
|
||||
type Entry = { el: { current: HTMLElement | null }; group?: string; image: { current: LightboxImage } }
|
||||
|
||||
type OpenState = { images: readonly LightboxImage[]; index: number; returnFocus: HTMLElement | null }
|
||||
@@ -152,7 +155,7 @@ export function LightboxProvider({ children, classNames, icons, loop = true, ren
|
||||
<LightboxContext value={api}>
|
||||
<TriggerContext value={triggerApi}>
|
||||
{children}
|
||||
{dialogProps ? (renderDialog ? renderDialog(dialogProps) : <LightboxDialog {...dialogProps} />) : null}
|
||||
{dialogProps ? (renderDialog ? renderDialog(dialogProps) : <Suspense fallback={null}><LightboxDialog {...dialogProps} /></Suspense>) : null}
|
||||
</TriggerContext>
|
||||
</LightboxContext>
|
||||
)
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
'use client'
|
||||
// No 'use client' on this barrel (1.18.0): every component behind it carries its own. A barrel with the
|
||||
// directive is one client boundary, so a page that imported one export shipped ALL of them (docs/performance.md).
|
||||
export { LightboxDialog, prefersReducedMotion } from './LightboxDialog.js'
|
||||
export type { LightboxDialogProps } from './LightboxDialog.js'
|
||||
export { LightboxProvider, LightboxTrigger, useLightbox, useOptionalLightbox } from './LightboxProvider.js'
|
||||
|
||||
@@ -42,6 +42,11 @@ export const MEDIA_SIZES = {
|
||||
export type MediaImageOptions = {
|
||||
/** Alt override (decorative images: ''). Default: the document's alt. */
|
||||
alt?: string
|
||||
/**
|
||||
* Above the fold but not the LCP image (a header logo): loads at once (`loading="eager"`) WITHOUT
|
||||
* fetchPriority="high" or a preload, so it does not compete with the one `priority` image. Ignored with `priority`.
|
||||
*/
|
||||
eager?: boolean
|
||||
/**
|
||||
* Dominant colour as the <img> background while it loads. The style stays after
|
||||
* load, so it would show through transparent pixels (white logo on a black box).
|
||||
@@ -88,7 +93,7 @@ const asDoc = (resource: unknown): MediaDoc | null =>
|
||||
*/
|
||||
export function mediaImageProps(
|
||||
resource: unknown,
|
||||
{ alt, placeholderColor = 'auto', priority = false, sizes = '100vw' }: MediaImageOptions = {},
|
||||
{ alt, eager = false, placeholderColor = 'auto', priority = false, sizes = '100vw' }: MediaImageOptions = {},
|
||||
): MediaImageProps | null {
|
||||
const doc = asDoc(resource)
|
||||
if (!doc || doc.mimeType?.startsWith('video/')) {return null}
|
||||
@@ -109,7 +114,7 @@ export function mediaImageProps(
|
||||
width: doc.width,
|
||||
...(priority
|
||||
? { fetchPriority: 'high' as const, loading: 'eager' as const, preload: true }
|
||||
: { loading: 'lazy' as const }),
|
||||
: { loading: eager ? ('eager' as const) : ('lazy' as const) }),
|
||||
...(blur ? { blurDataURL: blur, placeholder: 'blur' as const } : {}),
|
||||
...(Object.keys(style).length ? { style } : {}),
|
||||
}
|
||||
|
||||
@@ -12,18 +12,16 @@ type NotFoundLoaderProps = {
|
||||
/** The project's server action: `(locale) => renderNotFoundView(content, options, locale)`. */
|
||||
load: (locale: string) => Promise<ReactNode>
|
||||
locales: string[]
|
||||
/**
|
||||
* Shown until the content arrives (default nothing). The 404 content comes after hydration, so the footer moves
|
||||
* when it arrives (a layout shift: measured CLS 0.32 on a phone for the reference site). A block as tall as the
|
||||
* site's own 404 content keeps the footer still: `placeholder={<div style={{ minHeight: '36rem' }} />}`. A fixed
|
||||
* one-screen block is no fix in general: shorter content then pulls the footer UP into view (measured 0.36).
|
||||
*/
|
||||
placeholder?: ReactNode
|
||||
}
|
||||
|
||||
/**
|
||||
* The 404 content, fetched only when the 404 is actually shown.
|
||||
*
|
||||
* Next puts the `not-found` tree of a layout into the RSC payload of EVERY page
|
||||
* under it (the router needs it for a client-side 404). A 404 page with real
|
||||
* content (often the homepage) rendered there in every locale multiplied the
|
||||
* size of each page. Here only this small component is in the payload; the
|
||||
* content comes from a server action on mount.
|
||||
*/
|
||||
export function NotFoundLoader({ defaultLocale, load, locales }: NotFoundLoaderProps) {
|
||||
export function NotFoundLoader({ defaultLocale, load, locales, placeholder = null }: NotFoundLoaderProps) {
|
||||
const pathname = usePathname() ?? ''
|
||||
const locale = localeFromPath(pathname, locales, defaultLocale)
|
||||
const [view, setView] = useState<{ locale: string; node: ReactNode } | null>(null)
|
||||
@@ -43,5 +41,5 @@ export function NotFoundLoader({ defaultLocale, load, locales }: NotFoundLoaderP
|
||||
}
|
||||
}, [load, locale])
|
||||
|
||||
return <>{view?.locale === locale ? view.node : null}</>
|
||||
return <>{view?.locale === locale ? view.node : placeholder}</>
|
||||
}
|
||||
|
||||
@@ -49,11 +49,21 @@ export function TableOfContents({ classNames: cn = {}, items, label, offset = 0.
|
||||
setActive(current)
|
||||
}
|
||||
update()
|
||||
window.addEventListener('scroll', update, { passive: true })
|
||||
window.addEventListener('resize', update)
|
||||
// At most one measurement per frame: a scroll fires many events between two paints, and each one read the
|
||||
// position of every heading (forced layout on the main thread, which delays the next tap — INP).
|
||||
let frame = 0
|
||||
const schedule = () => {
|
||||
if (!frame) {frame = requestAnimationFrame(() => {
|
||||
frame = 0
|
||||
update()
|
||||
})}
|
||||
}
|
||||
window.addEventListener('scroll', schedule, { passive: true })
|
||||
window.addEventListener('resize', schedule)
|
||||
return () => {
|
||||
window.removeEventListener('scroll', update)
|
||||
window.removeEventListener('resize', update)
|
||||
cancelAnimationFrame(frame)
|
||||
window.removeEventListener('scroll', schedule)
|
||||
window.removeEventListener('resize', schedule)
|
||||
}
|
||||
}, [items, offset])
|
||||
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
'use client'
|
||||
// No 'use client' on this barrel (1.18.0): every component behind it carries its own. A barrel with the
|
||||
// directive is one client boundary, so a page that imported one export shipped ALL of them (docs/performance.md).
|
||||
export { IAPL_VISITOR_COOKIE, PageStats, VISITOR_COOKIE } from './PageStats.js'
|
||||
export type { PageStatsProps } from './PageStats.js'
|
||||
|
||||
@@ -85,7 +85,11 @@ export function Turnstile({ onToken, resetSignal = 0, siteKey, theme = 'page' }:
|
||||
ensureScript()
|
||||
|
||||
function render() {
|
||||
if (!ref.current || !window.turnstile || widgetId.current) {return}
|
||||
if (widgetId.current) {
|
||||
clearInterval(interval) // rendered: stop polling (it used to tick every 300 ms for the life of the form)
|
||||
return
|
||||
}
|
||||
if (!ref.current || !window.turnstile) {return}
|
||||
widgetId.current = window.turnstile.render(ref.current, {
|
||||
callback: (token: string) => onToken(token),
|
||||
'error-callback': () => onToken(null),
|
||||
@@ -95,9 +99,9 @@ export function Turnstile({ onToken, resetSignal = 0, siteKey, theme = 'page' }:
|
||||
})
|
||||
}
|
||||
|
||||
render()
|
||||
// Script may load after mount — retry briefly until ready.
|
||||
// Script may load after mount — retry until it is ready, then stop.
|
||||
const interval = setInterval(render, 300)
|
||||
render()
|
||||
return () => clearInterval(interval)
|
||||
}, [siteKey, widgetTheme, onToken])
|
||||
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
'use client'
|
||||
// No 'use client' on this barrel (1.18.0): every component behind it carries its own. A barrel with the
|
||||
// directive is one client boundary, so a page that imported one export shipped ALL of them (docs/performance.md).
|
||||
// Client-only exports — the Turnstile widget. Kept separate from index.ts so
|
||||
// the server-only verify never leaks into a browser bundle.
|
||||
export { Turnstile } from './Turnstile.js'
|
||||
|
||||
Reference in New Issue
Block a user