next.config.mjs, which determines whether pages are rendered at runtime or generated ahead of time.
Application Hosting
Choose Application Hosting (Server-Side Rendering) when your application requires server-side logic or dynamic behavior that static files cannot provide. SSR is ideal when your site includes:- Personalized user experiences, such as dashboards and profiles.
- Real-time or frequently changing data that must be fetched on every request.
- Dynamic routes that cannot be known ahead of time, making static pre-rendering impractical.
- Authentication-dependent content, where access and rendering vary based on the user’s session or permissions.
Configuration
To configure your Next.js application for use with Sevalla:-
Enable the
output: 'standalone'setting in yournext.config.jsfile. This option generates an optimized, self-contained production build that includes only the minimal files needed to run your application. - Use Next.js Image Optimization and configure the appropriate image loader to ensure efficient and high-quality asset delivery.
-
Use opt-in caching with the
use cachedirective for dynamic content. - Enable compression and set appropriate caching headers to improve performance.
Containerization
Dockerfile
The build for Dockerfiles is fully customizable. The following is an example Dockerfile for Next.js: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_VERSION environment 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 a Next.js 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 Next.js application, we recommend the following best practices:- Enable the CDN for all production applications to ensure faster global delivery and reduced latency.
- Use the Next.js
<Image>component to take advantage of built-in image optimization and responsive delivery. - Purge the CDN cache after deploying critical updates to guarantee that users receive the latest content.
- Next.js automatically versions built assets in
_next/static/, this ensures that static assets are uniquely hashed and safe to cache indefinitely. - Organize static files in the
public/directory, allowing the CDN to efficiently serve commonly accessed assets.
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 Next.js application, we recommend the following best practices:- Use the
use cachedirective for opt-in caching, avoiding implicit caching behaviors. - Select appropriate
cacheLifeprofiles based on how frequently your content changes. - Use
updateTag()in Server Actions to immediately update cached content while maintaining read-your-writes consistency. - Use
revalidateTag()with the requiredcacheLifeprofile to refresh cache selectively. - Set appropriate
Cache-Controlheaders for API routes to balance speed with accuracy. - Combine Edge Caching with the CDN for a comprehensive, high-performance caching strategy.
- Monitor cache efficiency using Cloudflare headers (e.g.,
cf-cache-status) to track hit rates. - Cache individual functions for more granular control over which parts of your application are cached.
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 Next.js 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 in_next/static/), 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.Opt-in caching with use cache
Caching individual functions
You can cache specific functions instead of entire components:API routes with edge caching
Cache invalidation APIs
Next.js 16 introduces new cache invalidation APIs:revalidateTag()- Now requires cacheLife profile:
updateTag()- New server actions-only API:
refresh()- New server actions-only API:
revalidatePath()- Still available:
Healthchecks and graceful shutdown
Ensure your application remains available during deployments by implementing health checks and graceful shutdown strategies:- 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.
- Finish in-flight requests before terminating processes.
- Set a shutdown timeout (recommended: 30 seconds).
- Close connections cleanly (e.g., database pools, Redis) to prevent resource leaks.
- Use a custom server if advanced shutdown control is needed.
- Test deployments in staging with monitoring scripts to validate health checks and shutdown behavior.
Basic health check
Separate readiness and liveliness health checks
Basic graceful shutdown
Multi-tenancy
Sevalla fully supports multi-tenancy. You can build multi-tenant Next.js 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 Next.js 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.
Next.js 16 multi-tenant implementation
Extract tenant from subdomain:Next.js 16 renamed
middleware.ts to proxy.ts and the exported function to proxy().Custom domains per tenant
Allow tenants to use their own domains (e.g.,customdomain.com → tenant data).
Map custom domains to tenants:
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 fully static Next.js site on Sevalla, make sure to include the following configuration in yournext.config.mjs file:
output: 'export'generates fully static HTML, CSS, and JS files for deployment to any static hosting. This generates all built assets to theoutfolder by default, unless you override this setting in thenext.config.mjsfile.images.unoptimized: truedisables Next.js image optimization, which is required for pure static exports.trailingSlash: trueensures all routes include a trailing slash, which helps maintain a consistent URL structure and meet static hosting requirements.
Redirects
To apply custom redirect rules, add a_redirects file containing your redirects to your repository’s root directory. Sevalla will automatically parse the file’s contents and apply the custom redirect rules.