if ( globalThis.SCRIPT_DEBUG ) { await import( 'preact/debug' ); } import { h, cloneElement, render } from 'preact'; import { batch } from '@preact/signals'; import './directives'; // Registers all the core directives. import { routerRegions } from './directives/router-region'; import { initialVdomPromise, hydrateRegions, getRegionRootFragment, } from './hydration'; import { getInteractiveNamespace, parseDirectiveName, parseDirectiveValue, toVdom, } from './vdom'; import { directive } from './hooks'; import { getNamespace } from './namespaces'; import { getScope, runWithoutScope } from './scopes'; import { parseServerData, populateServerData } from './store'; import { proxifyState } from './proxies'; import { afterNextFrame, deepReadOnly, navigationSignal, onDOMReady, sessionId, warn, } from './utils'; export { store, getConfig, getServerState, type AsyncAction, type TypeYield, } from './store'; export { getContext, getServerContext, getElement } from './scopes'; export { asDangerousHTML, type DangerousHTML } from './html'; export { withScope, useWatch, useInit, useEffect, useLayoutEffect, useCallback, useMemo, splitTask, withSyncEvent, watch, } from './utils'; export { useState, useRef } from 'preact/hooks'; const requiredConsent = 'I acknowledge that using private APIs means my theme or plugin will inevitably break in the next version of WordPress.'; /** * Returns the consent-gated private APIs used by internal consumers. * * @param lock Required acknowledgment of private API use. * @return The private interactivity APIs. */ export const privateApis = ( lock: 'I acknowledge that using private APIs means my theme or plugin will inevitably break in the next version of WordPress.' ) => { if ( lock === requiredConsent ) { return { getRegionRootFragment, initialVdomPromise, toVdom, // Parses directive attribute names for internal consumers. parseDirectiveName, // Shares the directive-value interpretation with internal consumers. parseDirectiveValue, // Shares the rules that turn a `data-wp-interactive` value into a // namespace with internal consumers. getInteractiveNamespace, directive, getNamespace, h, cloneElement, render, proxifyState, parseServerData, populateServerData, batch, routerRegions, deepReadOnly, navigationSignal, sessionId, warn, afterNextFrame, getScope, /** Runs a callback without a directive scope and restores the caller's stack. */ runWithoutScope, }; } throw new Error( 'Forbidden access.' ); }; // Parses and populates the initial state and config. populateServerData( parseServerData() ); // Hydrates all interactive regions when `DOMContentLoaded` is dispatched, or as // soon as the `@wordpress/interactivity` module is evaluated in the case that // the event was already dispatched. This ensures synchronous modules had the // opportunity to register their stores before hydration takes place. For // asynchronous modules, or modules importing this module asynchronously, this // cannot be guaranteed. onDOMReady( hydrateRegions ); // Tag the current history entry with the session ID so that, within the same // session, all entries share the same ID and back/forward works normally. window.history.replaceState( { ...window.history.state, wpInteractivityId: sessionId }, '' ); // When the browser fires `popstate` for a history entry that was created in a // different session (i.e., before a full page reload), force a reload so the // server can render the correct content. Without this, the URL would change but // the page content would remain stale because the interactivity router — which // handles client-side navigations — might not be loaded yet. // // Some `popstate` events (e.g., anchor/fragment navigations like // clicking ``) have `null` state. These are // same-document navigations and must NOT trigger a reload — the browser // should just scroll to the target element as normal. window.addEventListener( 'popstate', ( event ) => { if ( event.state !== null && event.state?.wpInteractivityId !== sessionId ) { window.location.reload(); } } );