--- url: "https://web.dev/articles/codelab-fix-sneaky-404" titulek: "Fix sneaky 404s | Articles | web.dev" h1: "Fix sneaky 404s Stay organized with collections Save and categorize content based on your preferences." popis: "In this codelab, learn how to track down a sneaky 404 that may prevent your page from being properly indexed." jazyk: "en" kanonicka: "https://web.dev/articles/codelab-fix-sneaky-404" drobecky: ["Articles", "Fix sneaky 404s"] zdroj: "live" wayback_snapshot: "" stazeno: "2026-09-14 00:03:48" znaku: 3313 slov: 560 kodovani: "iso-8859-2" sha256: "2941fdc686094580c34c7265f6c5d2d948761b29993c0aac8c8545f42debe914" --- Fix sneaky 404s Stay organized with collections Save and categorize content based on your preferences. Martin Splitt Single Page Apps can show different content without loading a new page. To do so, they use click handlers on links and the History API. The [History API](https://developer.mozilla.org/docs/Web/API/History) allows to manipulate the browser session history. This way we can update the URL when showing a different page (usually called a "view" in Single Page Apps). It also makes sure the browser's back button still works as expected. Take a look at the Single Page App in this codelab. It shows either cat or dog images and provides links to toggle between the two animals. It seems to work fine! Uncovering the sneaky 404 Unfortunately there is a subtle bug in the app. Let's take a look! To preview the site, press **View App**. Then press **Fullscreen** . Click the **Doggos** link. Notice how the URL changed. Reload the app. You get a page with "`Cannot GET /doggos`" on it—a sneaky 404. It is "sneaky", because the web app seems to work fine as long as you only click on links within it. It breaks when using the URLs in a new browser window or when refreshing the page. The issue is that the server does not know how to respond to a request for these URLs. The JavaScript code in our web app is using the History API to navigate between them, but the server does not know what to do with them. Whenever the server does not know what to do for a requested URL, it responds with the HTTP status code `404`. With this code the server says it hasn't found anything for the requested URL. Search engines will not index the URLs in this case, because a user would click on a search result and find the error message, but not the content they were looking for, such as the dog pictures. Fixing the server This project uses an [express.js](https://expressjs.com/) server written in JavaScript. Let's fix the server, so it responds with index.html and the single page app will take care of the rest. Click **Remix to Edit** to make the project editable. Select the `server.js` file. This file contains the server code. It sets up an express.js server and sends the content of index.html. The route setup in line 15 only serves the web app when requests go to the URL `/`. The server should also serve the other URLs we have created. Let's change this to serve all URLS, so it also works with additional URLs in the future. To do so, we can change the code starting at line 15 to this: `app.get('/*', function(request, response) { response.sendFile(__dirname + '/views/index.html'); });` The `/*` matches any URL and the server now responds with the web app in `index.html` for any given URL. To preview the site, press **View App**. Then press **Fullscreen** . Refreshing and opening the links in a new incognito window should now work as expected. Except as otherwise noted, the content of this page is licensed under the [Creative Commons Attribution 4.0 License](https://creativecommons.org/licenses/by/4.0/), and code samples are licensed under the [Apache 2.0 License](https://www.apache.org/licenses/LICENSE-2.0). For details, see the [Google Developers Site Policies](https://developers.google.com/site-policies). Java is a registered trademark of Oracle and/or its affiliates. Last updated 2018-11-05 UTC.