-
I'm using React Query and prefetching from rsc. In the normal react(csr), I used useErrorBoundary in the queryClient to return the error true if it was an axiosError, not an error if it was a Zod error. some think like that // root Provider
const queryClient = new QueryClient({
defaultOptions: {
queries: {
useErrorBoundary: (error) => {
// if zod error
if (error instanceof ZodError) {
// some alert show
return false;
}
// if other error ( like axiosError 401)
return true;
},
onError(err) { // axios Error
if (err instanceof AxiosError) {
if (err.response?.status === 401) {
// redirect
}
}
},
},
},
});
// useQuery (custom - hook)
const useSomeQuery = () => {
const { data } = useQuery({
queryKey: ['some'],
queryFn: api,
});
// zod schema
const result = schema.parse(data);
return { data: result };
};
// call query
function Some() {
// no undefined
const {data} = useSomeQuery();
} // using Some Function
function App() {
return <ErrorBoundary>
<Suspense>
<Some />
</Suspense>
</ErrorBoundary>
} The reason why I worked like this is because There was no suspenseQuery at the time Let's get back to the point Prefetching method is being used as per document. Below is the page(rsc) where you prepetching export default function Page() {
const queryClient = new QueryClient({
queryCache: new QueryCache({
onError: () => redirect("/"),
}),
});
await queryClient.prefetchQuery({
queryKey: ["some"],
queryFn: api,
});
return <Hydrate ...><SomeComponent /></Hydrate>
}
// queryFn api
async function api() {
const result = await fetch(somePath, {
method: "GET",
next: {
revalidate: 20,
},
});
if (result.status === 401) {
// 401 error throw
throw new FetchError(result.status, "fetch error 401");
}
const jsonData = await result.json();
return jsonData;
} The problem above is that redirect does not work. redirect is not working and I don't know why.. I wonder how the server component should handle the redirection for 401.. call redirect Error: NEXT_REDIRECT
at getRedirectError (/some-path/.next/server/chunks/node_modules__pnpm_a85090._.js:11039:19)
at Module.redirect (/some-path/.next/server/chunks/node_modules__pnpm_a85090._.js:11049:11)
at Object.onError (/some-path/.next/server/chunks/[root of the server]__435afd._.js:1087:253)
at Object.onError (/some-path/.next/server/chunks/node_modules__pnpm_a85090._.js:8810:45)
at reject (/some-path/.next/server/chunks/node_modules__pnpm_a85090._.js:8421:29)
at /some-path/.next/server/chunks/node_modules__pnpm_a85090._.js:8462:17
at process.processTicksAndRejections (node:internal/process/task_queues:95:5) {
digest: 'NEXT_REDIRECT;replace;/;false',
mutableCookies: p {
_parsed: Map(2) { 'ac' => [Object], 'ar' => [Object] },
_headers: HeadersList {
cookies: [Array],
[Symbol(headers map)]: [Map],
[Symbol(headers map sorted)]: null
}
}
}
Error: NEXT_REDIRECT
at getRedirectError (/some-path/.next/server/chunks/node_modules__pnpm_a85090._.js:11039:19)
at Module.redirect (/some-path/.next/server/chunks/node_modules__pnpm_a85090._.js:11049:11)
at Object.onError (/some-path/.next/server/chunks/[root of the server]__435afd._.js:1087:253)
at Object.onError (/some-path/.next/server/chunks/node_modules__pnpm_a85090._.js:8810:45)
at reject (/some-path/.next/server/chunks/node_modules__pnpm_a85090._.js:8421:29)
at /some-path/.next/server/chunks/node_modules__pnpm_a85090._.js:8462:17
at process.processTicksAndRejections (node:internal/process/task_queues:95:5) {
digest: 'NEXT_REDIRECT;replace;/;false',
mutableCookies: p {
_parsed: Map(2) { 'ac' => [Object], 'ar' => [Object] },
_headers: HeadersList {
cookies: [Array],
[Symbol(headers map)]: [Map],
[Symbol(headers map sorted)]: null
}
}
}
⨯ node_modules/.pnpm/next@14.0.3_react-dom@18.2.0_react@18.2.0/node_modules/next/dist/client/components/redirect.js (52:19) @ getRedirectError
⨯ unhandledRejection: Error: NEXT_REDIRECT
at getRedirectError (/some-path/.next/server/chunks/node_modules__pnpm_a85090._.js:11039:19)
at Module.redirect (/some-path/.next/server/chunks/node_modules__pnpm_a85090._.js:11049:11)
at Object.onError (/some-path/.next/server/chunks/[root of the server]__435afd._.js:1087:253)
at Object.onError (/some-path/.next/server/chunks/node_modules__pnpm_a85090._.js:8810:45)
at reject (/some-path/.next/server/chunks/node_modules__pnpm_a85090._.js:8421:29)
at /some-path/.next/server/chunks/node_modules__pnpm_a85090._.js:8462:17
at process.processTicksAndRejections (node:internal/process/task_queues:95:5) {
digest: 'NEXT_REDIRECT;replace;/;false',
mutableCookies: p {
_parsed: Map(2) { 'ac' => [Object], 'ar' => [Object] },
_headers: HeadersList {
cookies: [Array],
[Symbol(headers map)]: [Map],
[Symbol(headers map sorted)]: null
}
}
} |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 4 replies
-
not sure what this has to do with react-query? Wouldn't you get the same result if you did:
idk, maybe |
Beta Was this translation helpful? Give feedback.
not sure what this has to do with react-query? Wouldn't you get the same result if you did:
idk, maybe
redirect
is just not something you can do within a server component? But that's really a next specific question