Skip to main content

Deployment

Node.js deployment is the default Analog output preset for production builds.

When running npm run build with the default preset, the result will be an entry point that launches a ready-to-run Node server.

To start up the standalone server, run:

$ node dist/analog/server/index.mjs
Listening on http://localhost:3000

Environment Variables

You can customize server behavior using following environment variables:

  • NITRO_PORT or PORT (defaults to 3000)
  • NITRO_HOST or HOST

Built-in Presets

Analog can generate different output formats suitable for different hosting providers from the same code base, you can change the deploy preset using an environment variable or vite.config.ts.

Using environment variable is recommended for deployments depending on CI/CD.

Example: Using BUILD_PRESET

BUILD_PRESET=node-server

Example: Using vite.config.ts

import { defineConfig } from 'vite';

export default defineConfig({
plugins: [
analog({
nitro: {
preset: 'node-server',
},
}),
],
});

Deploying with a Custom URL Prefix

If you are deploying with a custom URL prefix, such as https://domain.com/ basehref you must do these steps for server-side-data-fetching, html markup and assets, and dynamic api routes to work correctly on the specified basehref.

  1. Update your vite.config.ts file.
export default defineConfig(({ mode }) => ({
base: '/basehref',
plugins: [
analog({
...(mode === 'production' ? {apiPrefix: 'basehref'} : {apiPrefix: 'basehref/api'}),
prerender: {
routes: async () => {
return [];
}
}
}),
],
  1. Update the app.config.ts file.

This instructs Angular on how recognize and generate URLs.

import { ApplicationConfig } from '@angular/core';
import { APP_BASE_HREF } from '@angular/common';

export const appConfig: ApplicationConfig = {
providers: [
[{ provide: APP_BASE_HREF, useValue: import.meta.env.BASE_URL || '/' }],
...
],
};
  1. HttpClient use injectAPIPrefix().
const response = await firstValueFrom(
this.httpClient.get<{ client: string }>(`${injectAPIPrefix()}/v1/hello`),
);
  1. In CI production build

Do not set VITE_ANALOG_PUBLIC_BASE_URL it should use what is in vite.config.ts. If VITE_ANALOG_PUBLIC_BASE_URL is present during build, ssr data will be refetched on server and client.

  # if using nx:
npx nx run appname:build:production
# if using angular build directly:
npx vite build && NITRO_APP_BASE_URL='/basehref/' node dist/analog/server/index.mjs
  1. In production containers specify the env flag NITRO_APP_BASE_URL.
NITRO_APP_BASE_URL="/basehref/"
  1. Preview locally:
npx vite build && NITRO_APP_BASE_URL='/basehref/' node dist/analog/server/index.mjs