question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Response status code is not 404 but 200 with custom pages/404.vue

See original GitHub issue

Environment


  • Operating System: Linux
  • Node Version: v14.16.0
  • Nuxt Version: 3.0.0-27265876.3cd4494
  • Package Manager: npm@7.17.0
  • Bundler: Vite
  • User Config: -
  • Runtime Modules: -
  • Build Modules: -

Reproduction

https://stackblitz.com/edit/github-tpmisr

Describe the bug

When using a pages/ directory and navigating to non existent page, result is a default nuxt 404 page and statusCode = 404. This is expected After adding a custom pages/404.vue page, the content of the page is returned as expected, but statusCode = 200 which is not expected*

Additional context

If this behavior by design, how expected* behavior could be achieved?

Logs

No response

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:5 (1 by maintainers)

github_iconTop GitHub Comments

4reactions
7utecommented, Dec 30, 2021

@maxdzin @levchak0910 This works for me, effectively setting the right status code (nuxt 3.0.0-27338323.1e98259)

setup() {
    if (process.server) {
        const nuxtApp = useNuxtApp();
        const error = new Error();
        error.statusCode = 404;
-       nuxtApp.ssrContext.error = error;
+       nuxtApp.ssrContext.nuxt.error = error;
    }
},
0reactions
maxdzincommented, Dec 30, 2021

@7ute Great, thanks! I thought that it is supposed to return a 404 error automatically. 😃

Read more comments on GitHub >

github_iconTop Results From Across the Web

Vue-router redirect on page not found (404) - Stack Overflow
The solution above would just send you to a 404 page but you are still getting 200 status code. To do this, I...
Read more >
Custom Error Pages with nuxt.js - Daniel 'mavrick' Lang
Searching Google for 'custom error page in nuxt' just yeilds github issues that will get you no ... error({ statusCode: 404, message: 'Post...
Read more >
How To Properly Serve 404 Errors on SPAs (with SEO in Mind)
In SEO terms, it means that every request will return a "200 Success" HTTP status code. In less technical terms: this is very...
Read more >
Generating custom error responses - Amazon CloudFront
CloudFront returns an HTTP 404 status code (Not Found) to the viewer that requested the object. When CloudFront returns a custom error page...
Read more >
How to correctly return the HTTP status code 404 for an error ...
... return the HTTP status code 404 for an error page in Nuxt project-Vue.js. ... directly go to the 404 page, you should...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found