7 Und 8 Klasse
astro

7 Und 8 Klasse

Jan 15, 2024
· STRUKTURIERTES LERNEN · 1 THEMEN ·

Getting Started with Astro

Astro is a modern static site generator that allows you to build fast websites with your favorite JavaScript frameworks. In this post, we’ll explore the basics of getting started with Astro.

Building a Blog with Astro

Why Choose Astro?

Astro provides several key benefits:

  • Zero JavaScript by default: Only ship JavaScript when you need it
  • Component Islands: Use any UI framework (React, Vue, Svelte, etc.)
  • Built-in performance: Automatic optimization and lazy loading
  • Developer experience: TypeScript support, hot reload, and more

Getting Started

To create a new Astro project, run:

npm create astro@latest

This will guide you through the setup process and create a new project with the latest Astro features.

Next Steps

Once you have your project set up, you can:

  1. Add pages in the src/pages/ directory
  2. Create components in src/components/
  3. Style your site with CSS or your preferred styling solution
  4. Deploy to platforms like Netlify, Vercel, or GitHub Pages

Stay tuned for more Astro tutorials!


1. Vite Dynamic Import Limitation

  • Vite (used by Astro) does not support fully dynamic import paths for assets. The import path must be statically analyzable, meaning you can’t use variables for the folder or filename in the import statement.
  • The warning:
    invalid import "../../content/blog/${post.slug}/${post.data.heroImage}". A file extension must be included in the static part of the import.
  • This means you cannot dynamically import images from the content folder using variables for both the folder and filename.

2. image 404 Error

  • This usually means the Astro Image integration is not able to find or process the image, often because the import failed or the path is not valid.

How to Fix This?

Astro’s Image Component requires static imports

You must import images statically at the top of your file, like:

import heroImage from '../../content/blog/post1/image-1.webp';

But you can’t do this dynamically for every post in a single [post].astro file.


Best Practice for Astro Content Collections + Image Optimization

Option 1: Use Public Directory for Hero Images

  • Place hero images in public/images/blog/
  • Reference them in frontmatter as /images/blog/image-1.webp
  • Use a regular <img src={post.data.heroImage} /> (not optimized, but works for dynamic content)

Option 2: Use Astro Image for Static Pages Only

  • If you want optimization, you must statically import the image in each page/component.
  • This is not possible for dynamic [post].astro routes unless you predefine all possible imports (which is not scalable).

Option 3: Use Markdown for Inline Images

  • For images inside markdown, just use the markdown syntax:
    ![Alt text](./image-1.webp)
    This works, but is not optimized.

What Should You Do?

If you want dynamic hero images for each post and are using content collections:

  • Use the public directory for hero images and reference them with an absolute path in frontmatter.
  • Use a regular <img src={post.data.heroImage} alt={post.data.title} /> in your layout or [post].astro.

If you want full optimization with Astro Image:

  • You must statically import the image, which only works for static pages, not dynamic [post].astro routes.

Example: Public Directory Approach

  1. Move your hero images to public/images/blog/
  2. In your markdown frontmatter:
    heroImage: "/images/blog/image-1.webp"
  3. In [post].astro:
    {post.data.heroImage && (
      <img src={post.data.heroImage} alt={post.data.title} style="margin-bottom:2rem;border-radius:8px;overflow:hidden;" />
    )}

Would you like me to update your project to use the public directory approach for hero images?
This is the most robust and scalable way for dynamic content in Astro right now.

Verfügbare Themen