komal.nakrani
Browse Themes
ThemesCategoriesDocsBlogTips & Tricks
Tips & Tricks

Astro Tips & Tricks

Short answers to specific Astro questions, each with code you can paste into your project.

Recent Articles

Canonical URLs in Astro

Add canonical URLs in Astro with new URL(Astro.url.pathname, Astro.site) in your head component. Handles query strings, trailing slashes, pagination and syndication.

Read ➔

Astro Build Speed: Faster Builds in CI

Speed up Astro builds in CI by caching node_modules/.astro (processed images and the content data store), using npm ci with a cache, and tuning image work.

Read ➔

Every Astro CLI Command You'll Actually Use

The Astro CLI commands that matter: astro dev, build, preview, check, add, sync and info, with the flags worth knowing in Astro 7 and when to reach for each.

Read ➔

Conditional Rendering in Astro

Conditional rendering in Astro uses JavaScript expressions in the template: && for show/hide, ternaries for either/or, and early variables for complex cases.

Read ➔

A Custom 404 Page in Astro

Create src/pages/404.astro and Astro builds a 404.html that Netlify, Vercel, Cloudflare Pages and GitHub Pages serve for missing URLs. Template and testing.

Read ➔

Point a Custom Domain at Your Astro Site

Set up an Astro custom domain: add it in your host, create the DNS records, pick www or apex, redirect the other, then update site in astro.config.mjs and redeploy.

Read ➔

Astro define:vars: Passing Values to CSS

Astro's define:vars passes frontmatter values into a style or script tag as CSS custom properties or variables. How it works, when to use it, and what it costs.

Read ➔

Fragment and set:html in Astro

Use set:html to render an HTML string in Astro, and Fragment to group elements or fill a slot without a wrapper. Examples, escaping rules and safety notes.

Read ➔

Lazy-Load Astro Islands with client:visible

Lazy load Astro islands with client:visible so a component's JavaScript downloads only when it scrolls into view. rootMargin, client:idle and when not to defer.

Read ➔

Newsletter Signup Forms in Astro

Add a newsletter signup form to Astro: post a plain HTML form to your email provider on a static site, or use an Astro Action to call its API on the server.

Read ➔

Prefetch Links in Astro

Enable Astro prefetch with prefetch: true in astro.config.mjs and data-astro-prefetch on links. The hover, tap, viewport and load strategies, and when to use each.

Read ➔

Add Reading Time to Astro Posts

Add reading time to Astro posts with a small remark plugin that writes minutesRead into frontmatter, then read it from render() in your post layout.

Read ➔

Redirects in Astro: Config vs Host

Astro redirects three ways: the redirects option in astro.config.mjs, your host's _redirects file for real 301s on static sites, and Astro.redirect() on demand.

Read ➔

Related Posts in Astro Without a Plugin

Show related posts in Astro by scoring other entries on shared tags with getCollection(), sorting by score and date, and rendering the top three. No plugin needed.

Read ➔

Astro Responsive Images with widths and sizes

Make Astro responsive images with the layout prop or explicit widths and sizes on Image and Picture, so phones download small files and retina screens stay sharp.

Read ➔

robots.txt for Astro Sites

Add robots.txt to Astro as a static file in public/ or generate it from an endpoint using site. Examples for production, staging and blocking AI crawlers.

Read ➔

Using Sass in Astro

Use Sass in Astro by installing the sass package and adding lang="scss" to style tags or importing .scss files. Partials, global variables and when to skip Sass.

Read ➔

Add a Sitemap to Astro in Two Minutes

Install @astrojs/sitemap, set site in astro.config.mjs, and Astro writes sitemap-index.xml on every build. Setup, excluding pages, lastmod and on-demand routes.

Read ➔

Auto-Generated Table of Contents in Astro

Build a table of contents in Astro from the headings array that render() returns: filter by depth, nest h3 under h2, and link to the auto-generated heading ids.

Read ➔

Astro trailingSlash and build.format

How Astro's trailingSlash and build.format settings work together, which combination to pick for your host, and how to keep links and canonical URLs consistent.

Read ➔

Fix: Could Not Find Sharp in Astro

Astro's 'Could not find Sharp' means the image library isn't installed for your platform. Install sharp, fix CI lockfiles, or use passthrough.

Read ➔

Fix: Duplicate Content Entry Slug in Astro

Astro's 'Duplicate content entry slug' means two files in one collection resolve to the same id. Rename one, fix duplicate slug frontmatter, or clean stray copies.

Read ➔

Fix: Image Not Found in Astro

Astro's 'Image not found' means the image path doesn't resolve. Check spelling, letter case, relative paths from the Markdown file, and where the file lives.

Read ➔

Fix: Invalid prerender Export in Astro

Astro's 'Invalid prerender export' means prerender isn't a literal true or false Astro can read at build time. Use export const prerender = false exactly.

Read ➔

Fix: Legacy Configuration Detected in Astro

Astro's 'Legacy configuration detected' names an astro.config.mjs option that moved or was removed. How to find the new location for common legacy keys.

Read ➔

Fix: Legacy Content Config File Found in Astro

Astro's 'Legacy content config file found' means src/content/config.ts must move to src/content.config.ts, with a loader on every collection. Steps and gotchas.

Read ➔

Fix: Local Images Must Be Imported in Astro

Astro's 'Local images must be imported' means Image got a file path string. Import the file, use image() in content collections, or import.meta.glob for many.

Read ➔

Fix: locals Must Not Be Reassigned in Astro

Astro's 'locals must not be reassigned' means code replaced context.locals with a new object. Set properties on it instead, and type them in src/env.d.ts.

Read ➔

Fix: MDX Integration Missing in Astro

Astro's 'MDX integration missing' means an .mdx file is rendered without @astrojs/mdx. Run npx astro add mdx, include mdx in glob patterns, and restart dev.

Read ➔

Fix: Missing Image Dimensions in Astro

Astro's 'Missing image dimensions' error hits remote images without width and height. Add both, use inferSize, or import the file if it lives in src.

Read ➔

Fix: No Matching Renderer Found in Astro

Astro's 'No matching renderer found' means a React, Vue, Svelte or Solid component is used without its integration. Add it with npx astro add, then restart.

Read ➔

Fix: Remote Image Is Not Allowed in Astro

Astro's 'Remote image is not allowed' means the image's domain isn't in image.domains or image.remotePatterns. Add the host to astro.config.mjs and rebuild.

Read ➔