DEV Community

Rodrigo Oler
Rodrigo Oler

Posted on

Resolving viewport duplication in Next.js 13.4

nextjs

  1. Introduction
  2. The Problem
  3. The Solution
  4. Conclusion

Introduction

When working with Next.js, it's common to encounter some technical issues along the way. One such problem that may arise is the duplication of the viewport tag. This article aims to address this issue specifically, providing a solution to resolve the viewport duplication problem in components in Next.js 13.


The Problem

Before we present the solution, it's crucial to first understand the problem at hand. When using the viewport tag directly in the code, as shown below:

import './globals.css'
import { Inter } from 'next/font/google'

const inter = Inter({ subsets: ['latin'] })

export default function RootLayout({
  children,
}: {
  children: React.ReactNode
}) {
  return (
    <html lang="en">
      <head>
        <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
      </head>
      <body className={inter.className}>{children}</body>
    </html>
  )
}
Enter fullscreen mode Exit fullscreen mode

warning


The Solution

To solve this problem, we need to change the way we are defining the viewport. Instead of placing it directly in the code, we should specify it in the metadata.

The solution was discovered and documented in a StackOverflow thread.

import './globals.css'
import type { Metadata } from 'next'
import { Inter } from 'next/font/google'

const inter = Inter({ subsets: ['latin'] })

export const metadata: Metadata = {
  title: 'Create Next App',
  description: 'Generated by create next app',
  viewport: 'width=device-width, initial-scale=1, maximum-scale=1', // <-- now here
}

export default function RootLayout({
  children,
}: {
  children: React.ReactNode
}) {
  return (
    <html lang="en">
      <body className={inter.className}>{children}</body>
    </html>
  )
}
Enter fullscreen mode Exit fullscreen mode

success


Conclusion

In the world of software development, we often come across technical challenges. These challenges may seem daunting at first glance, but with the power of the development community and the constant exchange of knowledge, the solution is often just a post away.

Top comments (1)

Collapse
 
oler profile image
Rodrigo Oler

If you found this article helpful, please give it a "like" and consider sharing it with your colleagues. If you have any suggestions, tips, or other strategies, feel free to leave a comment! Your contribution can further enrich the discussion and be of great help to other readers. Thank you!