To set page titles in Next.js, leverage the next/head module to include HTML <head> elements, such as the <title> tag. Here are the steps to set specific titles for each page in your Next.js application:
- Import the Head Component
In your page component file, import the
Headcomponent fromnext/head.
jsximport Head from 'next/head';
- Use the Head Component in Your Page Component
Within your page component's JSX, wrap the
<title>tag inside theHeadcomponent and specify your desired title.
For example, to set the homepage title to 'Home Page', you can do the following:
jsximport Head from 'next/head'; export default function Home() { return ( <> <Head> <title>Home Page</title> </Head> {/* Page content */} </> ); }
- Set Different Titles for Different Pages For each page in your application, follow the same approach to set unique titles. For instance, for an About page (about.js), set the title to 'About Us':
jsximport Head from 'next/head'; export default function About() { return ( <> <Head> <title>About Us</title> </Head> {/* About page content */} </> ); }
This ensures each page has a personalized title, enhancing user experience and SEO optimization. When the page is rendered, the browser tab will display the relevant title.
Ensure that each page component uses the Head component to guarantee distinct titles for all pages. For applications with dynamic pages or route parameters, dynamically set the title based on page content or parameters.