Shopify products at build time
Pulling a product catalogue into a static build, and what that costs you.
A client sells original paintings — one of each, never restocked. The stock lives in Shopify, but the site is static Astro, and I wanted the gallery pages to be real pages rather than a spinner over an API call.
So the products get fetched at build time. A small module authenticates against the Shopify Admin API with OAuth client credentials, fetches the collection, and hands back a plain array. Astro then generates one page per available painting.
Two things worth writing down.
The token is fetched fresh on every build. It expires after 24 hours, which is shorter than the gap between deploys on a site like this. Trying to cache it anywhere would have been a slow-burning outage. Getting a new one takes one request, so there’s no reason to be clever.
Availability is a build-time snapshot. inventory_quantity > 0 decides
whether a painting gets a page at all. For one-of-a-kind work that’s mostly
fine — the number goes 1 → 0 once and never comes back — but “mostly fine” is
carrying weight. Between a sale and the next build, the site is confidently
advertising something that no longer exists.
The fix is a webhook that triggers a rebuild when inventory changes, which is the sort of thing that sounds like five minutes and isn’t. For now the gap is short enough to live with, and the sold state is honest as soon as the site rebuilds.
What this doesn’t get you is a cart. Right now “Add to cart” is a link to the Shopify product page, which means the customer leaves the site to buy anything. That’s the next problem.