Skip to content

Examples

Each example starts in a workspace where the plugin is initialised and @astrojs/check is installed.

For Astro 6 and newer.

Terminal window
nx g @geekvetica/nx-astro:application notes
apps/notes/src/content.config.ts
import { defineCollection } from 'astro:content';
import { glob } from 'astro/loaders';
import { z } from 'astro/zod';
const blog = defineCollection({
loader: glob({ pattern: '**/*.md', base: './src/content/blog' }),
schema: z.object({
title: z.string(),
date: z.coerce.date(),
}),
});
export const collections = { blog };
apps/notes/src/content/blog/first-post.md
---
title: First post
date: 2026-01-15
---
Hello from a content collection.
apps/notes/src/pages/blog/[id].astro
---
import { getCollection, render } from 'astro:content';
import Layout from '../../layouts/Layout.astro';
export async function getStaticPaths() {
const posts = await getCollection('blog');
return posts.map((post) => ({ params: { id: post.id }, props: { post } }));
}
const { post } = Astro.props;
const { Content } = await render(post);
---
<Layout title={post.data.title}>
<h1>{post.data.title}</h1>
<Content />
</Layout>

check generates the types of the collection first, through sync:

Terminal window
nx run-many -t check build -p notes

The post is in dist/apps/notes/blog/first-post/index.html.

init has already installed the @astrojs/node adapter.

Terminal window
nx g @geekvetica/nx-astro:application api
apps/api/astro.config.mjs
import { defineConfig } from 'astro/config';
import node from '@astrojs/node';
export default defineConfig({
output: 'server',
adapter: node({ mode: 'standalone' }),
outDir: '../../dist/apps/api',
});
apps/api/src/pages/api/time.ts
import type { APIRoute } from 'astro';
export const GET: APIRoute = () => Response.json({ now: new Date().toISOString() });
Terminal window
nx build api
HOST=127.0.0.1 PORT=4321 node dist/apps/api/server/entry.mjs

http://127.0.0.1:4321/api/time answers with the current time.

To run the server outside the workspace, write a package.json and a lock file next to it:

Terminal window
nx build api --generatePackageJson --outputPath=../../dist/apps/api

Share code between apps builds a library with an Astro component and uses it in an app. An app that is generated after the library gets its alias automatically:

Terminal window
nx g @geekvetica/nx-astro:application shop
apps/shop/src/pages/index.astro
---
import Button from '@acme/ui/Button.astro';
---
<Button href="/cart">Open the cart</Button>
Terminal window
nx build shop

Give the sites a common tag:

Terminal window
nx g @geekvetica/nx-astro:application marketing --tags=type:site
nx g @geekvetica/nx-astro:application handbook --tags=type:site

Build all of them, or those a change affects:

Terminal window
nx run-many -t build -p tag:type:site
nx affected -t build

Give each a fixed port, so that they can run side by side:

apps/handbook/astro.config.mjs
import { defineConfig } from 'astro/config';
export default defineConfig({
output: 'static',
server: { port: 4332 },
outDir: '../../dist/apps/handbook',
});
Terminal window
nx run-many -t dev -p marketing,handbook
nx-astro 3.2.0 · MIT Developed by