astro.config.mjs file. Astro’s defineConfig.output setting can only be set to static or serveryou cannot mix both modes within a single project.
By default, Astro generates a fully static site (SSG). To use SSR, you must explicitly opt in by settingprerender: false. If any part of your application uses prerender: false, includes server-only code, or depends on dynamic rendering, you should use Application Hosting instead of Static Site Hosting.
Application Hosting
Choose Application Hosting (Server-Side Rendering) when your application requires server-side logic or dynamic behavior that static files cannot provide.Configuration
We recommend the following best practices for configuring Astro as an SSR on Sevalla:- Use Astro’s partial hydration (
client:*directives) only for components that need interactivity. - Leverage static generation (SSG) whenever possible for better performance.
- Use
output: "server"only for pages that truly require SSR. - Enable compression middleware in your Node adapter for smaller responses.
- Optimize images using Astro’s built-in
<Image />component. - Sanitize and validate environment variables, and never expose secrets using the
PUBLIC_prefix. - Use HTTPS in production for secure communication.
- Implement rate limiting for API routes to prevent abuse.
- Add an
/api/healthendpoint for health checks. - Configure structured logging for easier debugging and observability.
- Monitor build sizes and server response times to maintain performance.
astro.config.mjs file for deploying an Astro SSR app on Sevalla:
Containerization
Dockerfile
The build for Dockerfiles is fully customizable. The following is an example Dockerfile for Astro:Nixpacks
You can customize the Nixpacks build process by defining anixpacks.toml file and using Nixpacks-specific environment variables. This allows you to fine-tune how dependencies are installed, how your application is built, and which runtime settings are applied.
The following is an example nixpacks.toml configuration:
NIXPACKS_NODE_VERSIONenvironment variable.
Buildpacks
If you’re using Buildpacks, you cannot modify the underlying build phases directly or control dependencies. You must rely on the runtime environment that Buildpacks detects. You can influence the build process by adjusting thebuild script in your package.json. Buildpacks will run whatever command you specify under the build script. For example, the standard command used to compile an Astro application is:
CDN
Sevalla provides a premium, Cloudflare-powered CDN for Application Hosting at no additional cost. To get the most out of Sevalla’s CDN when deploying your Astro application, we recommend the following best practices:- Enable the CDN for all production applications.
- Use Astro’s
<Image>component for automatic image optimization and responsive delivery. - Set appropriate
Cache-Controlheaders on API routes to ensure proper caching behavior. - Purge the CDN cache after deploying critical updates to avoid serving stale content.
- Leverage Astro’s fingerprinted assets in the
_astro/directory for safe long-term caching. - Organize static files in the
public/directory for optimal delivery through the CDN. - Use
getStaticPathsfor dynamic routes to pre-render content at build time. - Combine static generation with incremental builds for frequently updated content.
Edge caching
Edge caching stores your Sevalla site cache on Cloudflare’s 260+ global data centers, delivering responses from the location nearest to each visitor for faster performance. To maximize the benefits of Sevalla’s Edge Caching for your Astro application, we recommend the following best practices:- Set appropriate
Cache-Controlheaders on server-rendered pages to control caching behavior. - Combine edge caching with the CDN to cover both dynamic and static assets.
- Monitor cache efficiency using the
cf-cache-statusheader returned by Cloudflare.
Cache-Control
With Sevalla’s Cloudflare integration, Cache-Control headers are respected at the edge, giving you precise control over how content is cached and served globally. The following are some common directives you can use in your Astro application:
public, s-maxage=3600- Caches the response on the CDN for 1 hour, improving performance for frequently accessed content.public, max-age=31536000, immutable- Ideal for versioned static assets (e.g., files indist/_astro/), allowing them to be cached for up to 1 year with no revalidation.private- Prevents CDN caching and ensures the response is only cached by the end user’s browser, for personalized or sensitive data.
Sevalla does not yet support the
stale-while-revalidate Cache-Control directive. To prevent unexpected caching behavior, we recommend not using this directive in your API or asset caching settings.API routes with edge caching
Edge-cache API responses by returning appropriate headers in your API routes. This speeds up API responses for repeated requests and reduces load on your origin server.Time-based revalidation
Use short-lived caching for content that updates frequently. This ensures frequently updated pages remain fresh and balances edge caching with dynamic content delivery.Health checks
Ensure your application remains available during deployments by implementing health checks:- Always implement health checks for production applications.
- Keep checks lightweight; responses should complete in under 1 second.
- Verify critical dependencies (e.g., databases, Redis) as part of the checks.
- Return 200 for degraded states to allow deployments to continue smoothly.
- Return 503 only for critical failures that require pod restarts.
- Close connections cleanly (e.g., database pools, Redis) to prevent resource leaks.
- Test deployments in staging with monitoring scripts to validate health checks.
Multi-tenancy
Sevalla fully supports multi-tenancy. You can build multi-tenant Astro applications using wildcard domains, allowing you to serve multiple tenants from separate subdomains efficiently and securely. Use the following best practices for multi-tenancy on Sevalla with your Astro application:- Use wildcard domains to serve subdomain-based tenants (e.g.,
tenant1.app.com). - Add custom domains individually for tenants who have their own domains.
- Cache tenant data to reduce database queries and improve performance.
- Validate tenant existence before rendering pages to prevent errors or unauthorized access.
- Isolate tenant data in the database using separate schemas or a
tenant_idcolumn. - Leverage free SSL certificates, which are automatically provided for both wildcard and custom domains.
Astro multi-tenant implementation
Below is a reference architecture for extracting tenant information from the hostname and injecting it into Astro’s request lifecycle. Extract tenant from subdomain (middleware)Custom domains per tenant
Tenants may want to use their own domains instead of subdomains. Sevalla supports this through domain mapping.Troubleshooting common SSR issues
Cannot use import.meta.env on the client
- Use the
PUBLIC_prefix for any environment variables that must be exposed to client-side code. - Keep all server-only variables unprefixed and ensure they are accessed only in server-side modules.
Route handler not found
- Confirm the file is located in the correct directory:
src/pages/. - Check that the file uses a supported extension:
.astro,.js, or.ts. - Verify that your
outputmode inastro.config.mjsmatches your intended deployment (e.g.,serverfor SSR).
Static Site Hosting
Static Site Generation (SSG) pre-renders all pages as static HTML files at build time, delivering fast and reliable performance. Use SSG when your site includes:- Content that changes infrequently, such as blog posts, documentation, or product catalogs.
- Pages that can be pre-rendered for all users without personalization.
- Requirements for maximum performance and minimal hosting costs.
- A global audience, as static pages benefit from fast CDN distribution.
Configuration
To deploy a static Astro site on Sevalla, setoutput: "static" in your astro.config.mjs file and ensure that no pages or components use prerender: false, which would force SSR.
/
Creating SSG pages
All.astro pages are automatically pre-rendered at build time in static mode:
Dynamic routes with SSG
Generate multiple pages from data usinggetStaticPaths():