SPA Deep Links Return 404 on Refresh: Fix the Server Fallback

Diagnose a single page app deep link 404 on refresh, configure an Nginx fallback, and verify that missing assets and API routes still return errors.

Published
Reading time
7 min read
SPA Deep Links Return 404 on Refresh: Fix the Server Fallback
On this page

A single page app deep link can return 404 on refresh even though the same route works when you navigate to it inside the app. The usual cause is that the browser requests the deep path from the server, but the server has no fallback to the app’s HTML entry point.

This guide uses React Router and Nginx to explain the difference, show an illustrative fallback configuration, and outline checks for routes, assets, and APIs. The configuration and code were not executed, and current React Router and Nginx documentation could not be consulted in this environment. Check them against the versions you deploy before applying the examples.

Key Takeaways

  • In-app navigation can update the URL without requesting a new HTML document; a refresh or direct visit requests that URL from the server.
  • For browser-history routing, configure the server to serve the app shell for frontend routes while keeping asset and API requests separate.
  • A server-side 404 and an application-rendered “not found” page are different. A fallback can return HTTP 200 even when the app displays a 404 page.
  • Test direct visits, refresh, browser history, missing assets, and missing API endpoints after deployment.

Suppose the app has a route at /users/42:

  1. A visitor loads /, receiving the HTML app shell and its JavaScript.
  2. They click an in-app link to /users/42. The client-side router updates the browser URL and renders the matching page without requesting another HTML document.
  3. They refresh. The browser now sends an HTTP request for /users/42 to the server.
  4. If the server looks for a file at that path and has no fallback, it can return 404 before the application starts.

A direct visit from a bookmark or a new tab has the same server-side requirement as a refresh. A successful fallback serves the app shell for the deep path; the router then reads the browser URL and decides what to render.

Advertisement

First, identify which layer returns the 404

Use the browser’s Network panel and reload the failing URL. Inspect the document request, not just requests for JavaScript or images.

What you seeLikely next step
The document request is 404 and contains the server’s error pageCheck the host’s rewrite or fallback configuration and deployed file path.
The document returns the app shell, but the app shows its not-found pageCheck the client route definitions, path spelling, and deployment base path.
The document returns the shell, but JavaScript or CSS requests failCheck asset URLs, build configuration, and the deployed asset directory.
An API request is 404Check the API route and proxy; an SPA fallback should not hide the error.

A client-side not-found page may still be delivered with HTTP 200 because the server sent the shell successfully. If a real HTTP 404 for unknown frontend routes is a requirement, use a server-aware routing or rendering strategy that can determine route validity before sending the response.

Example: React Router with an Nginx fallback

The following example assumes an existing React application using React Router’s BrowserRouter, Routes, and Route APIs. It also assumes the app is deployed at the domain root, the built entry point is /index.html, and generated static assets are requested under /assets/. Adapt the build and routes to your project; this is not a complete React project.

A minimal route component might look like this:

import { BrowserRouter, Link, Route, Routes } from "react-router-dom";

export default function App() {
  return (
    <BrowserRouter>
      <nav>
        <Link to="/">Home</Link>{" | "}
        <Link to="/users/42">User 42</Link>
      </nav>
      <Routes>
        <Route path="/" element={<h1>Home</h1>} />
        <Route path="/users/:id" element={<h1>User page</h1>} />
        <Route path="*" element={<h1>Page not found</h1>} />
      </Routes>
    </BrowserRouter>
  );
}

The wildcard route handles an unknown path after the app starts. It does not configure Nginx or guarantee an HTTP 404 response.

Separate frontend routes from assets and APIs

The following is an illustrative Nginx server block for an existing Nginx installation. It assumes the built files are in /srv/spa, an API service listens on 127.0.0.1:3000, and the API service receives paths beginning with /api unchanged. Configure the upstream and filesystem paths for your environment.

