Familiarity with the <Head> component in Next.js

nextjs head component guide
10 November 2024

The Next.js framework is considered one of the most important and efficient tools for most developers. This framework provides various capabilities for improving performance and simplifying software development. One of these capabilities is the use of the component, which allows you to easily manage the content of the section of your HTML documents across different pages.

Using the component in different Next.js pages allows you to easily change the title, meta descriptions, and other tags. This feature is particularly important for SEO and improving search engine visibility.

Whenever you need to change tags, , or other related <head> items in your projects, you can use the <Head> component dynamically without worrying about interactions with other pages.</p> <p>In continuation, we will look at an example of using the <Head> component in a Next.js project and explain how it works line by line.</p> <h2>Code Example</h2> <pre><code class="language-markup">import Head from 'next/head';<br><br>const MyComponent = () => {<br> return (<br> <div><br> <Head><br> <title>My Page</title><br> <meta name="description" content="This is my page description" /><br> </Head><br> <h1>Hello World!</h1><br> </div><br> );<br>};<br><br>export default MyComponent;<br></code></pre> <h2>Line by Line Code Explanation</h2> <p><code>import Head from 'next/head';</code><br>This line imports the Head library from the Next.js framework for use in our component. This component is used for managing <head> attributes.</p> <p><code>const MyComponent = () => {</code><br>Defines a functional component named MyComponent that will contain all its attributes in a single return statement.</p> <p><code>return ( <div> ... </div> );</code><br>This portion defines the JSX that will be rendered by this component. Here, the <div> serves as the container for all page elements.</p> <p><code><Head>...</Head></code><br>Inside this tag, we can specify anything that should be placed in the HTML <head>, such as <title> and <meta>.</p> <p><code><title>My Page</title></code><br>Defines the title of the page that appears in the browser tab.</p> <p><code><meta name="description" content="This is my page description" /></code><br>Adding a description meta tag to the HTML for the improvement of SEO and content description.</p> <p><code><h1>Hello World!</h1></code><br>The main title of the page presented with a simple message.</p> <p><code>export default MyComponent;</code><br>Makes this component available for use in other parts of the application.</p> </div> </div> </div> </div> </div> <div class="flex items-center justify-between"> <div class="flex items-center flex-wrap -mb-2 !space-x-reverse sm:space-x-2 space-x-0 w-full"> </div> </div> </div> </div> </div> <div class="my-10 w-full rounded-xl sm:px-10 px-4 pt-9 pb-12 dark:bg-dark-930 bg-white"> <h3 class="text-xl">FAQ</h3> <div class="mt-8" id="faq_3741_0"> <div data-question="question_faq_3741_0" id="answer_faq_3741_0" class="faq_answer bg-white dark:bg-dark-890 dark:border-opacity-0 rounded-lg border border-gray-210 flex items-center justify-between py-3 md:px-5 px-2 cursor-pointer"> <div class="flex items-center relative" > <div class="svgFaq !w-10 !h-10 absolute -top-8 -left-5 rotate-[-30deg] font-extrabold pt-2 sm:text-28 dark:text-gray-920 dark:bg-dark-930 text-2xl md:mr-5 mr-2 sm:w-11 w-8 sm:h-11 h-8 rounded-lg bg-slate-700 bg-opacity-20 flex items-center justify-center text-slate-500 font-b" > ? </div> <h3 class="sm:text-lg pt-4 !text-base text-gray-800 font-semibold w-auto sm:w-auto "> Why should we use the <code><Head></code> component? </h3> </div> <span class="arrowfaq transition duration-200 transform flex-shrink-0 -rotate-90 text-gray-800 " :class="{' !rotate-0 dark:!text-blue-450 !text-blue-700' : isActive(0)}"> <svg width="16" height="10" viewBox="0 0 16 10" fill="none" xmlns="http://www.w3.org/2000/svg"> <path d="M1 1.5L8 8.5L15 1.5" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"></path> </svg> </span> </div> <div id="question_faq_3741_0" class="faqquestion hidden sm:ml-16 ml-2 bg-white dark:bg-dark-890 dark:border-opacity-0 rounded-lg border border-gray-210 mt-3 sm:p-8 p-4"> <div class="content-area font-normal text-base leading-8"> <p> Using the <code><Head></code> component allows you to change the <code><meta></code>, <code><title></code>, and other <code><head></code> elements on the page dynamically, which is crucial for SEO and proper display in search engines. </p> </div> </div> </div> <div class="mt-8" id="faq_3741_1"> <div data-question="question_faq_3741_1" id="answer_faq_3741_1" class="faq_answer bg-white dark:bg-dark-890 dark:border-opacity-0 rounded-lg border border-gray-210 flex items-center justify-between py-3 md:px-5 px-2 cursor-pointer"> <div class="flex items-center relative" > <div class="svgFaq !w-10 !h-10 absolute -top-8 -left-5 rotate-[-30deg] font-extrabold pt-2 sm:text-28 dark:text-gray-920 dark:bg-dark-930 text-2xl md:mr-5 mr-2 sm:w-11 w-8 sm:h-11 h-8 rounded-lg bg-slate-700 bg-opacity-20 flex items-center justify-center text-slate-500 font-b" > ? </div> <h3 class="sm:text-lg pt-4 !text-base text-gray-800 font-semibold w-auto sm:w-auto "> How can I change meta descriptions on each page? </h3> </div> <span class="arrowfaq transition duration-200 transform flex-shrink-0 -rotate-90 text-gray-800 " :class="{' !rotate-0 dark:!text-blue-450 !text-blue-700' : isActive(0)}"> <svg width="16" height="10" viewBox="0 0 16 10" fill="none" xmlns="http://www.w3.org/2000/svg"> <path d="M1 1.5L8 8.5L15 1.5" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"></path> </svg> </span> </div> <div id="question_faq_3741_1" class="faqquestion hidden sm:ml-16 ml-2 bg-white dark:bg-dark-890 dark:border-opacity-0 rounded-lg border border-gray-210 mt-3 sm:p-8 p-4"> <div class="content-area font-normal text-base leading-8"> <p> Using the <code><Head></code> component on each page, you can simply add a <code><meta name="description" content="your description" /></code> tag to change the meta descriptions. </p> </div> </div> </div> <div class="mt-8" id="faq_3741_2"> <div data-question="question_faq_3741_2" id="answer_faq_3741_2" class="faq_answer bg-white dark:bg-dark-890 dark:border-opacity-0 rounded-lg border border-gray-210 flex items-center justify-between py-3 md:px-5 px-2 cursor-pointer"> <div class="flex items-center relative" > <div class="svgFaq !w-10 !h-10 absolute -top-8 -left-5 rotate-[-30deg] font-extrabold pt-2 sm:text-28 dark:text-gray-920 dark:bg-dark-930 text-2xl md:mr-5 mr-2 sm:w-11 w-8 sm:h-11 h-8 rounded-lg bg-slate-700 bg-opacity-20 flex items-center justify-center text-slate-500 font-b" > ? </div> <h3 class="sm:text-lg pt-4 !text-base text-gray-800 font-semibold w-auto sm:w-auto "> Can I use multiple <code><Head></code> components on one page? </h3> </div> <span class="arrowfaq transition duration-200 transform flex-shrink-0 -rotate-90 text-gray-800 " :class="{' !rotate-0 dark:!text-blue-450 !text-blue-700' : isActive(0)}"> <svg width="16" height="10" viewBox="0 0 16 10" fill="none" xmlns="http://www.w3.org/2000/svg"> <path d="M1 1.5L8 8.5L15 1.5" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"></path> </svg> </span> </div> <div id="question_faq_3741_2" class="faqquestion hidden sm:ml-16 ml-2 bg-white dark:bg-dark-890 dark:border-opacity-0 rounded-lg border border-gray-210 mt-3 sm:p-8 p-4"> <div class="content-area font-normal text-base leading-8"> <p> Yes, in Next.js, you can use multiple <code><Head></code> components on one page, and their contents will be combined automatically. </p> </div> </div> </div> </div> <div class="my-10 w-full rounded-xl sm:px-10 px-4 pt-9 pb-12 dark:bg-dark-930 bg-white"> <h3 class="text-xl">Related Posts</h3> <ul> <li><a title="Configuring the Training Page for Next.js Using AMP" class="my-2 block text-blue-500 hover:text-blue-900" href="https://mini-learn.net/nextjs-amp-configuration-guide">Configuring the Training Page for Next.js Using AMP</a></li> <li><a title="Caching in Next.js Framework" class="my-2 block text-blue-500 hover:text-blue-900" href="https://mini-learn.net/nextjs-custom-cache-handler">Caching in Next.js Framework</a></li> <li><a title="Programming with Next.js and Using the App Router" class="my-2 block text-blue-500 hover:text-blue-900" href="https://mini-learn.net/nextjs-app-router-client-components">Programming with Next.js and Using the App Router</a></li> <li><a title="Guide to Using App Router in Next.js: Data Fetching and Management" class="my-2 block text-blue-500 hover:text-blue-900" href="https://mini-learn.net/nextjs-app-router-data-fetching-guide">Guide to Using App Router in Next.js: Data Fetching and Management</a></li> <li><a title="Using App Router in Next.js for Optimization and Application Analysis" class="my-2 block text-blue-500 hover:text-blue-900" href="https://mini-learn.net/nextjs-app-router-and-analytics">Using App Router in Next.js for Optimization and Application Analysis</a></li> <li><a title="Managing Redirects in Applications Using App Router in Next.js" class="my-2 block text-blue-500 hover:text-blue-900" href="https://mini-learn.net/nextjs-app-router-redirecting">Managing Redirects in Applications Using App Router in Next.js</a></li> <li><a title="Next.js and Using the Pages Router: Client-side Data Fetching" class="my-2 block text-blue-500 hover:text-blue-900" href="https://mini-learn.net/nextjs-pages-router-client-side-fetching">Next.js and Using the Pages Router: Client-side Data Fetching</a></li> <li><a title="Using the Pages Router in Next.js: Optimization with OpenTelemetry" class="my-2 block text-blue-500 hover:text-blue-900" href="https://mini-learn.net/nextjs-pages-router-opentelemetry">Using the Pages Router in Next.js: Optimization with OpenTelemetry</a></li> <li><a title="How to Define Routes with Next.js?" class="my-2 block text-blue-500 hover:text-blue-900" href="https://mini-learn.net/nextjs-defining-routing">How to Define Routes with Next.js?</a></li> <li><a title="Using API: Reference and Configurations of Next.js for ETag Management" class="my-2 block text-blue-500 hover:text-blue-900" href="https://mini-learn.net/nextjs-pages-router-api-reference-next-config-js-generate-etags">Using API: Reference and Configurations of Next.js for ETag Management</a></li> </ul> </div> </div> </div> </div> </div> </div> </div> </div> </div> </section> </main> </div> <footer class="relative z-0 max-w-[1200px] mx-auto"> <div class="container w-[95%] mx-auto"> <div class="content pb-10 pt-10"> <div class="mb-12 relative"> <span class="absolute border-t border-1 sm:top-2/4 top-5 trasform sm:translate-y-2/4 dark:border-blue-980 border-chambray-100 w-full z-0 right-0"></span> <div class="flex items-center justify-between sm:flex-row flex-col relative z-10"> <div class="logo w-fit-content dark:bg-dark-890 sm:px-0 bg-background-main-rgb px-5 sm:pl-8 sm:mb-0 mb-6"> <img src="https://mini-learn.net/img/minilearnlogoen.webp" class=" inline-block" alt="مینی لرن" width="125"> </div> <ul class="flex dark:bg-dark-890 bg-background-main-rgb sm:pr-8 self-center "> <li class="mr-2"> <a href="https://t.me/mini_learn_ir" class="group flex items-center rounded justify-center w-10 h-10 dark:hover:bg-blue-970 dark:bg-blue-800 dark:bg-opacity-25 bg-blue-50 group hover:bg-blue-700 transition duration-300 ease-linear" rel="nofollow"> <svg width="17" height="17" viewBox="0 0 17 17" fill="none" xmlns="http://www.w3.org/2000/svg"> <path class="stroke-current transition duration-300 ease-linear dark:text-blue-950 dark:text-blue-950 text-blue-700 group-hover:text-white" d="M7.13998 9.96507L8.8695 8.23767M7.13998 9.96507C5.87212 11.2337 0.538685 9.00354 1.49647 6.18315C2.45426 3.36275 12.943 -0.0813092 15.0632 2.04019C17.1834 4.16169 13.7258 14.6899 10.9196 15.6121C8.11337 16.5342 5.87212 11.2337 7.13998 9.96507Z" stroke="currentColor" stroke-width="1.56405" stroke-linecap="round" stroke-linejoin="round"></path> </svg> </a> </li> </ul> </div> </div> <div class="grid md:grid-cols-12 grid-cols-1 xl:gap-20 gap-7"> <div class="md:col-span-6"> <div class="relative flex flex-col md:items-start md:mx-0 mx-auto w-fit-content items-start"> <h3 class="text-xl relative font-bold text-biscay-700 mb-4 z-10"> About Mini Learn </h3> <span class="z-0 w-4 h-4 flex absolute bg-blue-700 opacity-20 top-0 rounded-full -left-2"></span> </div> <div class="flex relative "> <span class="md:flex hidden absolute top-1 right-0 w-1 h-1 mt-2 bg-blue-700 opacity-30 rounded-full"></span> <div class="md:mr-2 md:text-left text-left"> <p class="text-base mb-2 md:w-12/12 w-full md:text-left text-left leading-7 dark:text-gray-920 text-biscay-650 "> Mini Learn is a platform for short and important programming tutorials in various languages. By using the short tutorials on Mini Learn, you can gain skills in different fields. Our goal is to help the programming community find and solve their questions and errors in programming through Mini Learn. </p> </div> </div> </div> <div class="md:col-span-3"> <div class="relative flex md:items-start md:mx-0 mx-auto w-fit-content items-start"> <h3 class="text-xl relative font-bold text-biscay-700 mb-4 z-10"> Most Popular Tutorials </h3> <span class="z-0 w-4 h-4 flex absolute bg-blue-700 opacity-20 top-0 rounded-full -left-2"></span> </div> <ul class="flex flex-col md:items-start items-start"> <li class="mb-3 last:mb-0 flex items-start"> <span class="md:flex hidden w-1 h-1 ml-2 mb-1 dark:text-gray-940 bg-blue-700 opacity-30 rounded-full"></span> <a class="text-base text-biscay-650 dark:text-gray-920 transition transform dark:hover:text-blue-450 hover:text-dark-700 duration-200 ease-linear" href="https://mini-learn.net/Javascript"> JavaScript Tutorial </a> </li> <li class="mb-3 last:mb-0 flex items-start"> <span class="md:flex hidden w-1 h-1 ml-2 mb-1 bg-blue-700 opacity-30 rounded-full"></span> <a class="text-base text-biscay-650 dark:text-gray-920 transition transform dark:hover:text-blue-450 hover:text-dark-700 duration-200 ease-linear" href="https://mini-learn.net/Laravel"> Laravel Tutorial </a> </li> <li class="mb-3 last:mb-0 flex items-center"> <span class="md:flex hidden w-1 h-1 ml-2 mb-1 bg-blue-700 opacity-30 rounded-full"></span> <a class="text-base text-biscay-650 dark:text-gray-920 transition transform dark:hover:text-blue-450 hover:text-dark-700 duration-200 ease-linear" href="https://mini-learn.net/Wordpress"> WordPress Tutorial </a> </li> <li class="mb-3 last:mb-0 flex items-center"> <span class="md:flex hidden w-1 h-1 ml-2 mb-1 dark:text-gray-940 bg-blue-700 opacity-30 rounded-full"></span> <a class="text-base text-biscay-650 dark:text-gray-920 transition transform dark:hover:text-blue-450 hover:text-dark-700 duration-200 ease-linear" href="https://mini-learn.net/React"> React Tutorial </a> </li> </ul> </div> <div class="md:col-span-3"> <div class="relative flex md:items-start md:mx-0 mx-auto w-fit-content items-center"> <h3 class="text-xl relative font-bold dark:tew text-biscay-700 mb-4 z-10"> Contact Us </h3> <span class="z-0 w-4 h-4 flex absolute bg-blue-700 opacity-20 top-0 rounded-full -left-2"></span> </div> <ul> <li class="flex items-center justify-between mb-7 last:mb-0"> <span class="flex items-center"> <span class="relative ml-3"> <i class="flex w-5 h-5 bg-blue-700 opacity-20 rounded-full"></i> <svg class="dark:text-blue-950 text-blue-980 absolute -top-1 -left-2" width="17" height="17" viewBox="0 0 17 17" fill="none" xmlns="http://www.w3.org/2000/svg"> <path d="M7.14715 9.85653L8.80585 8.19987M7.14715 9.85653C5.93121 11.0732 0.81617 8.93438 1.73474 6.22947C2.6533 3.52456 12.7125 0.221534 14.7459 2.25616C16.7793 4.29079 13.4633 14.3879 10.772 15.2723C8.08068 16.1567 5.93121 11.0732 7.14715 9.85653Z" stroke="currentColor" stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round"></path> </svg> </span> <span class="text-sm font-medium text-dark-550 dark:text-gray-920"> Telegram : </span> </span> <a class=" text-biscay-650 dark:hover:text-blue-450 font-medium text-sm transition ltr transform hover:text-dark-700 duration-200 ease-linear" href="https://t.me/mini_learn_ir">@mini_learn_ir</a> </li> </ul> <div class="flex items-center mt-9 space-x-reverse space-x-2"> </div> </div> </div> <div class="relative mt-2 flex md:justify-start justify-center"> <p class="dark:bg-dark-890 dark:text-gray-970 bg-background-main-rgb pl-3 text-center relative z-10 inline-block text-xs text-chambray-400"> All rights to the products and content of this site belong to Mini Learn, and any copying of the content for educational purposes is permitted. :) </p> <span class="absolute border-t border-1 top-2/4 trasform translate-y-2/4 dark:border-blue-980 border-chambray-100 w-full z-0 right-0"></span> </div> </div> </div> </footer><script> document.addEventListener("DOMContentLoaded", function () { const lazyloadImages = document.querySelectorAll("img.lazyload"); const lazyLoad = (entries, observer) => { entries.forEach(entry => { if (entry.isIntersecting) { const img = entry.target; if (img.dataset.src) { img.src = img.dataset.src; // Move data-src to src img.removeAttribute("data-src"); // Remove data-src attribute observer.unobserve(img); // Stop observing the loaded image } } }); }; const observer = new IntersectionObserver(lazyLoad, { root: null, // Use the viewport as root rootMargin: "0px", // No margin outside the viewport threshold: 0.1 // Start loading when 10% of the image is visible }); lazyloadImages.forEach(img => observer.observe(img)); }); </script> <script> $(document).ready(function(){ $("#pageloader").hide(); $(".faq_answer").on("click",function(){ $("#"+$(this).attr("data-question")).toggleClass("hidden"); $("#"+$(this).attr("id")+" .svgFaq").toggleClass("!bg-blue-700 !text-white"); $("#"+$(this).attr("id")+" .arrowfaq").toggleClass("!text-blue-700 !rotate-0 "); $("#"+$(this).attr("id")+" h3").toggleClass("!text-blue-700 "); }); $("#phoneMenuBtn").on("click",function(){ $("#phoneMenu").addClass("!left-0").removeClass("!-left-full"); }); $(".showmorebtn").on("click",function(){ $("#"+$(this).attr('data-id')).addClass("!h-fit-content").removeClass('h-96'); $(".showmorebtnfalse"+$(this).attr('data-id')).show(); $(this).hide(); }); $(".showmorebtnfalse").on("click",function(){ $("#"+$(this).attr('data-id')).removeClass("!h-fit-content").addClass('h-96');; $(".showmorebtn"+$(this).attr('data-id')).show(); $(this).hide(); }); $("#main,#closeMenu").on("click",function(){ $("#phoneMenu").removeClass("!left-0").addClass("!-left-full"); }); }); $(document).ready(function(){ $("#submit").on("click",function(e){ const phone = $("#phone").val(); const author = $("#author").val(); const comment = $("#comment").val(); const id = $("#comment_post_ID").val(); const type = $("#post_type").val(); function toEnglishNumber(strNum) { var pn = ["۰", "۱", "۲", "۳", "۴", "۵", "۶", "۷", "۸", "۹"]; var en = ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9"]; var an = ["٠", "١", "٢", "٣", "٤", "٥", "٦", "٧", "٨", "٩"]; var cache = strNum; for (var i = 0; i < 10; i++) { var regex_fa = new RegExp(pn[i], "g"); var regex_ar = new RegExp(an[i], "g"); cache = cache.replace(regex_fa, en[i]); cache = cache.replace(regex_ar, en[i]); } return cache; } const formattedPhone = toEnglishNumber(phone); const regEx = /^09\d{9}$/g; if (author === "") { setBorderStyle($("#author")); return; }else{ removeBorderStyle($("#author")); } if (formattedPhone.length !== 11 || !formattedPhone.match(regEx)) { setBorderStyle($("#phone")); return; }else{ removeBorderStyle($("#phone")); } if (comment === "") { setBorderStyle($("#comment")); return; }else{ removeBorderStyle($("#comment")); } $("#submit").val("در حال ارسال"); var settings = { "url": "https://mini-learn.net/setComment", "method": "POST", "timeout": 0, "mode": "no-cors", "cache": "no-cache", "headers": { 'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content') }, "data": JSON.stringify({ "phone": formattedPhone, "author": author, "comment": comment, "id":id, "post_type":type, }), }; $.ajax(settings).done(function (response) { $("#submit").val("ثبت نظر شما"); if(response=="copy"){ $("#msgComment").html(" نظر شما قبلا ثبت شده است و پس از تایید نمایش داده میشود").attr("style","color:red"); $("#phone").val(""); $("#author").val(""); $("#comment").val(""); $(".selectReshte").removeClass('active'); $("#reshteComment").val(""); setTimeout( function() { $("#msgComment").attr("style","").html(""); }, 8000); }else{ $("#msgComment").html(" دیدگاه شما با موفقیت ثبت شد و پس از تایید نمایش داده میشود").attr("style","color:green"); $(".selectReshte").removeClass('active'); $("#reshteComment").val(""); $("#phone").val(""); $("#author").val(""); $("#comment").val(""); } }); }); function setBorderStyle($element) { $element.attr("style", "border:1px solid #ff5858"); } function removeBorderStyle($element) { $element.attr("style", ""); } }); </script> </body> </html>