Commit 3d39a107 authored by Matija Obreza's avatar Matija Obreza

Server-side should not cancel XHR requests

parent 55ecae35
......@@ -60,14 +60,14 @@ const prerenderer = (html, errHtml) => (req, res) => {
initFromServerStorage(serverStorage, store.dispatch);
console.log(`Processing request`, req._parsedOriginalUrl);
// console.log(`Processing request`, req._parsedOriginalUrl);
const pathname = req._parsedOriginalUrl.pathname;
const search = req._parsedOriginalUrl.search;
const context = {};
function setLocale() {
const locale = detectLocaleFromPath(config.frontendPath, pathname, 0);
console.log('Detected locale for SSR is', locale);
// console.log('Detected locale for SSR is', locale);
req.i18n.changeLanguage(locale);
}
......@@ -139,7 +139,7 @@ const prerenderer = (html, errHtml) => (req, res) => {
const metaDescription = metaDescriptionText ? `<meta name="description" content="${metaDescriptionText}"/>` : '';
return html.replace(/SERVER_RENDERED_(CSS|STATE|HTML|TITLE|I18NSTORE|DIR|LANG|HEADLINKS|BUNDLECSS|BUNDLESCRIPTS|DESCRIPTION)/g, (match, x) => {
console.log(`Injecting ${match}`);
// console.log(`Injecting ${match}`);
switch (match) {
case 'SERVER_RENDERED_BUNDLECSS': return bundleStyles.map((style) => {
return `<link href="${style.file}" rel="stylesheet"/>`;
......@@ -174,16 +174,10 @@ const prerenderer = (html, errHtml) => (req, res) => {
store.dispatch(receiveLang(language));
fetchComponentData(store.dispatch, branch, search, store.getState())
.catch((err) => {
console.log('Error fetching component data', ApiError.axiosError(err));
res.status(500).end(makeErrorHtml(errHtml, {status: err.code || 500, data: err.message}, true));
// const errFilledHtml = makeErrorHtml(errHtml, err);
// res.status(500).set('Content-Type', 'text/html').send(errFilledHtml);
})
.then(() => {
console.log('Fetched all component data');
return renderView();
}).then((html) => {
const html = renderView();
const serverRenderTime = `${Date.now() - startTime}ms`;
console.log('Server render time:', startTime, Date.now(), serverRenderTime);
......@@ -205,14 +199,15 @@ const prerenderer = (html, errHtml) => (req, res) => {
return res.set('Content-Type', 'text/html').send(html);
}).catch((err) => {
console.error('Error:', err);
const errFilledHtml = makeErrorHtml(errHtml, err, false);
res.status(503).set('Content-Type', 'text/html').send(errFilledHtml);
console.log('Error fetching component data', ApiError.axiosError(err));
return res.status(500).end(makeErrorHtml(errHtml, {status: err.code || 500, data: err.message}, true));
// const errFilledHtml = makeErrorHtml(errHtml, err);
// res.status(500).set('Content-Type', 'text/html').send(errFilledHtml);
});
}).catch((err) => {
console.error('Error:', err.message);
const errFilledHtml = makeErrorHtml(errHtml, err, true);
res.status(503).set('Content-Type', 'text/html').send(errFilledHtml);
return res.status(503).set('Content-Type', 'text/html').send(errFilledHtml);
});
};
......
......@@ -13,13 +13,15 @@ export const createApiCaller = (method, payloadType: string, config: {withoutTim
// log('Api call started');
if (cancelToken && cancelToken.token) {
console.log('Canceling previous request');
cancelToken.cancel(); // Cancel previous
if (typeof window !== 'undefined') {
// Cancel only on the client
if (cancelToken && cancelToken.token) {
console.log('Canceling previous request', cancelToken);
cancelToken.cancel(); // Cancel previous
}
cancelToken = axios.CancelToken.source();
}
cancelToken = axios.CancelToken.source();
dispatch({ type: payloadType, payload: { apiCall: ApiCall.start() } }); // Loading
const accessToken = getState().applicationConfig.anonToken.access_token;
......@@ -81,13 +83,15 @@ export const createPureApiCaller = (method, config: {direct?: boolean, withoutTi
// log('Pure Api call started');
if (cancelToken && cancelToken.token) {
console.log('Cancelling previous request');
cancelToken.cancel(); // Cancel previous
if (typeof window !== 'undefined') {
// Cancel only on the client
if (cancelToken && cancelToken.token) {
console.log('Cancelling previous request', cancelToken);
cancelToken.cancel(); // Cancel previous
}
cancelToken = axios.CancelToken.source();
}
cancelToken = axios.CancelToken.source();
const accessToken = getState().applicationConfig.anonToken.access_token;
const xhrConfig: any = {
......
......@@ -6,7 +6,9 @@ const matchRoutes = (routes, pathname, parentPath = '', branch = []) => {
routes.some((route) => {
let match = null;
const absolutePath = (route.path) ? (parentPath !== '/' ? (parentPath + route.path) : route.path) : route.path;
console.log(`Matching route ${pathname} for abs=${absolutePath} parentPath=${parentPath} route.path=${route.path}`);
if (typeof window !== 'undefined') {
console.log(`Matching route ${pathname} for abs=${absolutePath} parentPath=${parentPath} route.path=${route.path}`);
}
const options = {
path: absolutePath,
exact: route.exact,
......
......@@ -39,17 +39,18 @@ axiosBackend.interceptors.response.use((config) =>
// Do something before request is sent
config
, (error) => {
console.log('Backend error!! Error', error);
if (axios.isCancel(error)) {
throw error;
}
console.log('Backend error!! Error', error);
if (error.response && error.response.status === 401 && error.response.data.error === 'invalid_token') {
console.log('Error on 401, invalid token: ', error);
clearCookies();
window.location.replace('/');
if (typeof window !== 'undefined') {
clearCookies();
window.location.replace('/');
}
// todo maybe add window reload
// return error.config.dispatch
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment