Skip to the indexed answers
Digital Marketing Index

usdigitalmarketingindex.com SEO

How should a single-page application signal a real HTTP 404 for a nonexistent route?

The server should return a 404 status for any path that does not correspond to real content, and where it cannot know that in advance, the application should either redirect to a URL that does return a 404 or add a noindex rule to its not-found view. A client-rendered single-page application can be deployed to serve the same HTML shell for every path with a 200 status, leaving the client-side router to decide what to show. For a path that matches nothing, the router displays a not-found message while the response has already said success. Google’s documentation names the problem and the two fallbacks: to avoid soft 404 errors with client-side routing, “Use a JavaScript redirect to a URL for which the server responds with a 404 HTTP status code”, or add a robots noindex meta tag to error pages using JavaScript (Google’s JavaScript SEO basics).

How SPAs return accidental 200

The standard deployment for a client-rendered application includes a catch-all rule: any request that is not a file is answered with the application shell.

# nginx: serve the shell for every unknown path
location / {
    try_files $uri /index.html;
}

With that rule, these three requests receive the same response.

GET /products/trail-runner        -> 200, index.html
GET /products/no-such-product     -> 200, index.html
GET /qwertyuiop                   -> 200, index.html

The router then renders a product page for the first and a not-found message for the others. A crawler receives 200 for all three. Every mistyped link, every removed product, and every invented address becomes a successful page that says not found.

Google’s documentation explains why the status matters: “Googlebot uses HTTP status codes to find out if something went wrong when crawling the page”, and a meaningful status code is how to tell it a page could not be found.

HTTP 404 strategy

The sound solution is for the server to know which routes exist.

Server-side or static rendering. If the server renders the route, it looks up the content first and returns 404 when there is none.

// Express-style handler; getProduct stands for the application's data lookup
app.get('/products/:slug', async function (req, res) {
  const product = await getProduct(req.params.slug);
  if (!product) {
    res.status(404).send(renderNotFoundPage());
    return;
  }
  res.status(200).send(renderProductPage(product));
});

A route manifest at the edge. For a purely client-rendered application, the server or CDN can hold a list of valid paths or patterns, serve the shell with 200 for those, and return 404 for everything else. For paths with dynamic segments, the edge can check the data source before answering.

A real 404 response should still carry a helpful page in its body: a clear message, navigation, and search. The status is for machines and the body is for people.

Fallback/noindex options

Where the server cannot determine validity, use the two strategies in Google’s documentation.

Redirect to a real 404. When the client discovers the content is missing, send the browser to a path for which the server returns 404.

fetch('/api/products/' + productSlug)
  .then(function (response) { return response.json(); })
  .then(function (product) {
    if (product.exists) {
      showProduct(product);
    } else {
      window.location.replace('/not-found');
    }
  });

The server must be configured so that /not-found itself answers with a 404 status.

Add noindex to the not-found view. If a redirect is not wanted, mark the error view so it is not indexed.

function showNotFound() {
  const meta = document.createElement('meta');
  meta.name = 'robots';
  meta.content = 'noindex';
  document.head.appendChild(meta);
  renderNotFoundView();
}

The trade-offs:

Option Status seen by clients Notes
Server returns 404 404 Correct for every client, with or without scripts
JavaScript redirect to a 404 URL 200, then 404 after scripts run Depends on rendering; the address in the browser changes
JavaScript noindex 200 Depends on rendering; the URL stays; clients that run no scripts see a normal page

Remove the tag when navigating away. In a client-side router, a noindex meta tag added for one view stays in the document until removed. If the visitor, or the renderer, moves to a valid route without a full reload, that route inherits the noindex.

Testing deep invalid paths

A QA case for nonexistent routes. Run it against production after each deployment.

Test Request Pass condition
Random top-level path /zz-not-a-page-123 404 status; or a 200 whose rendered HTML redirects to a 404 URL or carries noindex
Invalid item under a valid pattern /products/zz-not-a-product-123 Same
Valid pattern with extra segments /products/trail-runner/extra/segments Same
A removed item that once existed The URL of a deleted product 404 or 410, or a redirect to its replacement
A valid route /products/trail-runner 200, correct content, no noindex
A valid route reached after visiting an invalid one, without reload Navigate in the application No noindex left in the head
The not-found URL itself /not-found 404 status

Check the status from the command line:

$ curl -s -o /dev/null -w "%{http_code}n" https://www.example.com/zz-not-a-page-123

For the script-based options, the status alone will read 200. Use Search Console’s URL Inspection live test on an invalid URL and read the rendered HTML for the redirect’s result or the noindex tag.

Watch Search Console’s page indexing report for soft 404 entries afterward. A rise following a release points to a template that has stopped signaling correctly.

How Google comes to treat a 200 response as an error for ordinary pages, beyond the single-page application case, is explained in the guide to why a page returning HTTP 200 can be treated as a soft 404.

The order of preference: the server says 404. Failing that, redirect to something that does. Failing that, noindex the error view and clean up after it.

Link trail

Reader comments on this answer

Add your comment to this answer

Your email address will not be published. Required fields are marked *