Custom Error Page
Hi, I have created a custom error page and place .vue file in the root directory. Now, whenever there is a page that doesn''t exist; it automatically redirects to error.vue but in the Network tab I still see the status to be 200. it should be 404. help with that please, thanks
'
29 Replies
update: I have tried setResponseStatus() and useRequestEvent in my error.vue but still its not working..
could you provide a reproduction? In a new Nuxt project, it sets the correct 404 response code for me
yeah, hold on
@Thr0nSK when I console.log process.server, it prints
false
I'm not sure what you're talking about
Can you give me a small reproduction on stackblitz, for example?
~/error.vue
You do not need to set the response status there
404
should be automatically set for youbut I get
200
what could I be missing...yeah, so can you give me a reproduction with the project, where you're getting the
200
status code? 😅LOL.. that'd be a big big reply
create a small reproduction here
okay let me try
stackblitz preferably
https://stackblitz.com/edit/github-jagvr2?file=error.vue,app.vue,pages%2Fproducts.vue
okay, I am getting 404 here
something wrong with my project then...
if you use a catch-all you have to deal with 404s "on your own" 🙂
does that mean, I can't use the useRequestEvent compoable and etResponseStatus() plugin?
you should still be able to
Thank you for the help, I got it. See below:
Thank you @Thr0nSK and @manniL / TheAlexLichter
but this also has problems... lets say I directly enter the address of a certain page.. for example a direct link to a product. this code is going to add 404 status code to that as well..
do you have a [...].vue component somewhere?
yes, I do.. we have three apps using same code base. and there is a [...slug].vue page in the pages directory which is basically being used to render components based on the app coming from the configurations..
could you try using
in cases where you want to render an error page?
+
fatal: true
very importantit works but for some reason my ui doesn't load
does the error page in the root load when some other errors appear as well? for example there is a function that I am calling but it doesn't exist or some composable that has been called outside the nuxt context?
If I'm not mistaken, it will show a fullscreen error page for those errors as well if rendered on the server.
On the client side, it will behave as if you had
throw createError({ fatal: false })
hi again, I have used
throw createError
but whenever I use it, my layout doesn't load, does any of you have idea about what's going on here?
okay, using useRequestEvent()
and setResponseStatus(event, 404);
resolved the issue.
but I would still love to know why it didn't work with createError
well, throwing createError renders the error.vue file, which comes without anything (think of it like "an extra app.vue just for errors")
right, I read somewhere in the documentation ( i think it was for createError) that by default ~/error.vue is rendered with error layout. I tried to create a custom error layout by actually creating one and naming it error.vue but I don't think it worked.. I need to test that again though.
Also, if there is some parsing error like so:
<div> {{ hello world! }} </div>
how does nuxt throw error for that? because first when I navigate to the page which contains this code, it will show me that I have a parsing error in a modal kind of window. because of 'hello world!' . Now, if I refresh the page; I am taken to the ~/error.vue
but I see no layout at all (normally I see default which was setup by my team lead). and I have used useError
there which will get the 'statusCode', 'message' and ' statusMessage' etc. they are displayed but no layout.
but, instead of refreshing the page, if I just press escape and remove that error modal that showed parsing error. try to go to that page again (the one that has parsing error) I am taken to the ~/error.vue
page but this time it prints default error message which is kind of fallback and doesn't show any statusCode etc.
Same thing when I try to render a nuxt context related composable outside the context.
I would really be thankful if you could share your opnion on it. thanksright, I read somewhere in the documentation ( i think it was for createError) that by default ~/error.vue is rendered with error layoutNo, that's not the case. Where did you read that? error.vue is independent of the app to avoid errors in the error component and having a catch-22 😄
okay, my bad. it isn't in the docs. perhaps chatGPT told me that (free version LOL). any word about parsing errors?