Guides

Loading data

Bricks that show other documents, such as the latest posts or a product grid.

Some bricks show more than their own data: the newest posts, the products in a category. Give such a brick a load function. It receives the brick's data and a typed reader, and what it returns reaches the component as props.loaded.

export const latestPosts = defineBrick({
  name: 'latest-posts',
  label: 'Latest posts',
  schema: {
    heading: fields.text({ label: 'Heading', default: 'Latest posts' }),
    limit: fields.number({ label: 'How many', min: 1, max: 12, default: 3 }),
  },
  load: (data, content) => content.list(posts, { limit: data.limit }),
  component: (props) => (
    <section>
      <h2>{props.data.heading}</h2>
      <ul>
        <For each={props.loaded}>{(post) => <li><a href={`/posts/${post.slug}`}>{post.fields.title}</a></li>}</For>
      </ul>
    </section>
  ),
});

Write load before component, so props.loaded gets its type.

Where it runs

  • On the server, when the page renders, so the content is in the HTML. Pages that ship no JavaScript work too.
  • In the admin preview, in the browser. It reads saved content, and loads again after each save.

It reads from the ContentContext your app provides.

What makes it load again

Read the data fields your loader depends on before its first await. Changing those fields loads again, and changing others doesn't. While a new load is in flight, the previous result stays on screen.

A source can also offer track(): PageView calls it before each load, where reads are tracked, so reading a signal there makes every loader run again when it changes. The admin preview does this after saves, and this site does it to swap in your edits.