Skip to main content

SPA Mode

Learn how to build Single Page Applications (SPAs) with React Router, client-side only rendering, and no server component.

Overview

SPA Mode allows you to build React Router applications that run entirely in the browser, without server-side rendering. This is ideal for:
  • Static hosting (GitHub Pages, Netlify, Vercel)
  • Applications without server requirements
  • Progressive migration from client-only apps
  • Electron or Tauri desktop applications

Configuring SPA Mode

Disable SSR in your React Router config:

Client-Only Rendering

With SPA mode, all rendering happens in the browser:

Loaders in SPA Mode

Loaders run in the browser, fetching data from APIs:

Actions in SPA Mode

Actions also run client-side:

Authentication

Handle authentication client-side:

Environment Variables

Access client-side environment variables:

Static Deployment

Build and deploy to static hosts:
Configure server for client-side routing:

Loading States

Show loading indicators for client-side navigation:

Error Handling

Handle API errors gracefully:

Offline Support

Add service worker for offline functionality:

Local Storage Cache

Cache API responses locally:

Progressive Web App

Add PWA capabilities:

SPA vs SSR Trade-offs

SPA Advantages:
  • Simpler deployment (static hosting)
  • No server infrastructure needed
  • Lower hosting costs
  • Works offline with service workers
SPA Disadvantages:
  • Slower initial page load
  • SEO challenges (requires careful meta tag management)
  • No server-side data fetching benefits
  • Larger initial JavaScript bundle

Best Practices

  1. Code splitting - Use lazy loading to reduce initial bundle size
  2. Cache API responses - Reduce network requests
  3. Handle offline - Provide graceful degradation
  4. Optimize assets - Compress images and minify code
  5. Use CDN - Serve static assets from edge locations
  6. Monitor performance - Track load times and bundle sizes
  7. Security - Never expose secrets in client code
  8. Meta tags - Set appropriate tags for social sharing and SEO