Overview
TheMainHead component manages the HTML <head> section of the page, including meta tags, fonts, favicon, and critical theme initialization scripts. It ensures proper SEO, performance, and prevents flash of unstyled content.
Props
The page title displayed in browser tabs and search results.
Meta description for SEO and social sharing.
Usage
Real-World Example
In BaseLayout
Fromsrc/layouts/BaseLayout.astro:
Features Included
View Transitions
Enables Astro’s View Transitions for smooth page navigation:Meta Tags
Essential meta tags for SEO and responsiveness:Favicon
Links to the site’s SVG favicon:Google Fonts
Preconnects and loads the site’s fonts for optimal performance:- Public Sans: Body text (400, 700 weights, plus italic)
- Rubik: Brand/heading text (500, 600 weights)
Critical Theme Script
An inline, blocking script that prevents flash of wrong theme on page load:Theme Initialization Logic
The critical script performs these tasks:- Check localStorage: Looks for saved theme preference
- Check System Preference: Falls back to
prefers-color-scheme: dark - Apply Theme Class: Adds
theme-darkclass to<html>if needed - Set up Observer: Watches for theme changes and persists to localStorage
Performance Optimizations
Font Loading
- Preconnect: Establishes early connections to Google Fonts
- display=swap: Shows fallback fonts immediately while custom fonts load
- Subset Loading: Only loads required font weights
Inline Critical Script
The theme script is inlined (is:inline) rather than loaded as a separate file because:
- It must execute synchronously before first paint
- It’s tiny (< 1KB)
- Prevents flash of wrong theme
- No network request needed
CSS Import
Imports the global stylesheet:Integration with Theme System
MainHead works together with:- ThemeToggle Component: User-facing theme switcher
- Global CSS: Theme variables (
theme-darkclass selectors) - MutationObserver: Syncs theme changes to localStorage
SEO Considerations
The component sets essential SEO meta tags:- Page title (appears in Google search results)
- Meta description (appears in search result snippets)
- Open Graph description (for social media sharing)
- Viewport meta tag (for mobile responsiveness)
Customization
To customize the MainHead component:- Change Fonts: Update the Google Fonts URL with your preferred fonts
- Add Open Graph Tags: Include additional OG tags for richer social sharing
- Add Analytics: Insert tracking scripts (Google Analytics, Plausible, etc.)
- Update Favicon: Change the favicon href to your icon file
The default title uses the special characters “JUΛN ROCCIΛ” with Greek lambda (Λ) characters for visual style.