Configuration
With Fastify, you can either compile TypeScript to JavaScript for production or run TypeScript directly.Compile to JavaScript (recommended for production)
- Sevalla will run
npm run buildfollowed bynpm start - Provides faster startup and better runtime performance
package.json:
Run TypeScript directly
- Sevalla will run
npm startusingtsxorts-node - Simpler setup with no build step.
package.json:
Required for Sevalla deployments
Ensure your Fastify server listens on all interfaces (not justlocalhost):
Containerization
Dockerfile
The build for Dockerfiles is fully customizable. We recommend the following best practices when using a Dockerfile for Fastify on Sevalla:- Use Alpine images - Smaller image size (~40MB vs ~900MB).
- Multi-stage builds - Separate build and runtime dependencies.
- Layer caching - Copy
package.jsonbefore the source code. - Security - Run as a non-root user in production.
- .dockerignore - Exclude unnecessary files.
Nixpacks
Nixpacks automatically detects the lock file and uses the corresponding package manager during deployment. You can also modify the node version through theNIXPACKS_NODE_VERSION environment variable.
We recommend the following best practices when using Nixpacks for Fastify on Sevalla:
- Commit lock files - Always include
package-lock.json,yarn.lock, orpnpm-lock.yamlfor deterministic, reproducible builds. - Specify your Node.js version - Use the
enginesfield inpackage.jsonor an.nvmrcfile to ensure consistent versioning. - Prefer
npm ci- More reliable and faster thannpm installin CI/CD environments. - Set
NODE_ENV=production- Helps optimize performance and reduce bundle size. - Test builds locally - Use the Nixpacks CLI to identify build issues before deploying.
- Keep configuration minimal - Allow Nixpacks to auto-detect your setup; only add a
nixpacks.tomlfile for advanced customization. - Optimize TypeScript builds - After compiling, run
npm prune --productionto remove dev dependencies. - Define a
startscript - Ensurepackage.jsonincludes astartscript for consistent server startup.
nixpacks.toml file:
Buildpacks
Buildpacks, automatically detects your project’s lock file and installs dependencies using the appropriate package manager. When using Buildpacks, you cannot directly modify the underlying build phases or control how dependencies are installed; Buildpacks determine this based on your project’s structure and configuration. However, you can influence the final runtime behavior by defining the appropriatestart script in your package.json file, which Buildpacks will use when launching your Fastify application.
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 Fastify application, we recommend the following best practices:- Enable the CDN for all production applications.
- 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.
- Use versioned URLs for static assets, for example
/static/app.v123.js.
Optimizing Fastify for CDN
Install the static files plugin
Static files with cache headers
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 Fastify application, we recommend the following best practices:- Set appropriate
Cache-Controlheaders in loaders to control caching behavior. - Combine edge caching with the CDN for a complete caching strategy.
Optimizing Fastify for edge caching
Cache-Control
With Sevalla’s Cloudflare integration,Cache-Controlheaders 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 Fastify 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, 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.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.
Basic health check
Graceful shutdown
Fastify’s built-inclose() method handles graceful shutdown automatically by:
- Stopping the server from accepting new connections.
- Waiting for existing requests to complete.
- Closing all connections.
- Cleaning up registered plugins and hooks.