Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NextJS dynamic title

Tags:

next.js

Have been googling forever and found a way to change the <title>. That way is this: https://github.com/zeit/next.js/tree/master/examples/layout-component

The main problem with this is that everytime someone refresh the site/change page the title goes from http://localhost:3000 to the actual Title (eg. About us) and I'm a bit afraid of how this is affecting the SEO.

What is the correct way of chaning the page title dynamically?

My layout file:

import Link from 'next/link'
import Head from './../node_modules/next/head'

export default function Layout({ children, title = 'Welcome to my website' }) {
  return (
    <div>
        <Head>
          <title>{title}</title>
        </Head>

        {children}
    </div>
  )
}
like image 345
sfsefsf33fs3fs3fs Avatar asked Nov 25 '25 06:11

sfsefsf33fs3fs3fs


1 Answers

Check out next-seo and install it in your next.js application.

yarn add next-seo 
# or 
npm install --save next-seo

And it will handle the page title and the meta description for you magically.

import React from 'react';
import { NextSeo } from 'next-seo'; // then add the `NextSeo` at any `pages/` that you wish

export default () => (
  <>
    <NextSeo
      title="About Us, or just any title that you wish"
      description="Then with a short description here."
    />
    <p>Simple Usage</p>
  </>
);

I have implemented the same tactic on my own web app here.

like image 161
xun Avatar answered Nov 28 '25 02:11

xun



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!