Skip to content
7 changes: 7 additions & 0 deletions .changeset/silly-colts-doubt.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
'@aziontech/presets': minor
'@aziontech/unenv-preset': patch
'@aziontech/builder': patch
---

feat: add nitro preset
1 change: 0 additions & 1 deletion packages/builder/src/bundlers/esbuild/esbuild.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ export default {
platform: 'browser',
mainFields: ['browser', 'module', 'main'],
target: 'es2022',
keepNames: true,
allowOverwrite: true,
loader: {
'.js': 'js',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -178,9 +178,18 @@ function handleNodeJSGlobals(build: PluginBuild, getAbsolutePath: (moving: strin
const BUNDLER_POLYFILL_RE = /^@aziontech\/builder\/polyfills\/.+/;
const prefix = path.resolve(getAbsolutePath('../'), '_global_polyfill-');

const dotNotationKeys = new Set(['import.meta.url']);

if (inject['import.meta.url']) {
build.initialOptions.define = build.initialOptions.define ?? {};
build.initialOptions.define['import.meta.url'] = JSON.stringify('file://' + process.cwd() + '/index.js');
}

build.initialOptions.inject = [
...(build.initialOptions.inject ?? []),
...Object.keys(inject).map((globalName) => `${prefix}${globalName}.js`),
...Object.keys(inject)
.filter((globalName) => !dotNotationKeys.has(globalName))
.map((globalName) => `${prefix}${globalName}.js`),
];

// Resolve polyfills from @aziontech/unenv-preset
Expand Down
191 changes: 191 additions & 0 deletions packages/presets/docs/preset-nitro.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,191 @@
# Nitro Preset

This preset enables server-side rendering for Nitro-based applications on the Azion Platform.

## Prerequisites

- Node.js 18+
- A Nitro-based project (e.g. TanStack Start, Analog, or a custom Nitro app)
- Azion CLI installed globally

## Installation

Install the Azion presets package in your project:

```bash
npm install @aziontech/presets
```

## Configuration

### TanStack Start + Vite

Configure your `vite.config.ts` to use the Azion Nitro preset:

```typescript
import { tanstackStart } from '@tanstack/react-start/plugin/vite';
import { defineConfig } from 'vite';
import viteReact from '@vitejs/plugin-react';
import tailwindcss from '@tailwindcss/vite';

import { nitro } from 'nitro/vite';

// import the preset using createRequire to ensure correct path resolution
import { createRequire } from 'module';
const require = createRequire(import.meta.url);

export default defineConfig(() => {
return {
server: {
port: 3000,
},
resolve: {
tsconfigPaths: true,
},
plugins: [
tailwindcss(),
nitro({
// Use require.resolve to ensure the preset path is correctly resolved
preset: require.resolve('@aziontech/presets/nitro/preset'),
}),
tanstackStart(),
viteReact(),
],
};
});
```

### Generic Nitro App

For any Nitro-based project, configure your `vite.config.ts`:

```typescript
import { createRequire } from 'module';
const require = createRequire(import.meta.url);

export default defineNitroConfig({
preset: require.resolve('@aziontech/presets/nitro/preset'),
});
```

Or directly with the node_modules path:

```typescript
export default defineNitroConfig({
preset: './node_modules/@aziontech/presets/src/presets/nitro/custom/index.js',
});
```

## Project Setup

### 1. Link Your Project

Connect your project to Azion and select the Nitro preset:

```bash
azion link
```

When prompted, choose the **Nitro preset** from the available options.

## Development Workflow

### Preview Your Application

#### Build and Preview

Build your application and preview it locally:

```bash
azion build
azion dev
```

#### Skip Framework Build (Optional)

If you want to skip the framework build process and use existing build artifacts:

```bash
azion dev --skip-framework-build
```

This is useful when you've already built your application and want to quickly test the edge function behavior.

## Deployment

### Deploy to Azion Edge

Deploy your application directly from your local environment:

```bash
azion -t <personal-token>
azion deploy --local
```

This command will:

1. Build your application with the Nitro preset
2. Package the edge function
3. Deploy to Azion's edge network
4. Provide you with the deployment URL

## How It Works

After a successful build, Nitro outputs two directories:

- `.output/server/` — the server-side bundle (`index.mjs`) deployed as an Azion edge function
- `.output/public/` — static assets uploaded to Azion's storage bucket

At runtime, the Azion Nitro module:

1. Attaches the Azion runtime context (`env`, `ctx`) to each incoming request
2. Checks if the request targets a static asset
3. Serves static assets directly from storage
4. Forwards all other requests to Nitro's native fetch handler

## Features

The Nitro preset provides:

- **Server-Side Rendering**: Full SSR support via Nitro's native server
- **Edge Runtime**: Optimized for Azion's edge computing platform
- **Static Asset Handling**: Efficient static file serving from Azion storage with cache policy
- **API Routes**: Support for Nitro server API routes
- **WASM Support**: WebAssembly modules supported out of the box

## Troubleshooting

### Common Issues

**Build Errors**: Ensure the preset path in your config resolves correctly. Use `require.resolve` when possible to guarantee the path is valid.

**Deployment Failures**: Verify that the Azion CLI is authenticated and your project is properly linked.

**Runtime Errors**: Check that your application is compatible with edge runtime constraints (no Node.js-only APIs).

### Getting Help

For additional support:

- Check the [Azion Documentation](https://www.azion.com/en/documentation/)
- Contact Azion Support for platform-specific issues

## Example Project Structure

```
my-nitro-app/
├── vite.config.ts # Azion preset configuration (TanStack Start)
├── package.json # Include @aziontech/presets dependency
├── app/ # Application source
├── server/ # Nitro server routes and middleware
└── public/ # Static assets
```

## Next Steps

After successful deployment:

1. Test your application on the provided edge URL
2. Configure custom domains if needed

> **Note**: We are currently working on a Pull Request to the official Nitro repository to include an Azion preset natively. This will simplify the configuration process in future versions.
6 changes: 4 additions & 2 deletions packages/presets/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@
"./nuxt/*": "./src/presets/nuxt/nitro/*/index.js",
"./sveltekit": "./src/presets/svelte/kit/index.js",
"./sveltekit/cache": "./src/presets/svelte/kit/cache/index.js",
"./preset/*": "./dist/presets/*"
"./preset/*": "./dist/presets/*",
"./nitro/preset": "./src/presets/nitro/custom/index.js"
},
"author": "aziontech",
"license": "MIT",
Expand All @@ -32,7 +33,8 @@
"README.md",
"src/presets/next/*",
"src/presets/nuxt/nitro/*",
"src/presets/svelte/kit/*"
"src/presets/svelte/kit/*",
"src/presets/nitro/custom/*"
],
"dependencies": {
"@aziontech/config": "workspace:*",
Expand Down
1 change: 1 addition & 0 deletions packages/presets/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export * from './presets/hugo';
export * from './presets/javascript';
export * from './presets/jekyll';
export * from './presets/next';
export * from './presets/nitro';
export * from './presets/nuxt';
export * from './presets/opennextjs';
export * from './presets/preact';
Expand Down
Loading
Loading