# Add Featured Images to a Gatsby Blog

Yes. An agent adds a featuredImage frontmatter field, renders it with gatsby-plugin-image on posts and the index, sets og:image for sharing and opens a PR.

[Run this on Rush](https://cloud.getrush.ai/run/add-featured-images/gatsby/). Opens the console with this prompt ready to send, in a Project that has your repository.

## What the agent does

The agent reads how posts are sourced and rendered before it adds an image. In a blog shaped like gatsbyjs/gatsby-starter-blog, `package.json` pins `gatsby` ^5.14.6 with `gatsby-plugin-image`, `gatsby-plugin-sharp`, `gatsby-transformer-sharp`, `gatsby-source-filesystem` for `content/blog` and `src/images`, `gatsby-transformer-remark` with `gatsby-remark-images` at `maxWidth` 630, `gatsby-plugin-feed` and `gatsby-plugin-manifest`; its `test` script only prints a reminder and exits 1, and there is no linter beyond `prettier`. `gatsby-node.js` creates a page per `MarkdownRemark` node with `previousPostId` and `nextPostId`, adds a `slug` field in `onCreateNode`, and in `createSchemaCustomization` declares `Frontmatter` with `title`, `description` and `date` only. `src/templates/blog-post.js` queries those three and renders the header, the HTML and a `Bio`. `src/pages/index.js` lists every post with its date and `description` or `excerpt`. `src/components/seo.js` sets `og:title`, `og:description`, `og:type` and a `summary` Twitter card, with no `og:image` at all. `src/components/bio.js` already uses `StaticImage` for `src/images/profile-pic.png`, and `content/blog/hello-world/` holds `salty_egg.jpg` next to its `index.md`, referenced inline in the markdown but not in the frontmatter. `siteMetadata.siteUrl` is set in `gatsby-config.js`.

It extends the schema first so a post without an image keeps building. `createSchemaCustomization` adds `featuredImage: File @fileByRelativePath` and `featuredImageAlt: String` to `Frontmatter`, which resolves a frontmatter path relative to the post's own folder through the existing filesystem sources. `blog-post.js` queries `gatsbyImageData` at width 1200 with a blurred placeholder under `featuredImage.childImageSharp` and renders `GatsbyImage` from `getImage()` between the header and the body when present, with `featuredImageAlt` as the `alt`. `index.js` queries a 300 by 200 `gatsbyImageData` with cover fit and renders a thumbnail beside each `post-list-item`, with a few lines added to `src/style.css` next to the existing `.post-list-item` rules. `seo.js` takes an optional `image` prop, builds an absolute URL from `siteUrl` and the file's `publicURL`, emits `og:image`, `og:image:width` and `og:image:height`, and switches the Twitter card to `summary_large_image` when an image is given; `blog-post.js` queries a 1200 by 630 `resize` for that and passes it from its `Head` export. `content/blog/hello-world/index.md` gets `featuredImage: ./salty_egg.jpg` and an alt text as the worked example, and the other two posts stay without one to show the fallback.

There is no test suite to run, so the agent proves the change with the build. It runs `npm run build`, which fails on any GraphQL field the schema does not declare, then `npm run serve` and fetches the index, `/hello-world/` and `/my-second-post/` with `curl`, checking for the `<picture>` markup with `srcset` on the post, the thumbnail on the index, the `og:image` tag with an absolute `https://` URL, and the absence of all three on the post without an image. It runs `npm run format` so `prettier` leaves the diff clean, confirms `rss.xml` still builds from `gatsby-plugin-feed`, and pastes the build summary and the fetched tags into the pull request.

## Start it with one API call

`POST https://api.phoenix.so/o/{org}/sessions`

```json
{
  "agent": "claude",
  "input": "Add featured images to this Gatsby blog and open a pull request. Read package.json, gatsby-config.js, gatsby-node.js, src/templates/blog-post.js, src/pages/index.js, src/components/seo.js, src/components/bio.js, src/style.css and the three posts under content/blog first. In createSchemaCustomization add featuredImage: File @fileByRelativePath and featuredImageAlt: String to Frontmatter so posts without one still build. In blog-post.js query gatsbyImageData at width 1200 and render GatsbyImage between the header and the body when present. In index.js render a 300 by 200 cover thumbnail per post with styles added beside the existing .post-list-item rules. Give seo.js an optional image prop that emits og:image with an absolute URL from siteUrl, og:image:width and og:image:height, and switches twitter:card to summary_large_image; pass a 1200 by 630 resize from the post's Head export. Set featuredImage: ./salty_egg.jpg with an alt on content/blog/hello-world/index.md and leave the other posts without one. Run npm run build, npm run serve, fetch /, /hello-world/ and /my-second-post/ with curl, show the picture markup and the og:image tag, run npm run format, and paste the results in the pull request. Do not add a dependency or change the RSS feed.",
  "resources": [
    {
      "url": "https://github.com/your-org/your-app"
    }
  ]
}
```

1. **Get an API key.** In the console, open Settings, then API keys, and create a key for your org. Send it in the Authorization header as a Bearer token.
2. **Send the request.** POST the body in request.json to https://api.phoenix.so/o/{org}/sessions, with your repository as the resources URL. The run starts in its own cloud sandbox.
3. **Read what came back.** Follow the run on its live stream or wait for the run.completed webhook, then read the result and its trajectory.

## What comes back

What comes back is a pull request that edits `gatsby-node.js`, `src/templates/blog-post.js`, `src/pages/index.js`, `src/components/seo.js`, `src/style.css` and one post, with no change to `package.json` because the sharp plugins are already installed. The description shows the `og:image` tag and the `<picture>` element from the built pages and the `gatsby build` summary.

The starter has no CI workflow of its own, so the build output in the description is the check; a repository that runs `npm run build` on pull requests gets the same check there.

A reviewer checks that a post with an image also has an alt text, that `og:image` is absolute and 1200 by 630, that the index thumbnails are the small rendition and not the full image, and that the blurred placeholder does not bloat the HTML of the list page. Then they add images to the rest of the posts.

## Run it on every Gatsby blog without post images

A team with several Gatsby sites sends one `POST /o/{org}/sessions` per repository with this prompt and that repository in `resources`. Each run reads its own schema and templates, so a site on MDX gets the field declared on its MDX frontmatter type instead, and each run opens its own pull request.

Give the API key a `webhook_url` and Rush POSTs `run.completed` when each turn ends, with the `session_url` to read the pull request and the build output from.

## Why run it on Rush?

A Gatsby image change is only proven by a build: the schema, the GraphQL query and the sharp transforms have to agree, and the error shows at build time or not at all. In a Rush sandbox the agent runs `gatsby build`, serves the result and reads the rendered `<picture>` and `og:image` tags back with `curl`, and keeps all of it in the run's record, so the pull request shows the markup rather than describing it.

The change uses the plugins the starter already installs and the `@fileByRelativePath` extension Gatsby ships, so nothing is added to `package.json`. The same request works on any Gatsby blog that sources markdown through `gatsby-transformer-remark`.

## Frequently asked questions

### What happens to posts without a featuredImage?

They build and render as before. The field is declared in the schema, so the query returns `null` for them, the template skips the image, the index shows no thumbnail, and `seo.js` keeps the `summary` card with no `og:image`.

### Why declare the field instead of letting Gatsby infer it?

Inference only works when at least one post sets the field, and it breaks the build the day the last such post is removed. An explicit `File @fileByRelativePath` on `Frontmatter` keeps the query valid whatever the content holds.

### Does the RSS feed include the image?

Not in this change; the feed's `serialize` in `gatsby-config.js` keeps its fields. Adding an enclosure is a short follow-up on that function, and the pull request notes it.

### Which image sizes does the build generate?

A constrained 1200 pixel wide set for the post page, a 300 by 200 cover thumbnail for the index, and a 1200 by 630 resize for the share card, all produced by `gatsby-plugin-sharp` at build time from the file named in the frontmatter.

## More Gatsby jobs

- [Add Giscus Comments to a Gatsby Blog](https://getrush.ai/run/add-giscus-comments/gatsby/)
- [Set Up GitHub Actions CI for a Gatsby Site](https://getrush.ai/run/add-github-actions-ci/gatsby/)
- [Add i18n to a Gatsby Blog](https://getrush.ai/run/add-i18n/gatsby/)

## More jobs like this: Hand off a long task

- [Add File Uploads to an AdonisJS App](https://getrush.ai/run/add-file-uploads/adonisjs/)
- [Add File Uploads to a CodeIgniter 4 App](https://getrush.ai/run/add-file-uploads/codeigniter/)
- [Add File Uploads to a Livewire Component](https://getrush.ai/run/add-file-uploads/livewire/)

## Keep reading

- [How a Rush run works on your repository, from request to pull request](https://getrush.ai/cloud/)
- [Compare cloud agents, agent sandboxes and AI code review](https://getrush.ai/compare/)
- [Rush pricing: Free, Pro and Max](https://getrush.ai/pricing/)
