The HTML <head> contains information needed to configure a page, such as its title, description, external stylesheets, and JavaScript.
In Essepage, you can write <head> inside a page file just as you would in a regular HTML document.
<head>
<title>About Us</title>
<meta
name="description"
content="Learn more about our company."
>
</head>
<main>
<h1>About Us</h1>
<p>Learn more about our company.</p>
</main>Essepage treats content enclosed in <head> as part of the final HTML document’s <head>. The remaining content is displayed inside <body>.
You therefore do not need to write <!doctype html>, <html>, and <body> in every page file.
When a Page First Loads
When you visit a page URL directly or refresh the browser, the complete HTML document is generated using SSR.
The page’s <head> is also generated at this time.
<head>
<title>About Us</title>
<meta name="description" content="Learn more about our company.">
<link rel="stylesheet" href="https://example.com/styles.css">
<script src="https://example.com/library.js"></script>
</head>The browser reads the <head> provided by the server and processes information such as:
- The page title displayed in the browser tab
- The page description and other metadata
- External stylesheets
- External JavaScript
- Icons and other resources
When a search engine or an external service accesses a page URL directly, the page’s <head> is also provided through SSR.
When Navigating with CSR
When you follow an internal site link and navigate using CSR, the browser does not reload the complete HTML document.
<a href="/about">About</a>Essepage updates the browser URL and the page content displayed inside <body>, but it does not rebuild the entire <head>.
The basic behavior is as follows.
| Item | Behavior during CSR navigation |
|---|---|
| Browser URL | Changes to the URL of the destination page |
| Page content | Updates to the content of the destination page |
<head> content | Retains the content loaded with the first page |
<title> | Updates to the title of the destination page |
| JavaScript environment | Preserves the existing environment |
The page title is updated for the destination page, but the entire <head>, including its <meta>, <link>, and <script> elements, is not reloaded.
Put Shared Resources in <head>
Stylesheets and JavaScript libraries used across the site should be included in <head>.
<head>
<link
rel="stylesheet"
href="https://example.com/styles.css"
>
<script src="https://example.com/library.js"></script>
</head>Shared resources are loaded when the first page opens and remain available during CSR navigation.
Libraries used throughout the site, such as Tailwind CSS and Alpine.js, can also be loaded from the shared <head>.
Loading external JavaScript only from the <head> of a particular page can produce different results depending on how the page is opened:
- The script loads when the page URL is visited directly.
- The script may not load when navigating from another page with CSR because
<head>is not updated.
Libraries required by multiple pages or after CSR navigation should therefore be placed in the shared <head>.
JavaScript that runs only on a particular page should be written below that page’s content.
<h1>Contact Us</h1>
<form id="contactForm">
<!-- Form fields -->
</form>
<script>
const form = document.querySelector("#contactForm");
form.addEventListener("submit", event => {
event.preventDefault();
});
</script> Using a Shared <head>
You do not need to repeat the same <head> in every page file.
Create /public/+head.essepage to apply a shared <head> to every page on the site.
/public/+head.essepage<head>
<link
rel="stylesheet"
href="https://example.com/styles.css"
>
<script src="https://example.com/library.js"></script>
</head>Because +head.essepage affects every page, use Apply Changes to All Pages after modifying it.
See Head Layout for more information about its required location and usage.
Using Different Metadata for Each Page
Although the structure of <head> can be shared across the site, each page should have its own title and description.
Using a Head Layout with <essepage-meta> lets you keep a shared <head> structure while providing different values for each page.
In the Head Layout, retrieve the values provided by the page.
<head>
<title>{{$ essepage-meta : title $}}</title>
<meta
name="description"
content="{{$ essepage-meta : description $}}"
>
</head>Specify the values in each page file.
<essepage-meta
title="About Us"
description="Learn more about our company."
></essepage-meta>
<h1>About Us</h1>When the page URL is visited directly, SSR generates a <head> containing that page’s title and description.
During CSR navigation, the page title is updated, but the entire <head>, including its <meta> elements, is not replaced.
Search engines and external services normally access a page URL directly, allowing them to read the page metadata generated through SSR.
See Metadata for detailed configuration instructions.
Creating Global State in <head>
The JavaScript execution environment is not recreated during CSR navigation.
You can use this behavior to create global state in <head> and preserve its values while navigating between pages.
<head>
<script>
window.siteState = {
menuOpen: false,
selectedTheme: "light"
};
</script>
</head>The stored values remain available when navigating through internal site links.
window.siteState.selectedTheme = "dark";If you use Alpine.js, you can also initialize a global Store in <head>. Its reactive state remains available during CSR navigation, making it easy to build an interactive site that shares state between pages.
However, refreshing the browser or visiting a page URL directly creates a new JavaScript execution environment and resets the global state.
See Client-Side State for more information about managing values between pages.
Summary of <head> Behavior
The <head> in Essepage works as follows:
- Visiting a page URL directly or refreshing it generates the complete
<head>. - Following an internal site link updates the page content inside
<body>using CSR. - During CSR navigation, the page title changes, but the complete
<head>is not reloaded. - Shared stylesheets and JavaScript libraries should be placed in the Head Layout.
- JavaScript used only by a particular page should be written with that page’s content.
- The JavaScript execution environment and global state can be preserved during CSR navigation.
Next Steps
Now that you understand how <head> works, continue with the following guides:
- Manage JavaScript state between pages: Client-Side State
- Apply a shared
<head>to every page: Head Layout - Set different titles and descriptions for each page: Metadata
- Review the basic behavior of CSR and SSR: CSR and SSR
- Apply shared file changes to the live site: Apply Changes to All Pages