I have a SPA and want serve a not found component along with the correct status code 404. So I included the following config.
[[redirects]]
from = "/*"
to = "/index.html"
status = 404
[[redirects]]
from = "/*"
to = "/index.html"
status = 200
Expected
Status code 200 and the correct component showing when the route exists.
404 and not found component when the route isn’t defined.
Got
Works as expected on a manually deployed site, but when auto deploying with GitHub only index.html returns 200. All other resources, such as js for the components return 404.
Same behavior with toml file and _redirects
.
GitHub deployed site: preeminent-tapioca-41f4ba.netlify.app
Manually deployed site: fancy-fox-8b03f2.netlify.app
I’m seeing the same happen for both the sites. The reason why you’re getting a 404 for the assets in the second site is because they actually don’t exist. Here’s the structure of the manually deployed site:
/netlify.toml
/index.js
/html.js
/index.html
/router/routes.js
/router/index.js
/pages/home.js
/pages/test.js
/dist/home-719f42f3.js
/dist/test-521ae3c9.js
/dist/html-fd2b60e8.js
/dist/index.js
/components/custom-head.js
Here’s the Git-deployed site:
/index.js
/html.js
/index.html
/netlify.toml
/router/routes.js
/router/index.js
/pages/home.js
/pages/test.js
/components/custom-head.js
I don’t see the dist/index.js
that’s being requested.
In any case. I won’t recommend the path you’re going. In SPAs, there’s no way to serve a 404 status code for the 404-routes (without using SSR). For example, if you actually take a close look at the config you’ve shared, the second redirect rule is actually redundant and not doing anything. All your pages are returning a 404: https://fancy-fox-8b03f2.netlify.app/test (the browser shows content because of client-side routing, but search engines and other tools will get a 404 code).