skills/canvas-data-fetching/SKILL.md
Fetch and render Drupal content in Canvas components with JSON:API and SWR patterns. Use when building content lists, integrating with SWR, querying related entities, or constructing/changing any JSON:API request — every generated request must be executed and verified to return the expected results before rendering logic is written against it. Covers JsonApiClient, DrupalJsonApiParams, relationship handling, filter patterns, and request verification.
npx skillsauth add drupal-canvas/skills canvas-data-fetchingInstall this skill globally with one command. Works with Claude Code, Cursor, and Windsurf.
3 of 9 scanners reported clean
Some scanners were skipped, did not run, or reported a non-clean status. Review each row below.
Use SWR for all data fetching. It provides caching, revalidation, and a clean hook-based API.
import useSWR from 'swr';
const fetcher = (url) => fetch(url).then((res) => res.json());
export default function Profile() {
const { data, error, isLoading } = useSWR(
'https://my-site.com/api/user',
fetcher,
);
if (error) return <div>Failed to load</div>;
if (isLoading) return <div>Loading...</div>;
return <div>Hello, {data.name}!</div>;
}
To fetch content from Drupal (e.g., articles, events, or other content types),
use the autoconfigured JsonApiClient from the drupal-canvas package combined
with DrupalJsonApiParams for query building.
Important: Keep the default serializer enabled in final component code. The
runtime contract for Canvas components is the deserialized shape returned by
JsonApiClient, not the raw JSON:API document shape.
Important: Do not fabricate JSON:API resource payloads in Workbench mocks. Components that fetch data should render their real loading, empty, or error states in Workbench unless the user explicitly asks for a static, non-fetching preview shape.
Do not write component logic from raw JSON:API assumptions such as
data[0].attributes.title or data[0].relationships.field_image. Components
using JsonApiClient receive deserialized objects instead.
JsonApiClient to inspect the actual shape your component will consume.Any JSON:API request you generate — for a new component, a refactor, a filter change, an added include, a changed sort, or a new query for an existing component — must be executed and verified before any rendering logic is written or changed against it. Do not assume a query is correct because it "looks right". Build the query, run it, and confirm the response matches expectations.
A request is verified only after all of these checks pass:
null.addFields. Missing or consistently null
fields mean the query, the field name, or the content type is wrong.addInclude, confirm the relationship is hydrated on the deserialized
object the component will read.If any check fails, fix the query, the field names, or the content-type assumptions — not the component. Do not paper over an empty or wrong response with optional chaining, fallback strings, or "looks fine in the UI" reasoning. Re-run the probe after each fix and only proceed once the response matches expectations.
Use the probe pattern in the next section as the default mechanism for these
checks. A probe that prints count: 0, keys: [], or a shape missing the
fields the component needs is a failed verification, not a green light.
Before writing rendering logic, run a one-off JavaScript probe that uses the
same JsonApiClient call and DrupalJsonApiParams query pattern the component
will use. Inspect the first returned item and write the component against that
deserialized shape.
This probe runs outside the Canvas runtime, so it must provide baseUrl and
apiPrefix explicitly. Final component code should not copy that setup;
Canvas-provided component code should use the normal autoconfigured
new JsonApiClient() path instead.
Use a command in this pattern:
node --input-type=module -e "
globalThis.window = {};
import { JsonApiClient } from 'drupal-canvas';
import { DrupalJsonApiParams } from 'drupal-jsonapi-params';
const describeShape = (value) => {
if (Array.isArray(value)) {
return value.length > 0 ? [describeShape(value[0])] : ['empty-array'];
}
if (value && typeof value === 'object') {
return Object.fromEntries(
Object.entries(value).map(([key, nestedValue]) => [key, describeShape(nestedValue)]),
);
}
if (value === null) {
return 'null';
}
return typeof value;
};
const client = new JsonApiClient('https://example.ddev.site', {
apiPrefix: 'jsonapi',
});
const queryString = new DrupalJsonApiParams()
.addSort('created', 'DESC')
.addFields('node--article', ['title', 'created', 'body', 'path'])
.getQueryString();
const items = await client.getCollection('node--article', { queryString });
const first = items?.[0];
console.log('count:', items?.length ?? 0);
console.log('keys:', first ? Object.keys(first) : []);
console.log('shape:', JSON.stringify(first ? describeShape(first) : null, null, 2));
console.log(JSON.stringify(first, null, 2));
"
Pass the site root as baseUrl, not the /jsonapi endpoint. Adjust the
resource type, filters, includes, sorts, and fields to match the component you
are building. Do not inspect one query shape and implement a different one in
the component.
If this probe fails in a local HTTPS development environment, check whether Node trusts the local certificate chain before assuming the JSON:API client or query is wrong.
import { getNodePath, JsonApiClient } from 'drupal-canvas';
import { DrupalJsonApiParams } from 'drupal-jsonapi-params';
import useSWR from 'swr';
const Articles = () => {
const client = new JsonApiClient();
const { data, error, isLoading } = useSWR(
[
'node--article',
{
queryString: new DrupalJsonApiParams()
.addSort('created', 'DESC')
.getQueryString(),
},
],
([type, options]) => client.getCollection(type, options),
);
if (error) return 'An error has occurred.';
if (isLoading) return 'Loading...';
return (
<ul>
{data.map((article) => (
<li key={article.id}>
<a href={getNodePath(article)}>{article.title}</a>
</li>
))}
</ul>
);
};
export default Articles;
addIncludeWhen you need related entities (e.g., images, taxonomy terms), use addInclude
to fetch them in a single request.
Avoid circular references in JSON:API responses. SWR uses deep equality checks to compare cached data, which fails with "too much recursion" errors when the response contains circular references.
Do not include self-referential fields. Fields that reference the same
entity type being queried (e.g., field_related_articles on an article query)
create circular references: Article A references Article B, which references
back to Article A. If you need related content of the same type, fetch it in a
separate query.
Use addFields to limit the response. Always specify only the fields you
need. This improves performance and helps avoid circular reference issues:
const params = new DrupalJsonApiParams();
params.addSort('created', 'DESC');
params.addInclude(['field_category', 'field_image']);
// Limit fields for each entity type
params.addFields('node--article', [
'title',
'created',
'field_category',
'field_image',
]);
params.addFields('taxonomy_term--categories', ['name']);
params.addFields('file--file', ['uri', 'url']);
When building a component that displays a list of content items (e.g., a news listing, event calendar, or resource library), follow this workflow:
Before any JSON:API discovery or content-type checks, verify local setup:
.env in the project root~/.canvasrcCANVAS_SITE_URL.CANVAS_JSONAPI_PREFIX. If it is not set, default to
jsonapi.CANVAS_SITE_URL=<resolved site root>CANVAS_JSONAPI_PREFIX=<resolved prefix>CANVAS_SITE_URL is the site root, not the JSON:API endpoint.
For example, use https://example.ddev.site, not
https://example.ddev.site/jsonapi.{CANVAS_SITE_URL}/{resolved JSON:API prefix}.
Success means HTTP 200.CANVAS_SITE_URL=<their Drupal site root>CANVAS_JSONAPI_PREFIX=jsonapi (optional; defaults to jsonapi) Then wait
for the user to confirm they updated shell env, .env, or ~/.canvasrc,
and resolve the values again before retrying the request.CANVAS_SITE_URL and JSON:API prefix are known.vite.config.*) to troubleshoot connectivity.
Connectivity issues must be resolved via correct config values and Drupal
site availability, not build tooling changes.Examine the design to understand what data each list item needs:
Before writing code, verify that an appropriate content type exists in Drupal:
Check the JSON:API endpoint of your local Drupal site (configured via the
resolved CANVAS_SITE_URL and JSON:API prefix from the Setup gate) to find a
content type that matches the required structure. A plain HTTP request is
acceptable for endpoint discovery only, after passing the Setup gate.
If a matching content type exists, use it and note which fields are available.
Inspect a sample response through JsonApiClient using the same resource
type and query pattern the component will use. Run a one-off probe command,
inspect the first returned item, and verify the deserialized field shape
before writing rendering logic.
If no matching content type exists, stop and prompt the user to create one. Provide:
Create the content list component using JSON:API to fetch content. Only use
fields that actually exist on the content type and on the deserialized objects
returned by JsonApiClient—do not assume raw JSON:API field nesting will match
the runtime data shape.
If the list includes filters based on entity reference fields (e.g., filter by category, filter by author):
This ensures filters stay in sync with the actual content in Drupal and new options appear automatically without code changes.
Components like headers, footers, and sidebars often need menu links from Drupal. Use a dual implementation: fetch from a Drupal menu when one exists, and fall back to a static array when no menu is configured yet.
This means the component works immediately (using the hardcoded fallback), and automatically upgrades to live Drupal-managed links once the CMS editor creates the corresponding menu.
import { JsonApiClient, sortMenu } from 'drupal-canvas';
import useSWR from 'swr';
// Static fallback — always define this; it renders when no Drupal menu exists
const FALLBACK_LINKS = [
{ id: 'home', title: 'Home', url: '/' },
{ id: 'about', title: 'About', url: '/about' },
];
const client = new JsonApiClient();
const Navigation = ({ menuName = 'main' }) => {
const { data, error, isLoading } = useSWR(
menuName ? ['menu_items', menuName] : null,
([type, id]) => client.getResource(type, id),
);
// Use live Drupal menu links when available; otherwise use fallback
const links =
!error && !isLoading && data ? Array.from(sortMenu(data)) : FALLBACK_LINKS;
return (
<nav>
{links.map(({ id, title, url }) => (
<a key={id} href={url}>
{title}
</a>
))}
</nav>
);
};
Rules for menu components:
FALLBACK_LINKS constant with representative links. This
makes the component useful in Workbench and on sites where the Drupal menu
hasn't been created yet.menuName as a prop and register it in component.yml so CMS editors
can configure which Drupal menu to use without code changes.menuName = null disables fetching (SWR key is null) and renders the
fallback — useful for pure static previews./admin/structure/menu/add.component.yml example for menuName:
props:
properties:
menuName:
title: Menu name
type: string
examples:
- main
- footer
testing
Use for any task touching site chrome — header, footer, sidebar, or global navigation that repeats across pages — and for any region-spec work (create, modify, review, validate region JSON, or the project-level layout component). Also load when a task creates or edits multiple pages that share chrome, or asks for a "site" or "navigation between pages"; shared chrome belongs in regions, never inlined into page JSON.
content-media
Create and modify content templates that map Drupal content entities to Canvas component layouts. Use when asked to (1) Create a new content template, (2) Modify an existing content template, (3) Add or change entity field mappings in a template, (4) Compose components in a content template via slots. Content templates live in the configured `contentTemplatesDir` (default `content-templates/`) and define how Drupal entity types, bundles, and view modes render as Canvas component trees.
tools
Plans and builds Drupal Canvas navigation UI (main nav, footer links, sidebar nav, mobile drawers, breadcrumbs) using design decomposition for structure and props/slots, then JSON:API menu or page-context patterns from canvas-data-fetching. Use when the user asks for navigation, header or footer links, menus, menu_items, mobile nav, or breadcrumb trails. Run after canvas-design-decomposition for layout and API sketches; follow canvas-data-fetching for SWR, JsonApiClient, sortMenu, and menu fallbacks.
development
Plans structure for a component library with props/slots and right-sized component granularity. Run before building or adding Canvas components (new `src/components/` folders, component.yml, React), or for plan-only / breakdown-only work, whenever UI must map to a coherent tree. Mandatory for every new Figma frame or greenfield screen—repository drafts do not replace phases A–G.