Skip to main content

Function: loadBlocks()

loadBlocks(registry, names): Promise<Record<string, BlockComponent>>

Defined in: loadBlocks.ts:53

Resolves named blocks out of a registry, in parallel, into a map from name to component. Renderer calls it with Object.keys(artifact.blockVersions) before it walks the tree; call it directly to warm a route, or to check a registry against an artifact ahead of serving one.

Only the named importers run — an importer for a name not asked for is never invoked, so a registry naming every block in the app costs a route only the blocks its own page uses.

Every name is checked before any importer runs, so a missing one loads nothing at all: the result is the whole set or a refusal, never a partial map. The refusal names every block the registry cannot satisfy, comma-separated in one message.

Parameters​

registry​

BlockRegistry

Where the importers come from. Keys it holds and names does not are neither read nor invoked, so passing the application's whole registry is the intended use.

names​

readonly string[]

The blocks to load, matched against the registry's keys exactly, casing included. A name repeated in the list loads its importer once; an empty list resolves to {} and invokes nothing.

Returns​

Promise<Record<string, BlockComponent>>

Name → the component that name's importer resolved to, holding exactly the names asked for and no others. The components come back typed BlockComponent, widened from the BlockComponent<never> a registry stores — sound at this seam because what reaches them at render are props compile already validated against that block's schema.

Throws​

Coded block-not-loaded when the registry has no importer for one or more of names. It is raised before any importer runs, so nothing has loaded when it lands.

Throws​

Whatever an importer raises. A dynamic import() that fails to resolve propagates unchanged, and one importer failing rejects the whole call.

Examples​

Load the blocks one artifact needs

import { loadBlocks } from "@nubbin/react";

const blocks = await loadBlocks(registry, Object.keys(artifact.blockVersions));
const Hero = blocks.Hero;
if (Hero !== undefined) {
await Hero({ title: "Summer sale" });
}

Every missing name in one message

await loadBlocks(registry, ["Ghost", "Hero", "Phantom"]);
// NubbinError: registry has no importer for: Ghost, Phantom