Skip to main content

Command Palette

Search for a command to run...

Reviewing Astro: The Framework for Lightning-Fast Static Websites with JavaScript Flexibility

Updated
7 min read
Reviewing Astro: The Framework for Lightning-Fast Static Websites with JavaScript Flexibility

In the ever-evolving world of web development, finding the right framework for building high-performance websites can be daunting. Enter Astro—a rising star in the realm of static site generators. Today, I’ll walk you through my personal journey of using Astro to build my new portfolio website, highlighting its unique features, practical benefits, and why it stands out among modern web frameworks.

Why I Chose Astro for My Portfolio Website

When building a new portfolio website, I wanted a fast, efficient, and straightforward solution that could handle both static content and modern interactive elements. My choice landed on Astro for its Server-Side Rendering (SSR) capabilities and unique performance optimization. Here’s what drew me in:

  • Server-Side Rendering for Optimal Performance: By default, Astro renders pages on the server and delivers static HTML to the client’s browser, minimizing the need for heavy JavaScript. This makes it ideal for simple static websites that benefit from enhanced speed, SEO, and better overall performance.

  • Server Islands: Next-Level Interactivity: Unlike some other frameworks that require heavy bundles of JavaScript for interactivity, Astro’s Server Islands feature allows developers to specify exactly which JavaScript is sent to the client. This flexibility is perfect for components like buttons, forms, or more complex interactive elements that enhance the user experience without slowing down the entire website.

However, adopting any new framework comes with a learning curve. I faced some challenges understanding Astro’s .astro file types and how to distinguish between server-side code and client-side loaded JavaScript. Once I got past the initial hurdle, though, the benefits became apparent. Astro’s ability to deliver blazing-fast, static-first content while allowing selective interactivity is what truly makes it stand out.

Astro vs. Next.js: Why I Preferred Astro for This Project

A natural question might be: Why not use another popular framework, such as Next.js? After all, Next.js offers Partial Prerendering, which can achieve similar goals in terms of performance and flexibility. Here’s why I found Astro to be a better fit for my specific needs:

  • Self-Hosting Flexibility: While Next.js’ Partial Prerendering is a powerful tool, it requires deploying to Vercel and using their CDN to unlock its full capabilities. This can pose challenges for developers who prefer or need to self-host their applications. Astro, on the other hand, makes self-hosting seamless without losing any of its SSR benefits or flexibility.

  • Server Islands for Flexible Self-Hosting: Astro’s Server Islands approach offers out-of-the-box support for dynamic components, even when self-hosting. This unique capability made it an obvious choice for my self-hosted portfolio project, allowing me to maintain flexibility while delivering exceptional performance.

In short, Astro’s simplicity and power align perfectly with the needs of static websites that require a touch of dynamic functionality.

Using Multiple Frameworks with Astro

Astro shines when you need to integrate multiple technologies into a single website. For my project, I utilized React for a Dark Mode selector component, showcasing how easily Astro allows you to add client-side JavaScript components within a primarily static site.

Real-Life Example: React Component for Dark Mode

Here’s the simplified code I used to add a Dark Mode toggle:

// src/components/ThemeSwitch.tsx

import React, { useEffect, useState } from 'react';

const ThemeSwitch: React.FC = () => {
  const [darkMode, setDarkMode] = useState<string>('system');

  // Apply the dark mode based on the selected preference
  const applyDarkMode = (mode: string) => {
    const root = document.documentElement;
    if (mode === 'system') {
      const prefersDark = window.matchMedia('(prefers-color-scheme: dark)').matches;
      root.classList.toggle('dark', prefersDark);
    } else {
      root.classList.toggle('dark', mode === 'dark');
    }
  };

  // Handle changes in the select dropdown
  const handleChange = (event: React.ChangeEvent<HTMLSelectElement>) => {
    const selectedMode = event.target.value;
    setDarkMode(selectedMode);
    applyDarkMode(selectedMode);
    localStorage.setItem('darkMode', selectedMode);
  };

  // Initialize the dark mode setting on component mount
  useEffect(() => {
    const storedMode = localStorage.getItem('darkMode') || 'system';
    setDarkMode(storedMode);
    applyDarkMode(storedMode);
  }, []);

  // Listen for system dark mode changes if 'system' mode is selected
  useEffect(() => {
    if (darkMode === 'system') {
      const mediaQuery = window.matchMedia('(prefers-color-scheme: dark)');
      const handleChange = () => applyDarkMode('system');
      mediaQuery.addEventListener('change', handleChange);
      return () => mediaQuery.removeEventListener('change', handleChange);
    }
  }, [darkMode]);

  return (
    <div>
      <label htmlFor="darkModeSwitch">Theme:</label>
      <select id="darkModeSwitch" value={darkMode} onChange={handleChange}>
        <option value="system">System</option>
        <option value="dark">Dark</option>
        <option value="light">Light</option>
      </select>
    </div>
  );
};

export default ThemeSwitch;

Astro makes it easy to include this React component as an isolated piece of client-side JavaScript. The rest of the site was built using TypeScript and Astro components, but it remains static HTML thanks to SSR, ensuring optimal performance. This integration showcases the flexibility that Astro provides for incorporating modern UI functionality seamlessly.

Performance Improvements and Practical Benefits

Although I didn’t measure specific load times, the performance improvements when using Astro were immediately noticeable. By reducing the amount of JavaScript sent to the client and focusing on delivering static HTML, the pages loaded faster, resulting in a better user experience and improved SEO rankings. Here are some key benefits:

  • Reduced JavaScript Bloat: Unlike some frameworks that send a large JavaScript bundle regardless of the content, Astro ensures that only essential JavaScript is loaded on the client side. This means fewer resources to load, resulting in faster page loads and lower data usage for visitors.

  • Improved User Experience: Faster loading times translate to a smoother user experience, reducing bounce rates and keeping visitors engaged. This is particularly important for portfolio websites, where first impressions matter.

  • Better SEO: Since search engines prioritize fast, static pages, Astro’s approach to SSR gives websites built with it a competitive edge in search rankings.

Ideal Use Cases for Astro

From my experience, Astro excels at building the following types of websites:

  • Static Information Websites: Think of portfolios, landing pages, blogs, and documentation sites. The combination of speed, simplicity, and static HTML makes these websites incredibly fast while still allowing for selective interactivity.

  • Interactive Components Where Needed: By enabling the integration of JavaScript frameworks like React, Svelte, and Vue, Astro provides the flexibility needed for websites that require dynamic content and interaction without sacrificing performance.

On the other hand, if you’re developing complex, stateful applications like SaaS platforms or e-commerce sites with heavy data requirements, a more robust framework like Next.js might be a better fit. Astro’s SSR and Server Islands shine when simplicity, speed, and efficient client-side interactivity are the priorities.

Real-World Examples: Dark Mode Toggle and Command Palette

In my portfolio site, I used React to create both a Dark Mode toggle and a Command Palette component. These examples highlight how Astro handles client-side interactivity while maintaining the speed and efficiency of a static site:

  • Dark Mode Toggle: This component allows users to switch between light and dark themes on the website. It’s a lightweight React component that loads only when needed, thanks to Astro’s Server Islands functionality.

  • Command Palette: This feature lets users interact with key functions on the site through a custom command interface. Like the Dark Mode toggle, it uses client-side code without compromising the static rendering of other content.

By leveraging Astro's ability to load JavaScript selectively, these components enhance user experience without bloating the overall website.

Challenges and Limitations of Astro

While my experience with Astro was mostly positive, it’s important to acknowledge some potential limitations for developers considering this framework:

  • Initial Learning Curve: Understanding .astro file types, separating server-side and client-side code, and mastering the framework’s conventions can be challenging for newcomers. However, once you get past the initial learning phase, development becomes much smoother.

  • Not Ideal for Large, Complex Apps: If you’re building a complex, stateful application with heavy data requirements, Astro may not offer the level of complexity and built-in tools found in frameworks like Next.js or Angular.

That said, for static websites with dynamic components, Astro’s simplicity, flexibility, and performance benefits make it a compelling option.

Final Recommendations

For developers seeking to build simple, fast, and interactive static websites, Astro is a game-changer. Whether you're a beginner looking to create a quick portfolio or an experienced developer building a high-performance landing page, Astro offers unmatched flexibility and performance. Its ability to integrate JavaScript, Node.js, React, and other modern tools without sacrificing speed makes it a standout choice in the crowded field of web development frameworks.

Feel free to check out my portfolio and see Astro in action! 🚀

Click here to view my Portfolio Website

def engage_with_blog(reaction):
    if reaction == 'happy':
        leave_comment()
    elif reaction == 'loved':
        share_with_friends()
    elif reaction == 'amazed':
        react_with_emoji('😲')
    else:
        print('Thanks for reading!')

More from this blog

T

The Code Operation

16 posts

Haya, I'm Surgical a multifaceted professional specializing in IT Support, Web Design, System Management, Software Development, and Hosting Solutions.