Fix Error: Error: Dynamic server usage: force-dynamic is not allowed in generateStaticParams in Next.js
This error occurs when a statically generated page uses fetch with cache: 'no-store' or sets force-dynamic, but also exports generateStaticParams. These are contradictory: static generation requires cacheable data. Fix it by removing force-dynamic and using revalidation instead, or by removing generateStaticParams for truly dynamic pages.
Reading the Stack Trace
Here's what each line means:
- at staticGenerationBailout (/app/node_modules/next/dist/client/components/static-generation-bailout.js:18:11): Next.js detected a dynamic operation during static generation and threw because static and dynamic modes are incompatible.
- at fetchImpl (/app/node_modules/next/dist/server/lib/patch-fetch.js:172:15): The patched fetch detected cache: 'no-store' or a force-dynamic flag during static generation.
- at async buildStaticPaths (/app/node_modules/next/dist/build/utils.js:847:22): The build system is trying to generate static paths but the data-fetching strategy conflicts with static generation.
Common Causes
1. force-dynamic with generateStaticParams
The page exports both generateStaticParams and force-dynamic, which are mutually exclusive configuration options.
export const dynamic = 'force-dynamic';
export async function generateStaticParams() {
const posts = await fetch('https://api.example.com/posts').then(r => r.json());
return posts.map((p: any) => ({ slug: p.slug }));
}
export default async function BlogPost({ params }) {
const post = await fetch(`https://api.example.com/posts/${params.slug}`, {
cache: 'no-store'
}).then(r => r.json());
return <article>{post.title}</article>;
}
2. fetch with no-store in static page
Using cache: 'no-store' in a fetch call within a page that has generateStaticParams makes the page dynamic, conflicting with static generation.
export async function generateStaticParams() {
return [{ slug: 'hello' }, { slug: 'world' }];
}
export default async function Page({ params }) {
const data = await fetch(`/api/posts/${params.slug}`, { cache: 'no-store' });
return <div>{data.title}</div>;
}
3. Using cookies() or headers() in static page
Calling dynamic functions like cookies() in a page that exports generateStaticParams forces dynamic rendering.
import { cookies } from 'next/headers';
export async function generateStaticParams() {
return [{ id: '1' }, { id: '2' }];
}
export default async function Page({ params }) {
const token = cookies().get('auth');
return <div>Post {params.id}</div>;
}
The Fix
Replace force-dynamic and cache: 'no-store' with time-based revalidation using next: { revalidate: 60 }. This keeps the page statically generated but refreshes the data every 60 seconds, combining the performance of static generation with reasonably fresh data.
export const dynamic = 'force-dynamic';
export async function generateStaticParams() {
const posts = await fetch('https://api.example.com/posts').then(r => r.json());
return posts.map((p: any) => ({ slug: p.slug }));
}
export default async function BlogPost({ params }) {
const post = await fetch(`https://api.example.com/posts/${params.slug}`, {
cache: 'no-store'
}).then(r => r.json());
return <article>{post.title}</article>;
}
export const revalidate = 60;
export async function generateStaticParams() {
const posts = await fetch('https://api.example.com/posts').then(r => r.json());
return posts.map((p: any) => ({ slug: p.slug }));
}
export default async function BlogPost({ params }: { params: { slug: string } }) {
const post = await fetch(`https://api.example.com/posts/${params.slug}`, {
next: { revalidate: 60 }
}).then(r => r.json());
return <article>{post.title}</article>;
}
Testing the Fix
import { render, screen } from '@testing-library/react';
import BlogPost, { generateStaticParams } from '@/app/blog/[slug]/page';
global.fetch = jest.fn();
describe('BlogPost', () => {
beforeEach(() => {
(fetch as jest.Mock).mockResolvedValue({
json: () => Promise.resolve({ title: 'Hello World', content: 'Test' }),
});
});
it('renders the post title', async () => {
const Component = await BlogPost({ params: { slug: 'hello' } });
render(Component);
expect(screen.getByText('Hello World')).toBeInTheDocument();
});
it('generateStaticParams returns slugs', async () => {
(fetch as jest.Mock).mockResolvedValue({
json: () => Promise.resolve([{ slug: 'hello' }, { slug: 'world' }]),
});
const params = await generateStaticParams();
expect(params).toEqual([{ slug: 'hello' }, { slug: 'world' }]);
});
});
Run your tests:
npm test
Pushing Through CI/CD
git checkout -b fix/fetch-cache-static-params-conflict,git add src/app/blog/[slug]/page.tsx src/app/blog/[slug]/__tests__/page.test.tsx,git commit -m "fix: replace force-dynamic with revalidation for static pages",git push origin fix/fetch-cache-static-params-conflict
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)
- Remove force-dynamic and replace with revalidation strategy.
- Run the Next.js build locally to confirm static generation succeeds.
- Open a pull request with the caching changes.
- Wait for CI checks to pass on the PR.
- Merge to main and verify ISR works correctly in staging.
Frequently Asked Questions
BugStack verifies that static generation completes successfully, tests the revalidation strategy, runs your test suite, and confirms 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.
It depends on how fresh your data needs to be. For blog posts, 60-300 seconds is typical. For frequently changing data, consider 10-30 seconds or use on-demand revalidation with revalidateTag.
Yes. Each page can have its own caching strategy. Use generateStaticParams with revalidation for content pages and force-dynamic for personalized pages that need request-time data.