---
title: generateViewport
description: API Reference for the generateViewport function.
url: "https://nextjs.org/docs/14/app/api-reference/functions/generate-viewport"
docs_index: /docs/14/llms.txt
lastUpdated: 2024-03-19
prerequisites:
  - "API Reference: /docs/14/app/api-reference"
  - "Functions: /docs/14/app/api-reference/functions"
related:
  - /docs/14/app/api-reference/file-conventions/metadata
  - /docs/14/app/building-your-application/optimizing/metadata
---

> For an index of all documentation, see [/docs/14/llms.txt](/docs/14/llms.txt).

You can customize the initial viewport of the page with the static `viewport` object or the dynamic `generateViewport` function.

> **Good to know:**
>
> - The `viewport` object and `generateViewport` function exports are **only supported in Server Components**.
> - You cannot export both the `viewport` object and `generateViewport` function from the same route segment.
> - If you're coming from migrating `metadata` exports, you can use [metadata-to-viewport-export codemod](/docs/app/building-your-application/upgrading/codemods#metadata-to-viewport-export) to update your changes.

## The `viewport` object

To define the viewport options, export a `viewport` object from a `layout.js` or `page.js` file.

**TypeScript**

```tsx title="layout.tsx | page.tsx" 
import type { Viewport } from 'next'

export const viewport: Viewport = {
  themeColor: 'black',
}

export default function Page() {}
```

**JavaScript**

```jsx title="layout.js | page.js" 
export const viewport = {
  themeColor: 'black',
}

export default function Page() {}
```

## `generateViewport` function

`generateViewport` should return a [`Viewport` object](#viewport-fields) containing one or more viewport fields.

**TypeScript**

```tsx title="layout.tsx | page.tsx" 
export function generateViewport({ params }) {
  return {
    themeColor: '...',
  }
}
```

**JavaScript**

```jsx title="layout.js | page.js" 
export function generateViewport({ params }) {
  return {
    themeColor: '...',
  }
}
```

> **Good to know:**
>
> - If the viewport doesn't depend on runtime information, it should be defined using the static [`viewport` object](#the-viewport-object) rather than `generateMetadata`.

## Viewport Fields

### `themeColor`

Learn more about [theme-color](https://developer.mozilla.org/docs/Web/HTML/Element/meta/name/theme-color).

**Simple theme color**

```jsx title="layout.js | page.js"
export const viewport = {
  themeColor: 'black',
}
```

```html title="<head> output"
<meta name="theme-color" content="black" />
```

**With media attribute**

```jsx title="layout.js | page.js"
export const viewport = {
  themeColor: [
    { media: '(prefers-color-scheme: light)', color: 'cyan' },
    { media: '(prefers-color-scheme: dark)', color: 'black' },
  ],
}
```

```html title="<head> output"
<meta name="theme-color" media="(prefers-color-scheme: light)" content="cyan" />
<meta name="theme-color" media="(prefers-color-scheme: dark)" content="black" />
```

### `width`, `initialScale`, `maximumScale` and 'userScalable'

> **Good to know:**
>
> The `viewport` meta tag is automatically set with the following default values. Usually, manual configuration is unnecessary as the default is sufficient. However, the information is provided for completeness.

```jsx title="layout.js | page.js"
export const viewport = {
  width: 'device-width',
  initialScale: 1,
  maximumScale: 1,
  userScalable: 1,
  // Also supported by less commonly used
  // interactiveWidget: 'resizes-visual',
}
```

```html title="<head> output"
<meta
  name="viewport"
  content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=1"
/>
```

### `colorScheme`

Learn more about [`color-scheme`](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/meta/name#:~:text=color%2Dscheme%3A%20specifies,of%20the%20following%3A).

```jsx title="layout.js | page.js"
export const viewport = {
  colorScheme: 'dark',
}
```

```html title="<head> output"
<meta name="color-scheme" content="dark" />
```

## Types

You can add type safety to your viewport object by using the `Viewport` type. If you are using the [built-in TypeScript plugin](/docs/app/building-your-application/configuring/typescript) in your IDE, you do not need to manually add the type, but you can still explicitly add it if you want.

### `viewport` object

```tsx
import type { Viewport } from 'next'

export const viewport: Viewport = {
  themeColor: 'black',
}
```

### `generateViewport` function

#### Regular function

```tsx
import type { Viewport } from 'next'

export function generateViewport(): Viewport {
  return {
    themeColor: 'black',
  }
}
```

#### With segment props

```tsx
import type { Viewport } from 'next'

type Props = {
  params: { id: string }
  searchParams: { [key: string]: string | string[] | undefined }
}

export function generateViewport({ params, searchParams }: Props): Viewport {
  return {
    themeColor: 'black',
  }
}

export default function Page({ params, searchParams }: Props) {}
```

#### JavaScript Projects

For JavaScript projects, you can use JSDoc to add type safety.

```js
/** @type {import("next").Viewport} */
export const viewport = {
  themeColor: 'black',
}
```

## Version History

| Version   | Changes                                       |
| --------- | --------------------------------------------- |
| `v14.0.0` | `viewport` and `generateViewport` introduced. |

## Next Steps

View all the Metadata API options.

- [Metadata Files](/docs/14/app/api-reference/file-conventions/metadata): API documentation for the metadata file conventions.
- [Metadata](/docs/14/app/building-your-application/optimizing/metadata): Use the Metadata API to define metadata in any layout or page.

---

For a semantic overview of all documentation, see [/sitemap.md](/sitemap.md)

For an index of all available documentation, see [/docs/14/llms.txt](/docs/14/llms.txt)