Remote datasources

A P1 page doesn't have to be built only from content stored in P1. Editors arrange blocks and write copy in the P1 editor. The same page can also show data that lives somewhere else, such as a product catalog, an events API or a headless CMS. P1 calls these external sources remote datasources. Your site's code defines a datasource once. After that, editors can use its data on any page without writing code, and every time the page renders, the server fetches current data from the remote source.

Listing remote content with the Data List block

The component library includes a Data List block for showing a collection of items. A Data List can display content managed in P1 or content from a remote datasource. Editors choose the source in the block's settings, then map the source's fields to the parts of each item: title, subtitle, teaser, image and icon.

The block also has a few settings for shaping the list:

  • Sort by a field, ascending or descending.

  • Filter to items whose chosen field contains some text.

  • Start at and Max items to show a fixed window of results, such as items 1–10.

These settings apply after the data has been fetched, so the datasource still returns the full result set.

The starter kit comes with an example datasource built on the Star Wars API: swapi_list returns a list of characters, and swapi returns a single character.

A listing of Star Wars characters in a P1 listing block

Referencing datasource fields with expressions

Any text field on any block can include an expression that refers to a datasource. Expressions take the form {​{ datasource.field }}:

{​{ swapi.name }} was born in {​{ swapi.birth_year }}

When the page renders, P1 runs the datasources that the page references and replaces each expression with the value it points to.

  • Missing values. If a datasource or field doesn't exist, the expression becomes an empty string. It doesn't cause an error.

  • Formatting functions. A small set of functions is available, including toUpperCase, default and truncate. For example: unknown.

  • Whole lists. When a field contains only a single expression and that expression points to a list, the field receives the list itself rather than text. For example, in an array field fills it with the list's items.

The editor suggests datasource fields as you type an expression.

Combining datasources with template paths

A datasource often needs to know which record to load: which character, which product, which event. On published pages, that information comes from the URL, through a template path.

A template path is a path with a colon segment, such as /star-wars-characters-by-id/:id. The page at a template path lays out every URL that matches it and has no page of its own:

URLid
/star-wars-characters-by-id/11 (Luke Skywalker)
/star-wars-characters-by-id/44 (Darth Vader)
/star-wars-characters-by-id/1010 (Obi-Wan Kenobi)

Here is an example of information about Luke Skywalker rendering at at /star-wars-characters-by-id/1.

A screenshot of the P1 editor showing information about Luke Skywalker

Declaring a remote datasource

You declare a datasource in your site's code, in two places that share the same id:

  1. A definition in the datasource registry. It tells the editor that the datasource exists and which fields it has.

  2. A fetcher. It loads the data when a page renders.

P1 doesn't look for these in any particular file. It uses whatever arrays you pass to two SDK functions:

WhereFunctionWhat to passWhat it's used for
app/p1/api/[...p1]/route.tscreateP1HandlerbuiltinDatasourceRegistry (definitions) and builtinFetchers (fetchers)The editor: autocomplete, Data List field pickers and previews
app/published-pages.tsxcreatePublishedPagefetchers (fetchers)Published pages

Pass your fetchers to both functions. If they reach only createP1Handler, previews work in the editor but published pages render empty values. Definitions are needed only by the editor.

By convention, the starter kit collects definitions in lib/remote-datasources.ts (REMOTE_DATASOURCE_REGISTRY) and fetchers in lib/remote-datasource-fetchers.ts (REMOTE_DATASOURCE_FETCHERS). Both arrays are already passed to the SDK, so the simplest way to add a datasource is to add an entry to each. You can organize your code differently. For example, you can export a datasource's definition and fetcher from one module so the two stay in sync, then assemble the arrays from those modules.

The definition

A definition is a RemoteDatasourceDefinition object, such as an entry in the starter kit's REMOTE_DATASOURCE_REGISTRY array. All of its properties are required.

PropertyDescription
idThe name editors use in expressions. For id: "swapi", editors write {​{ swapi.name }}. It must match the fetcher's id.
labelThe name shown in the editor, such as "Star Wars character detail".
descriptionA short explanation of what the data is.
resolutionAn explanation, for editors, of how the datasource decides what to load. For example: "Reads :id from the template path." P1 displays this text but doesn't act on it.
fieldsAn array of { path, description } objects, one for each field editors can use. Paths use dot notation. Use items[].name for fields of items in a list. These paths power expression autocomplete and the Data List field pickers.
// lib/remote-datasources.ts
import type { RemoteDatasourceDefinition } from "@pantheon-systems/puck-css/server";
export const REMOTE_DATASOURCE_REGISTRY: RemoteDatasourceDefinition[] = [
{
id: "swapi",
label: "Star Wars character detail",
description: "A single character from the Star Wars API.",
resolution: "Reads the character id from the template path, e.g. /characters/:id.",
fields: [
{ path: "name", description: "Character name" },
{ path: "birth_year", description: "Birth year" },
{ path: "homeworld", description: "URL of the homeworld resource" },
],
},
];

The fetcher

A fetcher is a RemoteDatasourceFetcher object, such as an entry in the starter kit's REMOTE_DATASOURCE_FETCHERS array. It has two properties:

PropertyDescription
idThe same id as the definition. The fetched data is made available under this name.
fetchAn async function that returns an object. Expressions read from this object, so {​{ swapi.name }} reads the name property.

P1 calls fetch with a single parameter object:

ParameterDescription
urlParamsThe values of the template path's colon segments, such as { id: "4" } for /characters/4 matching /characters/:id.
savedPreviewParamsValues an editor saved to preview the page at a template path in the editor.
searchParamsThe URL's query string. It is empty on published pages.
fetchImplThe fetch function to use for HTTP requests. Use it instead of the global fetch.
// lib/remote-datasource-fetchers.ts
import type { RemoteDatasourceFetcher } from "@pantheon-systems/puck-css/server";
export const REMOTE_DATASOURCE_FETCHERS: RemoteDatasourceFetcher[] = [
{
id: "swapi",
fetch: async ({ urlParams, savedPreviewParams, fetchImpl }) => {
const id = urlParams.id ?? savedPreviewParams.id;
if (!id) return {};
try {
const res = await fetchImpl(`https://swapi.info/api/people/${id}`);
return res.ok ? await res.json() : {};
} catch {
return {};
}
},
},
];

A few guidelines for fetchers:

  • Return {} when there's nothing to show. Don't throw. Expressions that point to an empty object render as empty text, so the rest of the page still renders.

  • Return lists as { items: [...] }. That way editors can use , and the Data List block can pick fields with paths like items[].name.

  • Keep the definition and the fetcher in sync. P1 doesn't check that the fetcher returns the fields the definition lists. If they drift apart, editors see fields in autocomplete that render empty.

  • Only referenced datasources run. P1 runs a page's datasources only when the page uses them in an expression, so an unused datasource doesn't slow pages down.