Fix Error: Error: invariant: static generation store missing in revalidateTag in Next.js
This error occurs when revalidateTag or revalidatePath is called outside a server action or route handler context. These functions require the static generation store which is only available during server-side request processing. Fix it by calling revalidation functions inside a server action or API route handler.
Reading the Stack Trace
Here's what each line means:
- at revalidateTag (/app/node_modules/next/dist/server/web/spec-extension/revalidate-tag.js:12:15): The revalidateTag function checks for the static generation store and throws because it is not available in the current execution context.
- at updatePost (/app/src/app/actions.ts:24:3): The server action at line 24 calls revalidateTag but may not have the 'use server' directive or is being invoked incorrectly.
- at async callServer (/app/node_modules/next/dist/client/app-call-server.js:18:16): The client is calling the server action through Next.js's server action RPC mechanism.
Common Causes
1. Missing 'use server' directive
The function calling revalidateTag is not marked as a server action, so it runs in the wrong context.
// src/app/actions.ts
import { revalidateTag } from 'next/cache';
// Missing 'use server' directive
export async function updatePost(id: string, data: FormData) {
await db.post.update({ where: { id }, data: { title: data.get('title') } });
revalidateTag('posts');
}
2. Calling revalidation in a client component
revalidateTag is imported and called directly in a client component instead of through a server action.
'use client';
import { revalidateTag } from 'next/cache';
export function EditButton() {
return <button onClick={() => revalidateTag('posts')}>Refresh</button>;
}
3. Calling revalidation in a utility function
A shared utility function calls revalidateTag but is invoked from a context that does not have the server store.
// src/lib/cache-utils.ts
import { revalidateTag } from 'next/cache';
export function invalidatePostCache() {
revalidateTag('posts'); // No server context when called from client
}
The Fix
Add the 'use server' directive at the top of the file to mark all exported functions as server actions. This ensures Next.js provides the static generation store when the function executes, allowing revalidateTag to work correctly.
// src/app/actions.ts
import { revalidateTag } from 'next/cache';
export async function updatePost(id: string, data: FormData) {
await db.post.update({ where: { id }, data: { title: data.get('title') } });
revalidateTag('posts');
}
'use server';
import { revalidateTag } from 'next/cache';
export async function updatePost(id: string, data: FormData) {
await db.post.update({ where: { id }, data: { title: data.get('title') as string } });
revalidateTag('posts');
}
Testing the Fix
import { updatePost } from '@/app/actions';
import { revalidateTag } from 'next/cache';
jest.mock('next/cache', () => ({
revalidateTag: jest.fn(),
}));
jest.mock('@/lib/db', () => ({
db: {
post: {
update: jest.fn().mockResolvedValue({ id: '1', title: 'Updated' }),
},
},
}));
describe('updatePost server action', () => {
it('updates the post and revalidates the cache tag', async () => {
const formData = new FormData();
formData.set('title', 'New Title');
await updatePost('1', formData);
expect(revalidateTag).toHaveBeenCalledWith('posts');
});
it('calls db.post.update with correct arguments', async () => {
const { db } = require('@/lib/db');
const formData = new FormData();
formData.set('title', 'Updated Title');
await updatePost('1', formData);
expect(db.post.update).toHaveBeenCalledWith({
where: { id: '1' },
data: { title: 'Updated Title' },
});
});
});
Run your tests:
npm test
Pushing Through CI/CD
git checkout -b fix/revalidation-use-server-directive,git add src/app/actions.ts src/app/__tests__/actions.test.ts,git commit -m "fix: add 'use server' directive to enable revalidateTag",git push origin fix/revalidation-use-server-directive
Your CI config should look something like this:
name: CI
on:
pull_request:
branches: [main]
jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: '20'
cache: 'npm'
- run: npm ci
- run: npm test -- --coverage
- run: npm run build
The Full Manual Process: 18 Steps
Here's every step you just went through to fix this one bug:
- Notice the error alert or see it in your monitoring tool
- Open the error dashboard and read the stack trace
- Identify the file and line number from the stack trace
- Open your IDE and navigate to the file
- Read the surrounding code to understand context
- Reproduce the error locally
- Identify the root cause
- Write the fix
- Run the test suite locally
- Fix any failing tests
- Write new tests covering the edge case
- Run the full test suite again
- Create a new git branch
- Commit and push your changes
- Open a pull request
- Wait for code review
- Merge and deploy to production
- Monitor production to confirm the error is resolved
Total time: 30-60 minutes. For one bug.
Or Let bugstack Fix It in Under 2 minutes
Every step above? bugstack does it automatically.
Step 1: Install the SDK
npm install bugstack-sdk
Step 2: Initialize
import { initBugStack } from 'bugstack-sdk'
initBugStack({ apiKey: process.env.BUGSTACK_API_KEY })
Step 3: There is no step 3.
bugstack handles everything from here:
- Captures the stack trace and request context
- Pulls the relevant source files from your GitHub repo
- Analyzes the error and understands the code context
- Generates a minimal, verified fix
- Runs your existing test suite
- Pushes through your CI/CD pipeline
- Deploys to production (or opens a PR for review)
Time from error to fix deployed: Under 2 minutes.
Human involvement: zero.
Try bugstack Free →No credit card. 5-minute setup. Cancel anytime.
Deploying the Fix (Manual Path)
- Add the 'use server' directive to the actions file.
- Run the test suite locally to confirm revalidation works.
- Open a pull request with the server action fix.
- Wait for CI checks to pass on the PR.
- Merge to main and verify cache revalidation works in staging.
Frequently Asked Questions
BugStack verifies the server action executes in the correct context, confirms revalidation triggers properly, runs your tests, and validates the build before marking it safe.
Every fix is delivered as a pull request with full CI validation. Your team reviews and approves before anything reaches production.
revalidateTag invalidates all cached fetch requests tagged with a specific string. revalidatePath invalidates the cache for a specific URL path. Both require a server action or route handler context.
Yes. API routes in the App Router (route.ts files) have the static generation store available. You can call revalidateTag inside a GET, POST, or other handler function.