Astro Components: A Guide to Building SEO-Friendly Astro Components
Astro is a powerful tool for creating web projects. When working with Astro, you’ll encounter files with the .astro
extension. In this article, we’ll explore Astro components and learn how to leverage them effectively.
Let’s start by examining a sample Astro component file, specifically the one included in the Minimal template:
1 | --- |
An Astro component is essentially an HTML file, but with a special feature called frontmatter. Frontmatter is denoted by two ---
lines at the top of the file and resembles the concept used in Markdown files. In Astro, frontmatter can be utilized to set page titles, post dates, and more.
It’s worth noting that frontmatter can be omitted if there is no content to be included. You can simply start the component with an HTML tag. However, the example above includes frontmatter as it serves as the default Astro example.
What sets Astro apart is its ability to incorporate JavaScript (or TypeScript) within components. In the previous example, the component contained html
, head
, and body
tags because it was a page component, designed to respond to specific routes and stored in the src/pages
directory.
In addition to HTML, Astro also supports Markdown files within the src/pages
directory. These files can be rendered as plain HTML, unless a specific layout is defined, which we will explore in another blog post.
Astro components can also be simpler, such as the following example demonstrating how to define variables in the frontmatter using JavaScript or TypeScript and utilizing them in the HTML using a JSX-like syntax:
1 | --- |
It’s important to note that this JavaScript code runs at build time, not in the browser. If you wish to include JavaScript code that runs in the browser, you can simply add a <script>
tag within the component:
1 | --- |
Astro components offer a wide range of capabilities beyond variable definition. You can import components or libraries, fetch data, and define variables that are accessible within the HTML.
Scoped CSS can be defined within any component using the <style>
tag:
1 | --- |
When you define a component in the src/components
folder, it becomes accessible throughout your Astro components. Simply import it and embed it wherever needed:
1 | --- |
Astro components don’t use traditional JSX syntax, but they offer several improvements. For instance, to modify the head
part of a page component, you can add a <head>
tag. Regular HTML comments (<!-- -->
) can be used instead of {/* */}
for commenting. HTML special characters can also be utilized, and HTML attributes no longer need to be camelCased (e.g., className
).
Astro also provides built-in components to enhance functionality. For instance, if you want to embed Markdown within your components, you can use the Markdown
component:
1 | --- |
Additionally, Astro offers the Code
component for syntax-highlighted code embedding directly at build time. If you prefer to use Prism (a popular library for syntax highlighting), you can utilize Prism
, which includes client-side JavaScript. Lastly, the Debug
component enables debugging of frontmatter code in the client-side by displaying variable contents within the page:
1 | --- |
By understanding the power and capabilities of Astro components, you’ll be able to create SEO-friendly web projects with ease.
Tags: Astro, Astro Components, JavaScript, TypeScript, HTML, SEO