Skip to content

Instantly share code, notes, and snippets.

@hcastro
Created May 7, 2025 20:26
Show Gist options
  • Select an option

  • Save hcastro/1d8489eb164fba4c3276c50461715ece to your computer and use it in GitHub Desktop.

Select an option

Save hcastro/1d8489eb164fba4c3276c50461715ece to your computer and use it in GitHub Desktop.
Cache Invalidation playbook (nextjs 15 / RSC)

1. Tell Next.js not to cache anything in this route

Add either (they are equivalent):

// src/app/components/page.tsx ─ very top of the file
export const revalidate = 0;            // Renders on **every** request
// --- or ---
export const dynamic = 'force-dynamic'; // Opt-out of Full-Route + Data cache

revalidate = 0 / dynamic = 'force-dynamic' skips both the Full Route Cache and the Data Cache, so the server calls your RSC again each hit. Only the tiny client-side Router cache is left, and that clears on hard-reloads.


2. Make external data calls “no-store”

Next only auto-caches fetch() calls. Because you’re using the Contentful SDK (client.getEntries()), mark the segment as uncached yourself:

// src/lib/contentful/client.ts
import { unstable_noStore as noStore } from 'next/cache';

export const getPremierPartners = async () => {
  noStore();                // <- forces fresh call each request
  return (await client.getEntries({ content_type: 'marketplaceBlockPremierPartner' })).items;
};

If you ever switch to plain fetch, add { cache: 'no-store' } or revalidate: 0 to the fetch options instead.


3. Disable Netlify’s build-time cache when you redeploy staging

Netlify persists .next/cache, node_modules, etc. between builds. For a truly clean rebuild:

  • One-off: Deploys → Trigger deploy → “Clear cache and deploy site” in the UI (or run netlify deploy --build --clearCache --context=staging from the CLI).

  • Every build: set an env var to skip the cache:

    # netlify.toml
    [build.environment]
      NETLIFY_NEXT_PLUGIN_CACHE = "false"   # plugin won’t restore .next/cache

(If you also use the community netlify-plugin-cache-nextjs, remove or disable it.)


4. Purge Netlify’s CDN cache for SSR / API functions (optional)

Static assets are fingerprinted, so they don’t need purging. For SSR responses you can invalidate on demand via the Netlify API:

curl -X POST \
     -H "Authorization: Bearer $NETLIFY_AUTH_TOKEN" \
     -d '{}' \
     https://api.netlify.com/api/v1/sites/<site_id>/purgeCache

This avoids a full redeploy and clears the edge cache in seconds.


5. Automate it just for staging

Put the opts behind an env check so production can still use ISR if you want:

export const revalidate = process.env.NEXT_PUBLIC_STAGE === 'staging' ? 0 : 3600 // 1 h in prod

TL;DR

  • Next: export const revalidate = 0 or dynamic = 'force-dynamic' + noStore()
  • Netlify build cache: “Clear cache and deploy” or NETLIFY_NEXT_PLUGIN_CACHE=false
  • Netlify CDN cache (SSR): optional purgeCache API call

With those switches flipped, your staging environment will always render the very latest Contentful / RaaS data on every request, no manual hard-refreshes required.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment