Skip to content

feat: Handle static file serve using fastify#86

Merged
x42en merged 2 commits intomasterfrom
develop
Feb 25, 2026
Merged

feat: Handle static file serve using fastify#86
x42en merged 2 commits intomasterfrom
develop

Conversation

@x42en
Copy link
Owner

@x42en x42en commented Feb 25, 2026

This pull request introduces static file serving to the IOServer framework, enabling seamless support for single-page applications (SPAs) and static assets. The new functionality allows serving a directory of static files (such as a built frontend) and provides an optional SPA fallback mode, ensuring client-side routing works as expected. Comprehensive tests have been added to verify all static serving scenarios.

Key changes:

Static file serving and SPA fallback support

  • Added a rootDir option to IOServerOptions and constructor, enabling serving of static files from a specified directory using @fastify/static. If the directory does not exist, static serving is safely disabled with a warning. (src/IOServer.ts, package.json) [1] [2] [3]
  • Introduced a spaFallback option (default: true) that, when enabled with rootDir, serves index.html for any unmatched route—allowing SPA client-side routers to function correctly. If disabled, unmatched routes return the standard 404 JSON response. (src/IOServer.ts) [1] [2] [3]

Core implementation and API changes

  • Registered @fastify/static plugin conditionally in the setupPlugins method and updated the not-found handler to support SPA fallback or default 404s as appropriate. (src/IOServer.ts) [1] [2]
  • Updated the version to 2.1.0 to reflect the new feature. (package.json, src/IOServer.ts) [1] [2]

Testing

  • Added a comprehensive test suite (IOServer.static.test.ts) covering all static file serving behaviors: constructor handling, static file and asset serving, SPA fallback logic, API route priority, and default 404 handling when static serving is disabled. (tests/unit/IOServer.static.test.ts)

These changes provide robust, production-ready static file serving for SPAs and static assets, with full test coverage and backward compatibility for existing API-only usage.

@x42en x42en self-assigned this Feb 25, 2026
@x42en x42en merged commit cad96e4 into master Feb 25, 2026
3 of 4 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant