SpaRoute class

Route for serving Single Page Applications (SPAs) with fallback support.

Combines static file serving with automatic fallback to a specified file when requested files don't exist, enabling client-side routing.

pod.webServer.addRoute(
  SpaRoute(
    Directory('web/app'),
    fallback: File('web/app/index.html'),
  ),
);

About caching

The fallback file is always served without a Cache-Control header when it answers a client side route, leaving caching behavior to client side heuristics. Caching the app shell would serve a stale application after a deploy, so neither cacheControlFactory nor the environment variable below applies to it.

The files in directory are served without a Cache-Control header as well, unless SERVERPOD_WEB_SERVER_SPA_CACHE_CONTROL is set. Set it when the SPA build embeds content hashes in its asset URLs, so that assets can safely be cached for a long time.

The SERVERPOD_WEB_SERVER_STATIC_CACHE_CONTROL environment variable used by StaticRoute does not apply to this route.

Inheritance

Constructors

SpaRoute(Directory directory, {required File fallback, CacheControlFactory? cacheControlFactory, CacheBustingConfig? cacheBustingConfig, String? host})
Creates a new SpaRoute.

Properties

asHandler → Handler
Returns this HandlerObject as a Handler.
no setterinherited
cacheBustingConfig → CacheBustingConfig?
Cache busting configuration for static files
final
cacheControlFactory → CacheControlFactory
Cache control factory for the files in directory.
final
directory → Directory
The directory containing static files
final
fallback → File
The fallback file (typically directory/index.html)
final
hashCode → int
The hash code for this object.
no setterinherited
host → String?
The virtual host this route will respond to.
finalinherited
methods → Set<Method>
The methods this route will respond to, i.e. HTTP get or post.
finalinherited
path → String
The suffix path this route will respond to.
finalinherited
runtimeType → Type
A representation of the runtime type of the object.
no setterinherited

Methods

call(Request req) → Future<Result>
Handles a call to this route, by extracting Session from request and forwarding to handleCall.
inherited
handleCall(Session session, Request request) → FutureOr<Result>
Handles a call to this route.
override
injectIn(RelicRouter router) → void
Adds this handler to the given router with Method.get and path '/' Override to add differently.
override
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
toString() → String
A string representation of this object.
inherited

Operators

operator ==(Object other) → bool
The equality operator.
inherited