My App

SPA Fallback

SPA Fallback

SPA fallback ensures client-side routes work seamlessly by serving your index.html for non-API, non-asset GET requests.

How It Works

dexSpaFallback mounts a catch-all GET route (*) and serves the file at indexHtmlPath.

It automatically bypasses fallback for:

  • Non-GET HTTP methods
  • /api/* (API endpoints)
  • /assets/* (static asset requests)
  • /__dev/* (dev reload SSE requests)
  • Any path containing . (requests for files with extensions like .ico, .png, .json)
  • Requests with an Accept header that explicitly does not accept text/html or */*

Usage

import { dexSpaFallback } from '@dex/server'

app.use(dexSpaFallback({
  indexHtmlPath: 'build/index.html' // in production, or 'web/public/index.html'
}))

Options

OptionTypeDefaultDescription
indexHtmlPathstring(required)Path to the entry index.html file to serve for SPA routing.

Request Handling Example

RequestdexSpaFallback Behavior
GET /Serves index.html
GET /aboutServes index.html
GET /users/123Serves index.html
GET /api/healthBypassed (handled by API routes)
GET /assets/client.jsBypassed (handled by dexAssetsRoute)
GET /__dev/reloadBypassed (handled by dexDevReloadRouter)
GET /favicon.icoBypassed (contains .)
POST /submitBypassed (non-GET method)

See Also

On this page