Published
- 4 min read
Astro Practice
初めてAstroでサイトを作成
最短15分程度で作成からデプロイまで.
Astroでテンプレートを利用することでゼロからサイトを作成し、デプロイを行います。
以下、概要と作成手順について記述します。
Astroとは
Astroは、高速で軽量なウェブサイトを構築するための最新のJavaScriptフレームワークです。 静的サイト生成とサーバーサイドレンダリングを組み合わせ、優れたパフォーマンスと開発者体験を提供します。 コンポーネントベースのアプローチと、多様な統合オプションにより、柔軟で効率的な開発が可能です。
Variables
Local variables can be added into the HTML using the curly braces syntax:
---
const name = 'Astro'
---
<div>
<h1>Hello {name}!</h1>
<!-- Outputs <h1>Hello Astro!</h1> -->
</div>
Dynamic Attributes
Local variables can be used in curly braces to pass attribute values to both HTML elements and components:
---
const name = 'Astro'
---
<h1 class={name}>Attribute expressions are supported</h1>
<MyComponent templateLiteralNameAttribute={`MyNameIs${name}`} />
caution HTML attributes will be converted to strings, so it is not possible to pass functions and objects to HTML elements. For example, you can’t assign an event handler to an HTML element in an Astro component:
---
// dont-do-this.astro
function handleClick() {
console.log('button clicked!')
}
---
<!-- ❌ This doesn't work! ❌ -->
<button onClick={handleClick}>Nothing will happen when you click me!</button>
Instead, use a client-side script to add the event handler, like you would in vanilla JavaScript:
---
// do-this-instead.astro
---
<button id='button'>Click Me</button>
<script>
function handleClick() {
console.log('button clicked!')
}
document.getElementById('button').addEventListener('click', handleClick)
</script>
ressions
Dynamic HTML
Local variables can be used in JSX-like functions to produce dynamically-generated HTML elements:
---
const items = ['Dog', 'Cat', 'Platypus']
---
<ul>
{items.map((item) => <li>{item}</li>)}
</ul>
Astro can conditionally display HTML using JSX logical operators and ternary expressions.
---
const visible = true
---
{visible && <p>Show me!</p>}
{visible ? <p>Show me!</p> : <p>Else show me!</p>}
Dynamic Tags
You can also use dynamic tags by setting a variable to an HTML tag name or a component import:
---
import MyComponent from './MyComponent.astro'
const Element = 'div'
const Component = MyComponent
---
<Element>Hello!</Element>
<!-- renders as <div>Hello!</div> -->
<Component />
<!-- renders as <MyComponent /> -->
When using dynamic tags:
- Variable names must be capitalized. For example, use
Element, notelement. Otherwise, Astro will try to render your variable name as a literal HTML tag. - Hydration directives are not supported. When using
client:*hydration directives, Astro needs to know which components to bundle for production, and the dynamic tag pattern prevents this from working.
Fragments
Astro supports using either <Fragment> </Fragment> or the shorthand <> </>.
Fragments can be useful to avoid wrapper elements when adding set:* directives, as in the following example:
---
const htmlString = '<p>Raw HTML content</p>'
---
<Fragment set:html={htmlString} />
Differences between Astro and JSX
Astro component syntax is a superset of HTML. It was designed to feel familiar to anyone with HTML or JSX experience, but there are a couple of key differences between .astro files and JSX.
Attributes
In Astro, you use the standard kebab-case format for all HTML attributes instead of the camelCase used in JSX. This even works for class, which is not supported by React.
<div className="box" dataValue="3" />
<div class="box" data-value="3" />
Multiple Elements
An Astro component template can render multiple elements with no need to wrap everything in a single <div> or <>, unlike JavaScript or JSX.
---
// Template with multiple elements
---
<p>No need to wrap elements in a single containing element.</p>
<p>Astro supports multiple root elements in a template.</p>
Comments
In Astro, you can use standard HTML comments or JavaScript-style comments.
---
---
<!-- HTML comment syntax is valid in .astro files -->{/* JS comment syntax is also valid */}
HTML-style comments will be included in browser DOM, while JS ones will be skipped. To leave TODO messages or other development-only explanations, you may wish to use JavaScript-style comments instead.