Skip to content
Discussion options

You must be logged in to vote

Yes, your understanding is correct. During static generation, request-specific APIs like headers() or req.headers are not available since there is no request.

Next.js does not currently provide a built-in isStaticGeneration flag, but you can handle this situation safely using the following approaches:

1. Use try/catch around headers() to detect the environment

import { headers } from 'next/headers';

let headerValue: string | null = null;

try {
  const hdrs = headers();
  headerValue = hdrs.get('x-my-header');
} catch {
  // Likely running in static generation
  headerValue = null;
}

This is the most reliable and straightforward way to prevent build-time errors.

2. Use process.env.NEXT…

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@finom
Comment options

Answer selected by finom
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Help
Labels
None yet
2 participants