server {
    listen 8080;
    server_name localhost;
    root /srv/spa;

    # Fail rather than repeatedly falling back if the shell is missing.
    location = /index.html {
        try_files $uri =404;
    }

    # Keep API requests out of the frontend fallback.
    location = /api {
        proxy_pass http://127.0.0.1:3000;
    }

    location ^~ /api/ {
        proxy_pass http://127.0.0.1:3000;
    }

    # Missing assets should return 404, not index.html.
    location = /assets {
        return 404;
    }

    location ^~ /assets/ {
        try_files $uri =404;
    }

    # A missing favicon should also remain a 404.
    location = /favicon.ico {
        try_files $uri =404;
    }

    # Existing files are served normally; other paths get the app shell.
    location / {
        try_files $uri /index.html;
    }
}

Place the block in the appropriate Nginx configuration context for your installation. Ensure Nginx can read the build files and is configured to send appropriate MIME types for JavaScript and CSS. Validate the complete configuration with nginx -t before reloading Nginx using your installation’s normal procedure. Configuration syntax and behavior should be checked against the official Nginx documentation for your deployed version.

The separation depends on the URL layout. This example protects /assets/, /favicon.ico, and /api, but an unknown request such as /missing.js would reach the general fallback and could receive HTML. Put static files under a protected asset prefix, or add specific rules for every other static-file location your build uses. Do not assume the server can infer whether every arbitrary path is an asset or a frontend route.

Likewise, the API service must return its own appropriate status for unknown endpoints. If it is unavailable, the proxy may return an error rather than a 404; neither case should be replaced with the app shell.

Advertisement

Verify the deployed behavior

Run these checks against a local or authorized deployment. The commands below use the example Nginx port; replace the address if your deployment differs. These are verification steps, not observed results.

  1. In-app navigation: Open the home page and click the User 42 link. Confirm that the address changes to /users/42 and the user page renders.
  2. Direct navigation and refresh: Open /users/42 in a new tab, then refresh it. The document response should contain the app shell, and the router should render the user page.
  3. Back and forward: Navigate between the home and user pages, then use the browser’s Back and Forward controls. Confirm that both the address and rendered view follow the history.
  4. Unknown frontend path: Visit /not-a-real-page directly and refresh. The app should render its not-found page. Inspect the document status separately; with this fallback it will normally be the shell’s status, not necessarily 404.
  5. Missing asset: Request an asset that does not exist. Confirm the response is 404, not a successful HTML app-shell response.
  6. Missing API route: Request an endpoint that your API does not implement. Confirm that the response comes from the API or proxy, rather than the app shell.

For example, inspect status, headers, and body with:

curl -i http://127.0.0.1:8080/users/42
curl -i http://127.0.0.1:8080/not-a-real-page
curl -i http://127.0.0.1:8080/assets/definitely-missing.js
curl -i http://127.0.0.1:8080/api/definitely-missing

Also inspect the Network panel for failed script and stylesheet requests. A response with a successful status is not enough: a JavaScript request that receives the HTML shell can still prevent the app from loading.

Common Mistakes

  • Rewriting every request to the shell. This can turn missing assets and API errors into misleading HTML responses.
  • Adding only a client-side wildcard route. It cannot run if the server returns a 404 before loading the application.
  • Treating an app-rendered not-found page as an HTTP 404. Check the document response status independently.
  • Testing only through app links. They do not exercise the same initial document request as a direct visit or refresh.
  • Ignoring deployment under a subpath. A site hosted under a prefix needs matching server fallback paths, router base settings, and asset URLs. The root-deployment example above is not sufficient on its own.

Best Practices

  • Reserve clear URL namespaces for frontend routes, static assets, and APIs; document any exceptions such as /favicon.ico.
  • Test the production build through the actual hosting layer, including any reverse proxy or CDN that can change routing or cache responses.
  • Keep the deployed web root limited to files intended to be public, and review cache behavior for the HTML shell separately from versioned assets.
  • Consult the official documentation for your installed React Router and Nginx versions before deployment. Those sources could not be retrieved here, so the example’s current-version compatibility has not been verified.

Conclusion

A refresh 404 usually means the server does not know to serve the SPA shell for a frontend route. Add a targeted fallback, preserve real asset and API errors, and verify both browser navigation and HTTP responses in the deployed environment.

What would your domain sell for?

A free TidyURLs account unlocks the Domain Estimator. It prices a name from 191,011 real domain sales.