remix-utils-rt
Version:
This package contains simple utility functions to use with [React Router](https://reactrouter.com/home).
93 lines (92 loc) • 3.63 kB
TypeScript
import type { HandleConventionArguments } from "./handle-conventions.js";
export type ReferrerPolicy = "no-referrer-when-downgrade" | "no-referrer" | "origin-when-cross-origin" | "origin" | "same-origin" | "strict-origin-when-cross-origin" | "strict-origin" | "unsafe-url";
export type CrossOrigin = "anonymous" | "use-credentials";
export type ScriptType = "module" | "text/javascript";
export type ScriptDescriptor = {
/** Enable preloading of this script on SSR */
preload?: boolean;
/**
* @see https://developer.mozilla.org/en-US/docs/Web/HTML/Element/script#async
*/
async?: boolean;
/**
* @see https://developer.mozilla.org/en-US/docs/Web/HTML/Element/script#attr-crossorigin
*/
crossOrigin?: CrossOrigin;
/**
* @see https://developer.mozilla.org/en-US/docs/Web/HTML/Element/script#attr-defer
*/
defer?: boolean;
/**
* @see https://developer.mozilla.org/en-US/docs/Web/HTML/Element/script#attr-integrity
*/
integrity?: string;
/**
* @see https://developer.mozilla.org/en-US/docs/Web/HTML/Element/script#attr-nomodule
*/
noModule?: boolean;
/**
* @see https://developer.mozilla.org/en-US/docs/Web/HTML/Element/script#attr-nonce
*/
nonce?: string;
/**
* @see https://developer.mozilla.org/en-US/docs/Web/HTML/Element/script#attr-referrerpolicy
*/
referrerPolicy?: ReferrerPolicy;
/**
* @see https://developer.mozilla.org/en-US/docs/Web/HTML/Element/script#attr-src
*/
src: string;
/**
* @see https://developer.mozilla.org/en-US/docs/Web/HTML/Element/script#attr-type
*/
type?: ScriptType;
/**
* Optional element ID. Use only if the script element needs to be explicitly referenced later.
*/
id?: string;
};
export type ExternalScriptsFunction<Loader = unknown> = (args: HandleConventionArguments<Loader>) => ScriptDescriptor[];
/**
* Define the shape of the `handle` export if you want to use `scripts`. Combine
* it with your own `handle` type to add `scripts` to your route.
* @description Add a scripts function that access the route's loader data
* @example
* export const handle: ExternalScriptsHandle<SerializeFrom<typeof loader>> = {
* scripts(loaderData) { ... }
* }
* @description Add a static scripts array
* @example
* export const handle: ExternalScriptsHandle = {
* scripts: [...]
* }
* @description Extend it with your own handle type
* @example
* interface Handle<Data = unknown> extends ExternalScriptsHandle<Data> {
* // extra things here
* }
* export const handle: Handle = {
* scripts, // define scripts here
* // and any other handle properties here
* }
*/
export interface ExternalScriptsHandle<Data = unknown> {
scripts?: ExternalScriptsFunction<Data> | ScriptDescriptor[];
}
/**
* Load scripts defined by each route in a single place, often in `root`.
* @example
* // Defines a `scripts` function in a route `handle`
* export const handle: ExternalScriptsHandle<SerializeFrom<typeof loader>> = {
* scripts(loaderData) { ... }
* }
* // Or define a scripts array directly
* export const handle: ExternalScriptsHandle = {
* scripts: [...]
* }
* // Then render ExternalScripts in your root
* return <ExternalScripts />
*/
export declare function ExternalScripts(): import("react/jsx-runtime").JSX.Element;
export declare function useExternalScripts(): any[];
export declare function ExternalScript({ src, preload, async, defer, crossOrigin, integrity, type, referrerPolicy, noModule, nonce, id, }: ScriptDescriptor): import("react/jsx-runtime").JSX.Element | null;