🔥 All-Access Pass — $199 USD (Regular $499) · Unlock all 2,836 Astro themes ➔
komal.nakrani
Get All-Access Pass ($199)
ThemesCategoriesDocsBlogTips & TricksPricingGet All-Access Pass ($199)
Server Rendering••By Komal Nakrani

Astro ISR on Vercel: Cache On-Demand Pages

Enable Astro ISR on Vercel with the adapter's isr option: expiration times, excluding routes, on-demand revalidation with bypassToken, and what not to cache.

#Astro#Server Rendering#Astro ISR#Vercel#Caching

Astro ISR on Vercel is switched on with the isr option of @astrojs/vercel: your on-demand pages are rendered once, cached by Vercel, and regenerated after the expiration you set, so visitors get cached HTML at static speed while content still updates. Use exclude for routes that must run on every request, such as APIs and personalised pages, and bypassToken to revalidate a page on demand after a content change.

Key Takeaways

  • ISR applies to on-demand (server-rendered) routes; prerendered pages are already static.
  • isr: true caches each page until the next deployment; expiration sets a time limit in seconds.
  • Exclude anything that depends on cookies, sessions or request headers.
  • A request with header x-prerender-revalidate: <bypassToken> regenerates a page immediately.

Enable it

// astro.config.mjs
import { defineConfig } from 'astro/config';
import vercel from '@astrojs/vercel';

export default defineConfig({
  output: 'server',
  adapter: vercel({
    isr: {
      expiration: 60 * 60, // regenerate at most once an hour
      exclude: ['/api/cart', /^\/account\/.*/, /^\/api\/private\/.*/],
    },
  }),
});

npx astro add vercel installs the adapter and adds it to your config. With output: 'server' every page is on demand, and ISR caches all of them except those in exclude. If you use the default static output and only mark some pages export const prerender = false, ISR covers just those pages.

The expiration options:

Setting Behaviour
isr: true Cached until the next production deployment
isr: { expiration: 3600 } Regenerated in the background at most once per hour
isr: { expiration: false } Same as true, written out

When a cached page expires, the next visitor still gets the cached copy while Vercel renders a fresh one in the background. No visitor waits for a render after the first.

What to exclude

ISR serves one cached copy to everyone. Exclude a route if its output depends on:

  • Cookies or sessions: account pages, carts, dashboards.
  • Request headers: geolocation, language negotiation, A/B tests.
  • Mutations: API routes that create or change data.

exclude accepts strings for exact paths and regular expressions for patterns. Excluded routes run as normal serverless functions on every request. Our guide to protected routes in Astro covers the pages that typically need excluding.

Revalidate on demand

Waiting for expiration is fine for most content, but after publishing a correction you may want the page updated now. Set a bypassToken:

// astro.config.mjs
import { defineConfig } from 'astro/config';
import vercel from '@astrojs/vercel';

export default defineConfig({
  output: 'server',
  adapter: vercel({
    isr: {
      expiration: false,
      bypassToken: process.env.ISR_BYPASS_TOKEN,
    },
  }),
});

Use a long random string and keep it in Vercel’s environment variables; Vercel expects it to be at least 32 characters. Then request the page with the token in a header to rebuild and re-cache it:

# Terminal
curl -I https://example.com/blog/my-post/ \
  -H "x-prerender-revalidate: $ISR_BYPASS_TOKEN"

Point your CMS webhook at a small function that sends this request for the changed URL, and pages update within seconds of publishing without a full deploy. Combining expiration: false with on-demand revalidation means pages only regenerate when content really changes. If you would rather rebuild the whole static site, see rebuilding Astro when CMS content changes.

ISR versus static and SSR

Approach Freshness Speed Cost per visit
Static (prerendered) Updates on rebuild Fastest None
ISR After expiration or revalidation Static speed after first render Rare renders
SSR without cache Every request Render time per request One function call

ISR suits large catalogs and content sites where rebuilding everything takes too long but per-request rendering is wasteful. For small sites, a static build with a webhook rebuild is simpler. Our Astro caching guide compares ISR with Cache-Control headers on other hosts, and deploying Astro to Vercel covers the rest of the setup. Any of our Astro themes can move to this model by adding the adapter.

Check that it works

Deploy, load a page twice, and inspect the response headers. A cached response from Vercel’s cache shows a cache status such as HIT in the x-vercel-cache header. If you always see MISS, the route may be excluded, or it may be prerendered and served as a static file instead.

FAQ

Does Astro support ISR?

Yes, on Vercel through the isr option of @astrojs/vercel. It caches on-demand pages and regenerates them after an expiration time or when revalidated with a bypass token.

How do I set the revalidation time for Astro ISR?

Set isr: { expiration: seconds } in the Vercel adapter options. Without expiration, cached pages last until the next deployment.

How do I exclude API routes from ISR?

List them in isr.exclude as strings or regular expressions, for example /^\/api\/.*/. Excluded routes run on every request.

Can I use ISR on Netlify or Cloudflare with Astro?

The isr option is specific to the Vercel adapter. On other hosts, get similar behaviour with Cache-Control and stale-while-revalidate headers on on-demand responses, or with the host’s own caching features.