Overview
Document is a special component for managing document-level elements like <html>, <head>, <body>, and their contents. It provides different factory constructors for various document manipulation scenarios.
Factory Constructors
Document() - Base Document
<html>, <head> and <body> tags.
Rendered as the
<title> element in <head>.Set as the
lang attribute on the <html> element.Rendered as the
<base href="..."> element. Automatically normalized to start and end with /.Rendered as a
<meta charset="..."> element in <head>.Rendered as a
<meta name="viewport" content="..."> element.Additional meta tags rendered as
<meta name="key" content="value"> elements.CSS styles rendered in a
<style> element inside <head>.Additional components rendered inside
<head>.The component rendered inside the
<body> element.Document.template() - Template Document
.template.html file from the filesystem and attaches the provided child component to that template.
Defines which template file to load:
web/<name>.template.html.CSS selector defining where to attach the child component in the loaded template.
The component to attach to the template.
Document.html() - HTML Attributes
<html> element.
This can be used at any point in the component tree and is supported both on the server during pre-rendering and on the client.
Attributes to attach to the
<html> element.An optional key for the component.
Can be used multiple times. Deeper or latter mounted components will override duplicate attributes from other
Document.html() components.Document.head() - Head Elements
<head> of the document.
Any children are pulled out of the normal rendering tree and rendered inside a special section of the <head> element. This is supported both on the server during pre-rendering and on the client.
Rendered as the
<title> element.Meta tags rendered as
<meta name="key" content="value"> elements.Additional components rendered inside
<head>.An optional key for the component.
Document.head() are overridden using this system:
- Elements with an
idoverride other elements with the sameid <title>and<base>elements override other<title>or<base>elements<meta>elements override other<meta>elements with the samename
Can be used multiple times. Deeper or latter mounted components will override duplicate elements from other
Document.head() components.Document.body() - Body Attributes
<body> element.
This can be used at any point in the component tree and is supported both on the server during pre-rendering and on the client.
Attributes to attach to the
<body> element.An optional key for the component.
Can be used multiple times. Deeper or latter mounted components will override duplicate attributes from other
Document.body() components.Common Use Cases
Basic App Setup
Dynamic Page Titles
SEO Metadata
Dark Mode Toggle
External Stylesheets and Scripts
Using Custom Template
Platform Differences
The base
Document() constructor and Document.template() are only available on the server. On the client, only Document.html(), Document.head(), and Document.body() are available.Important Notes
The
Document.head(), Document.html(), and Document.body() factory constructors can be used anywhere in the component tree, including within child components deep in the hierarchy.Related Components
- StatelessComponent - Base class for stateless components
- Router - For managing navigation and routes