Skip to content

Add Router.fallback Handler property #197

Closed
1 of 1 issue completed
Closed

Description

@nielsenko

Problem to Solve

When configuring routes on Serverpod's WebServer, there's currently no way to specify a fallback handler for requests that don't match any defined routes. Say fx. you want to register a specific route /assets/x/y/z and pass all other paths to a SPA.

In Relic you can achieve this with:

final handler = const Pipeline()
  .addMiddleware(routeWith(router))
  .addHandler(fallbackHandler); // Handles unmatched requests

but this is not exposed in Serverpod, and cannot easily be exposed as it require the fallbackHandler to be known when building the pipeline, which currently happens to early.

Also it uses the soon-to-be deprecated Pipeline class.

Proposal

Add a Router.fallback property that allows developers to specify a fallback handler for requests that don't match any routes in the router, and can be set late.

Alternatives

  1. Implement backtracking so the router tries other routes if no match is found. However, this sacrifices the current performance guarantees. It also makes routing dependent on the order routes are added, which is annoying if/when composition is spread across multiple files.

  2. Workaround with manual route configuration, ie. define explicit routes for all paths that need handling:

pod.webServer.addRoute(
  AppConfigRoute(serverConfig: pod.config.apiServer),
  '/assets/packages/assets/json/config.json',
);

pod.webServer.addRoute(
  RouteStaticDirectory(serverDirectory: 'app/assets'),
  '/assets/**',
);

pod.webServer.addRoute(
  RouteSinglePageApp(serverDirectory: 'app', basePath: '/'),
  '/**',
);

This works but is convoluted and error-prone, requiring developers to manually manage all path hierarchies.

  1. Delay pipeline construction in Serverpod.

Sub-issues

Sub-issues

1 of 1 Issues completed
Loading 1 sub-issues
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

area: routingenhancement

Type

No type

Projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions