Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set document's title per page?

Tags:

I am actually using the pages/_document.js hook to add Bootstrap and jQuery to my pages, by the way I set the <Head>

export default class MyDocument extends Document {   render() {     return (       <html>         <Head>           <title>Default title</title>           <link rel="stylesheet" href="/static/lib/bootstrap3/css/bootstrap.min.css" />         </Head>          <body>           <Main/>           <NextScript/>           <script src="/static/lib/jquery3/jquery-3.3.1.min.js" />           <script src="/static/lib/bootstrap3/js/bootstrap.min.js" />         </body>       </html>     )   } } 

Now I would like to set a different Title,for my pages. Is it possible to used <Head> outside of Document ? I mean in <div> like this:

const ContactPage = () => {   return (     <div>       <Head>         <title>You better contact us!</title>       </Head>        <div className="page-body">...</div>     </div>   ) } 

And if possible, will it overwrite or merge what is already set in pages/_document.js ?

like image 208
acmoune Avatar asked Sep 04 '18 16:09

acmoune


People also ask

How do you make a page title?

Again, on Windows, you can select Ctrl + F and then type “title” to quickly find the Title. That's all there is to it.

How do I change the dynamic page title?

The textContent property of an element returns the text content of a specific node. The title of the page can be changed by assigning the required new title as a string to the textContent property. This will change the title of the website to the preferred title.

How do I change the title of each page in react?

import { Helmet } from 'react-helmet'; When we go to the Guides page in the browser, we get our customized title in the tab: “CoderGuides | Guides.” That's all there is to it. So, to customize the title for all your pages, just repeat the same steps for each page you want to customize the title for.

How do you change the title of a page in HTML?

Editing the HTML Title Tag html, or default. html. Update the text between the opening and closing title tags, i.e. <title> and </title>. For this to work, you must maintain the structure of the opening <title> and closing </title> and just modify the text between them.


1 Answers

You want to use the next/head component:

import Head from 'next/head'  export default () =>   <div>     <Head>       <title>My page title</title>       <meta name="viewport" content="initial-scale=1.0, width=device-width" />     </Head>      <p>Hello world!</p>   </div> 

See the docs: https://nextjs.org/docs/api-reference/next/head

like image 192
Jeff Kilbride Avatar answered Sep 22 '22 09:09

Jeff Kilbride