【发布时间】:2022-01-09 11:20:33
【问题描述】:
我想在 axios 拦截器中重定向页面。
但是,当服务器端渲染时,我无法在 axios 拦截器中访问服务器端上下文。所以我尝试使用下一个/路由器。但它只适用于客户端。
我怎么能这样?
下面是axios拦截器中执行的函数
// customAxios.ts
const customAxios = axios.create();
customAxios.interceptors.response.use((response) => response, responseErrorHandler);
// responseErrorHandler.ts
const responseErrorHandler = (error: AxiosError): Promise<AxiosError> => {
if (error.response) {
if (error.message === TIMEOUT_MESSAGE) {
if (typeof window === 'undefined') {
// Redirect to /503 page in server side rendering
} else {
window.location.href = '/503';
}
}
}
return Promise.reject(error);
}
【问题讨论】:
标签: typescript axios next.js