preact-missing-hooks
Version:
A lightweight, extendable collection of missing React-like hooks for Preact — plus fresh, powerful new ones designed specifically for modern Preact apps.
72 lines (71 loc) • 2.93 kB
TypeScript
/** Open Graph / content type for og:type. */
export type OGType = "website" | "article" | "profile" | "video.other" | "product" | "music.song" | "book";
/** Config for the useLLMMetadata hook. All fields optional except route. */
export interface LLMConfig {
/** Current route path (e.g. "/blog/ai-hooks"). Changes trigger metadata update. */
route: string;
/** "manual" = use title/description/tags from config. "auto-extract" = derive from DOM. */
mode?: "manual" | "auto-extract";
/** Page title (manual mode). */
title?: string;
/** Page description (manual mode). */
description?: string;
/** Tags/keywords (manual mode). */
tags?: string[];
/** Canonical URL (absolute). */
canonicalUrl?: string;
/** Content language (e.g. "en", "en-US"). */
language?: string;
/** Open Graph type (website, article, etc.). */
ogType?: OGType;
/** OG image URL (absolute). */
ogImage?: string;
/** OG image alt text. */
ogImageAlt?: string;
/** Site name (e.g. for social previews). */
siteName?: string;
/** Author name (for articles). */
author?: string;
/** ISO date string (article publish). */
publishedTime?: string;
/** ISO date string (article last modified). */
modifiedTime?: string;
/** Robots hint (e.g. "index, follow"). */
robots?: string;
/** Extra key-value pairs (e.g. section, category). Keys/values are sanitized. */
extra?: Record<string, string | number | boolean | string[]>;
}
/** Payload injected as JSON in the LLM script tag. Only includes defined, safe values. */
export interface LLMPayload {
route: string;
title?: string;
description?: string;
tags?: string[];
outline?: string[];
canonicalUrl?: string;
language?: string;
ogType?: string;
ogImage?: string;
ogImageAlt?: string;
siteName?: string;
author?: string;
publishedTime?: string;
modifiedTime?: string;
robots?: string;
generatedAt: string;
extra?: Record<string, string | number | boolean | string[]>;
}
/**
* Production-ready hook: injects an AI-readable metadata block into the document head
* when the route changes. Framework-agnostic (React 18+ and Preact 10+ via aliasing).
*
* - Rich structure: route, title, description, tags, outline (auto), canonicalUrl, language,
* ogType, ogImage, ogImageAlt, siteName, author, publishedTime, modifiedTime, robots, extra.
* - Safe usage: never throws; invalid/missing config is normalized; DOM and JSON are guarded.
* - Cacheable: if the generated payload is unchanged, the script is not replaced.
* - SSR-safe: no-op when window/document is undefined.
* - Cleans up on unmount (removes the script).
*
* @param config - Route (required), mode, and optional metadata fields. Can be partial; defaults applied.
*/
export declare function useLLMMetadata(config: LLMConfig | null | undefined): void;