In this Astro JS tutorial, we will see how to create an Astro JS site from spinning up a local development server right up to getting the app into the cloud and hosted. Although we will build a fairly simple site, we will see some important Astro features like hydration in action. We will also take advantage of Astro’s Islands architecture to add Svelte and React components. This is just a quick introduction to Astro, so be sure to explore the related posts and videos listed at the bottom of the page to boldly go further!
We build a simple app which shows YouTube videos in Svelte and React, as well as an Astro Markdown component. We will do that in three steps. First, is the setup, which should take a couple of minutes. Then we will add our own content to the minimal app we created in the previous step. Finally, we will see how to deploy the app to the cloud.
You spin up a new Astro project from the command line. First, you need to create a new directory for the project, then change into that directory and spin up Astro. We will be using Svelte and React in our Astro JS tutorial app. We can configure Astro to use these as part of the setup. Let’s do that now, running these commands:
init astro step, select Empty. Then in the
astro add step, enter yes so Astro configures React and Svelte for
you. We disable telemetry here, skip that step if you want to keep telemetry enabled. After the
final step, you should have a dev server running. The Terminal will give you the URL for it, so
you can open it in your browser. Typically, it will be running on
http://localhost:4321/. However, Astro will automatically find a free port if there is already something running on
Open up the browser using the given URL and you should just see the word ‘Astro’ in black text. We will add our own content to this minimal app next.
Just before moving on, open up
astro.config.mjs in the project
root folder. This is Astro’s main config file. You will see the setup tool has imported the
necessary Svelte and React integration packages and added them to the config for you.
In this section, we are going to build out our app, and learn a little about the structure of an Astro project in the process. We will also see how to add vanilla CSS styling in Svelte.
Astro routing works similar to NextJS, Remix and Deno Fresh. Astro generates pages from files in
src/routes. So the file you
saw in the browser is generated from the content in
src/routes/index.astro. Let’s look at that file next.
Replace the content in the file with this:
We have a fair bit in here. Broadly, the file has three sections. The first is the front matter in
In the first line, we import our global stylesheet. We will create that file in a moment. We also
add the components which we will use to add video here. This might be new if you are coming from
in the following section. In our case, we are just importing items which we will use in the main
The main markup (lines
30) looks a lot like HTML, with a
body section. As well as regular HTML elements, we use our ReactVideo and SvelteVideo components.
The final section contains styles. The styles in this file will be automatically scoped just to the content of this file. Our app will not work for the moment; we need to add some new content we reference in the front matter first.
client:load directive in lines
we tell Astro to hydrate our components. Because of this, when we click the buttons in the app,
We will put our components in a new
src/components directory. You
might notice though that the import statement mentions
~components. This is just an alias for
src/components which we will use for
convenience. For the alias to work, we need to update the
tsconfig.json file in the project root folder. Let’s do that:
You can add any other aliases that might make sense for the project you are working on. Note we
only add these for convenience, they are optional and as an example we could instead have written
import '../styles/styles.css'. I prefer the alias, as well as looking neater, on larger projects where you might have to
traverse multiple directories to get to your file, the syntax is more manageable.
Speaking of styles, let’s add our global styles. Astro works with SCSS, Tailwind and other
styling frameworks. To keep this project simple, and also because modern CSS is now quite
powerful, we stick to vanilla CSS. Create a
src/styles folder and
inside, add a
styles/css file with the following content:
src/styles/styles.css — click to expand code.
58 you will notice we have some self-hosted fonts. We will download those to our project shortly.
At the bottom of the file you will see we have some style for our React component. We take a
different approach for the Svelte component, taking advantage of in-built scoped styles, similar
to what we have for the
Next, we can paste in the React code. Create a
src/components/react folder and then create a Video.jsx file with the following content inside:
The final component we need to add is the Svelte one. Create the
Video.svelte file in a new
src/components/svelte folder, with this content:
As well as our Svelte and React components displaying video, we have a video embedded in Markdown.
Let’s add the source. The app should work again once we have this in place. Create a
src/siteContent folder and inside, make a
video.md file, with this content:
We mentioned that we are using self-hosted fonts above. For the hosting to work, we need to
include the fonts in our repo, so our host can serve them. Download the Roboto Font in Regular, 400 and 700 . Extract the zip and then create a new
fonts folder in the
project’s public folder. Drop the two unzipped files in that folder. The public folder is
for anything which we do not need Astro (or Vite, under the hood) to process. As well as fonts,
web manifest files for PWA and favicons fall into this category.
We won’t optimize fonts here, just to get finished a little quicker. There is a nice video which focusses on self-hosted fonts in Astro together with optimization. If you are interested in optimization, do have a look. You can save 80% on some fonts files, especially where, for example, you only use the 900 weight font in titles.
The app should be working just fine now, with a nice Roboto sans serif font and all the colours. Try pressing the buttons below the React and Svelte components to check they work. You should notice the background colour change.
The next step is to build the site locally to check it is all working as expected. Run these
commands to build and preview the site (stop the dev server with Ctrl
If all is well, commit the code to a git repo and upload it to your GitHub or GitLab account, so
we can host it as a static site. You might notice your site gets built to the
dist directory in your project. There is no need to include this in your repo, as your host will
generate the site there for you.
It is worth adding a
.nvmrc file to the project root folder,
whichever host you are using. This will let the host know which version of node to use. We will go
for the Astro minimum node version, which is
18.14.1 at the time
Although we have used
pnpm in this tutorial to build the site, for
maximum compatibility, in the cloud use
npm run build as your
build command. We just mentioned that Astro outputs projects to the
dist directory, so on your host console, set the build output directory or publish directory to
Here are screenshots for Netlify and Cloudflare Pages which should help you out. Other services will be similar. Select the Astro preset if your host
has one, then just check the build command and output / publish directory is
In this post, we have run through the pipeline for building a static Astro site. We have seen:
- how to spin up a new Astro project with Svelte and React integrations;
- how you can add global CSS styles, local scoped styles and style React components with plain CSS; and
- configuration for deploying your static Astro site to the cloud.
The Astro JS tutorial code is in the Rodney Lab GitHub repo . You can also try it on Stackblitz .
I hope you found this article useful and am keen to hear how you will the starter on your own projects as well as possible improvements.
- Astro should work on any host able to build static sites with node. We have seen more detailed build instructions for Netlify and Cloudflare Pages, though you should also be able to get a hassle-free build on Render, Vercel and other similar services.
- Astro lets you define import aliases, which are essentially syntactic sugar. They let you use an absolute alternative path in your import statements. Because this path is absolute rather than the default relative paths, it does not need to be updated if you move your source file from one folder to another. On top, the syntax is cleaner. We have seen how to define your own custom aliases in the tsconfig.json file.
- We have seen a few different ways to add styling in Astro. We can add a global stylesheet and import this into Astro pages with the regular ES Module import syntax. For less complex apps, we can add React component styling here. On top, we can add locally scoped styles in a script tag at the bottom of an Astro page. As this is also a Svelte feature, we can style Svelte component in this way too. Using these styling methods, Astro is able to optimize our CSS.
Have you found the post useful? Would you prefer to see posts on another topic instead? Get in touch with ideas for new posts. Also, if you like my writing style, get in touch if I can write some posts for your company site on a consultancy basis. Read on to find ways to get in touch, further below. If you want to support posts similar to this one and can spare a few dollars, euros or pounds, please consider supporting me through Buy me a Coffee.
Finally, feel free to share the post on your social media accounts for all your followers who will find it useful. As well as leaving a comment below, you can get in touch via @askRodney on Twitter and also askRodney on Telegram . Also, see further ways to get in touch with Rodney Lab. I post regularly on Astro as well as SvelteKit. Also, subscribe to the newsletter to keep up-to-date with our latest projects.