To add additional elements to the <head> section only for the home page in Astro, you have a few options:
Option 1: Use conditional rendering in your layout
You can modify your layout to conditionally render additional head elements based on the current page path:
---
import Header from '../components/Header.astro';
import Footer from '../components/Footer.astro';
// Get the current page path
const currentPath = Astro.url.pathname;
const isHomePage = currentPath === "/";
const { pageTitle } = Astro.props;
---
<html lang="en">
<head>
<meta charset="utf-8" />
<link rel="icon" type="image/svg+xml" href="/favicon.svg" />
<meta name="viewport" content="width=device-width" />
<meta name="generator" content={Astro.generator} />
<title>{pageTitle}</title>
{isHomePage && (
<>
<!-- Additional head elements for home page only -->
<meta name="description" content="Welcome to my homepage" />
<link rel="stylesheet" href="/styles/home.css" />
</>
)}
</head>
<body>
<Header />
<main>
<slot />
</main>
<Footer />
<script>
import "../scripts/menu.js";
</script>
</body>
</html>
You can get the current page's path using Astro.url as shown in the example above Migrating from Gatsby.
Option 2: Use a named slot for head content
Another approach is to use a named slot in your layout that allows pages to inject additional head content: