Fireship · @fireship

Astro in 100 Seconds

Uploaded July 15, 2021 · Length 2:17

Astro in 100 Seconds

Summary

Astro builds fast, SEO-friendly websites with JavaScript frameworks while shipping zero JavaScript to the browser.

  • astro
  • static website
  • javascript frameworks
  • zero js
  • static site generator

Transcript preview

The first 2 minutes of the transcript. Select a timestamp to watch that moment on YouTube.

  1. 0:00

    Astro, a tool for building static websites with your favorite JavaScript frameworks while

  2. 0:05

    shipping zero JavaScript to the browser.

  3. 0:08

    End users love a fast-loading website, but developers love to build them with big, heavy

  4. 0:12

    frameworks.

  5. 0:13

    Unfortunately, the key to better performance is to use less JavaScript.

  6. 0:17

    Astro allows you to build a site with your favorite framework or even multiple frameworks

  7. 0:22

    at the same time, and renders them to static HTML at build time.

  8. 0:26

    That sounds like a static site generator, but what's unique is that when you have a

  9. 0:29

    component that needs to be fully interactive with JavaScript, you have multiple strategies

  10. 0:33

    at your disposal to render and hydrate it client-side.

  11. 0:37

    Any component in your UI with the client load directive will be rendered with JavaScript

  12. 0:41

    on the initial page load like normal.

  13. 0:43

    But if the component is not a high priority, you can wait until the browser is idle before

  14. 0:47

    rendering it.

  15. 0:48

    Or if it's a component that's further down the page, you can hold off on rendering it

  16. 0:52

    until it's visible to the actual user with IntersectionObserver.

  17. 0:56

    Instead of thinking of a website as a single-page application where the JavaScript takes over

  18. 0:59

    everything think of it as an archipelago where your water is the static HTML and islands are self components that add interactivity when needed The end result is a website that is fast and search engine friendly but also has the full interactive power of a front framework To build your first project open an empty directory

  19. 1:17

    and run npm init astro. Inside the project, you'll find the pages directory. The file structure here

  20. 1:23

    defines the actual URL structure or routing configuration for your website. Each page is

  21. 1:28

    defined as an astro component, inside of which you can write static HTML, while also declaring

  22. 1:33

    components that might be written in React, Vue, or Svelte. At the top of a component, you'll notice

  23. 1:38

    triple dashes. This is where you can execute JavaScript on the server at build time. You may

  24. 1:43

    want to fetch data from your server or content management system, or local markdown files in

  25. 1:48

    your source code. And as an added bonus, it supports top-level await here, making it easier

  26. 1:52

    to write clean code. And finally, you can expect all of the essential features found in other static

  27. 1:57

    site generators, like RSS feeds, sitemaps, and so on.