-
-
Notifications
You must be signed in to change notification settings - Fork 2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Catching 404 errors on server side #7101
Comments
Duplicate of #6106 |
@dummdidumm this isn't a duplicate of #6106 at least not in the solution. I am looking for a way to catch 404s that are not in the routes, having my function in the |
No, you won't find out if it's a 404 or not because the layout doesn't run until it's determined which route to go to (or none, in the case of 404). Adding something to layout which says "you're on a 404 route" isn't wanted in the current design. What you can do for this specific problem is to create a catch-all route, from which you redirect: // src/routes/[...notFound]/+page.js
export function load() {
// you end up here when no other route was matched, i.e. the 404 case
throw redirect(301, 'somewhere/else');
} |
I thought I am unclear the difference between |
you can place it in |
@dummdidumm Ummm... throwing redirect is not working. However vite console prints this:
The site doesn't redirect, page still loads as a 404. I tried both Even tried a local existing path:
This is how i am throwing: |
@dummdidumm ?? Hello? Is this a new issue ? I don't want to create another unless I need to. |
Just tried it out, works for me. // [...notfound]/+page.js
import { redirect } from '@sveltejs/kit';
export function load() {
throw redirect(307, '/docs'); // or whereever you want to redirect
} |
Strange, it doens't work for me. Do you see any reason it will not work with this: import env from '$lib/env'
import { error, redirect } from '@sveltejs/kit'
import got from 'got'
export async function load ({ url }) {
let e404 = false
const sitePage = await got(`${env.api}/site-pages/?pathname=${url.pathname}`)
.then(response => {
let obj = JSON.parse(response.body)
obj = obj.data || obj
if (obj[0]) {
return obj[0]
} else {
e404 = true
return null
}
})
.catch((e) => {
console.error(e)
e404 = true
})
console.log('sitePage', sitePage)
if (e404) {
await got(`${env.api}/redirects/?linkName=${url.pathname}`)
.then(response => {
let obj = JSON.parse(response.body)
obj = obj.data || obj
if (obj[0]) {
throw redirect(obj[0].status, obj[0].location)
}
})
.catch((e) => {
console.error(e)
e404 = true
})
}
if (e404) throw error(404, 'not-found')
return {
sitePage
}
} Considering all the data requests are working as expected (I have confirmed this with console logs)? |
HAhaha wait a sec, I am catching the throw... lol My bad! |
I can't tell from reading the code without context what is going on. Have you tried to throw out everything in that load function and just try to redirect to some url? Also that |
@dummdidumm the following works perfectly. Thanks for your help: import env from '$lib/env'
import { error, redirect } from '@sveltejs/kit'
import got from 'got'
import _ from 'lodash'
export async function load ({ url }) {
let e404 = false
let redirectObj = {}
const sitePage = await got(`${env.api}/site-pages/?pathname=${encodeURI(url.pathname)}`)
.then(response => {
let obj = JSON.parse(response.body)
obj = obj.data || obj
if (obj[0]) {
return obj[0]
} else {
e404 = true
return null
}
})
.catch((e) => {
console.error(e)
e404 = true
})
if (e404) {
redirectObj = await got(`${env.api}/redirects/?pathname=${url.pathname}`)
.then(response => {
let obj = JSON.parse(response.body)
obj = obj.data || obj
if (obj[0]) {
return obj[0]
}
})
.catch((e) => {
console.error(e)
e404 = true
})
}
if (!_.isEmpty(redirectObj)) throw redirect(redirectObj.status, redirectObj.location)
if (e404) throw error(404, 'not-found')
return {
sitePage
}
} |
caught me too. I think this is a 'not good coding style' to have |
@nohea I agree completely. Throwing it like an error when it is the wanted action is strange and confusing. |
Describe the problem
Some code a while go worked perfectly:
__error.js
but now none of this will work, and I cannot make it work in any way other than in a way that loads the whole site.
This is used in 2 ways.
If you change the structure of your site, for SEO reasons, you want a 301 redirect.
I also use it for short links for affiliate forwarding using a 307 redirect.
The load function also doesn't have
error
orstatus
so I cannot do it in+layout.server.js
Describe the proposed solution
Allow
+error.js
or+error.server.js
files, or have the error in+layout.server.js
??Alternatives considered
No response
Importance
would make my life easier
Additional Information
No response
The text was updated successfully, but these errors were encountered: