Open In App

Next.js Function generateViewport

Last Updated : 20 Sep, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

Next.js is a Javascript based framework to develop full-stack web applications. In some cases, we want to customize the initial viewports of the page getting rendered with some dynamic content. In this article, we will learn how to do so using generateViewport function in next.js

We can set the viewport metadata in our html pages generated by next.js using viewport object, or generateViewport function. The return value of the generateViewport function helps in setting up the dynamic contents of the viewport metadata in HTML.

Setting up the NextJS project

Use the below commands to create a next.js project

npx create-next-app@latest gen-viewport
cd gen-viewport

Project structure:

d
Project Structure

Updated dependencies:

"dependencies": {
"react": "^18",
"react-dom": "^18",
"next": "14.2.10"
}

Creating viewport using viewport object

We will create a viewport meta tag using the viewport object in next.js. We will add a meta tag with the property of "theme-color" and its value as "red".

JavaScript
export const viewport = {
    themeColor: "red",
};

export default function Home() {
    return (
        <>
            <body>
                <h1>My Page</h1>
            </body>
        </>
    );
}

Run the server

Start the server using the below command:

npm run dev

Output:

next

Creating viewport using generateViewport function

we will create a viewport meta tag using the generateViewport function that next.js provides. We will add a meta tag with property of "theme-color" and its value as "red"

JavaScript
 src/app/page.js 
 
export function generateViewport({ params }) {
    return {
        themeColor: "red",
    };
}

export default function Home() {
    return (
        <>
            <body>
                <h1>My Page</h1>
            </body>
        </>
    ); 
}


Run the server

Start the server using the below command:

npm run dev

Output:

next

Viewport fields

themeColor

To add theme-color metadata, we can return "themeColor" key in the Viewport object in the page layout file.

JavaScript
// src/app/page.js

export function generateViewport({ params }) {
    return {
        themeColor: "red",
    };
}

export default function Home() {
    return (
        <>
            <body>
                <h1>My Page</h1>
            </body>
        </>
    );
}

Output:

next

width, initialScale, maximumScale and userScalable

To set the initialScale, maximumScale, width, and userScalable in the viewport metadata, we can create the Viewport object as below.

JavaScript
// src/app/page.js
export const viewport = {
    width: "device-width",
    initialScale: 1,
    maximumScale: 1,
    userScalable: false,
  };
  
  export default function Home() {
    return (
      <>
        <body>
          <h1>My Page</h1>
        </body>
      </>
    );
  }
  

Output:

s

colorScheme

To set the colorScheme in viewport metadata, we can the viewport object as below.

JavaScript
export const viewport = {
    colorScheme: "dark",
};

export default function Home() {
    return (
        <>
            <body>
                <h1>My Page</h1>
            </body>
        </>
    );

}

Output:

s
Output

Types

We can also add typesafety to the viewport object, or the generateViewport function.

viewport object

To add the type to the viewport object, we can use the Viewport inbuilt type in next.js

import type { Viewport } from 'next'

export const viewport: Viewport = {
colorScheme: "dark",
};

generateViewport function

We can add the types to the generateViewport function using 2 different approaches:

1. Regular function

import type { Viewport } from 'next'

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

2. Using segment props

import type { Viewport } from 'next'

type Props = {
params: { id: string }
}

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

export default function Page({ params }: Props) {}

Next Article
Article Tags :

Similar Reads