Skip to content

Commit

Permalink
Chromium and errors optimization (#25)
Browse files Browse the repository at this point in the history
* Add debug information

* Add Progress log

* Optimize errors
  • Loading branch information
Aleksey Dmitriev authored Aug 29, 2018
1 parent b3dfd24 commit 11fcd25
Show file tree
Hide file tree
Showing 14 changed files with 75 additions and 90 deletions.
5 changes: 3 additions & 2 deletions client/src/app.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,11 @@ addLocaleData([...en, ...ru]);
export const App = () => {
return (<Provider store={ AppStore }>
<ConnectedIntlProvider textComponent={ TextComponent }>
<BrowserRouter basename={`/${locale}`}>
<BrowserRouter basename={ `/${locale}` }>
<LastLocationProvider>
<Switch>
<Route path='/server-error' exact={ true } component={ ServerErrorComponent }/>
{ window.__HAS_ERROR__ && <Route path='/' component={ ServerErrorComponent }/> }

<Route path='/' component={ AppComponent }/>
</Switch>
</LastLocationProvider>
Expand Down
3 changes: 1 addition & 2 deletions client/src/pages/server-error/server-error.component.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import * as React from 'react';
import { Link } from 'react-router-dom';

import './server-error.scss';

Expand All @@ -11,7 +10,7 @@ export class ServerErrorComponent extends React.PureComponent {
Unhandled Error occurred
</h2>

<Link to={'/'}>Go back to home</Link>
<a href={ '/' }>Go back to home</a>
</div>
);
}
Expand Down
1 change: 1 addition & 0 deletions client/src/typings.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ declare module 'highcharts-react-official';

interface Window {
__PRELOADED_STATE__: any;
__HAS_ERROR__: number;
__APP_CONFIG__: {
apiUrl: string;
};
Expand Down
31 changes: 5 additions & 26 deletions server/app.server.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ const TextComponent = (props: any) => {

addLocaleData([...en, ...ru]);

export const App = ({ location, context, preloadedState }: any) => {
export const App = ({ location, context, preloadedState, hasError }: any) => {
const languages = ['en', 'ru'];
let locale = languages[0];

Expand All @@ -31,29 +31,8 @@ export const App = ({ location, context, preloadedState }: any) => {
locale = pathLanguage;
}

preloadedState.settings.locale = locale;

const AppStore = configureStore(preloadedState);

return (<Provider store={ AppStore }>
<ConnectedIntlProvider textComponent={ TextComponent }>
<StaticRouter location={ location } context={ context } basename={ `/${locale}` }>
<LastLocationProvider>
<AppComponent/>
</LastLocationProvider>
</StaticRouter>
</ConnectedIntlProvider>
</Provider>);
};

export const Error = ({ location, context, preloadedState }: any) => {
const languages = ['en', 'ru'];
let locale = languages[0];

const pathLanguage = location.split('/')[1];

if (languages.includes(pathLanguage)) {
locale = pathLanguage;
if (!preloadedState.settings) {
preloadedState.settings = {};
}

preloadedState.settings.locale = locale;
Expand All @@ -62,9 +41,9 @@ export const Error = ({ location, context, preloadedState }: any) => {

return (<Provider store={ AppStore }>
<ConnectedIntlProvider textComponent={ TextComponent }>
<StaticRouter location={ location } context={ context }>
<StaticRouter location={ location } context={ context } basename={ `/${locale}` }>
<LastLocationProvider>
<ServerErrorComponent/>
{ hasError ? <ServerErrorComponent/> : <AppComponent/> }
</LastLocationProvider>
</StaticRouter>
</ConnectedIntlProvider>
Expand Down
63 changes: 7 additions & 56 deletions server/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@ import sprite from 'svg-sprite-loader/runtime/sprite.build';

import { serverHtml } from './server.html';

import { App, Error } from './app.server';
import { App } from './app.server';

import '../client/src/config/axios.config';

import { AddressPage } from './pages/address.page';
import { BlockPage } from './pages/block.page';
import { ChartImage, generateImages } from './pages/chart-image';
import { ChartImage, runImageGeneration } from './pages/chart-image';
import { ChartPage } from './pages/charts.page';
import { DataPage } from './pages/data.page';
import { SearchPage } from './pages/search.page';
Expand Down Expand Up @@ -67,49 +67,14 @@ server.use('/:locale?/search', SearchPage);

server.use((req: any, res, next) => {
axios.interceptors.response.use(response => response, () => {
if (req.explorer.skipError) {
return;
if (!req.explorer.skipError) {
req.explorer.hasError = true;
}

return res.redirect('/server-error');
});

next();
});

server.use('/:locale?/server-error', (req: any, res) => {
const context: any = {};

const body = renderToString(
(
<Error location={ req.url }
context={ context }
preloadedState={ req.explorer.preloadedState }/>
)
);

const helmet = Helmet.renderStatic();

const htmlToRender = serverHtml({
assets: manifest.assets,
body,
helmet,
preloadedState: req.explorer.preloadedState,
spriteContent: sprite.stringify()
});

if (context.url) {
res.writeHead(302, {
Location: context.url
});

res.end();
} else {
res.write(htmlToRender);
res.end();
}
});

server.use('*', Preloader);

server.use('/:locale?', DataPage);
Expand All @@ -128,6 +93,7 @@ server.get('*', (req: any, res) => {
(
<App location={ req.url }
context={ context }
hasError={ req.explorer.hasError }
preloadedState={ req.explorer.preloadedState }/>
)
);
Expand All @@ -137,6 +103,7 @@ server.get('*', (req: any, res) => {
const htmlToRender = serverHtml({
assets: manifest.assets,
body,
hasError: req.explorer.hasError,
helmet,
preloadedState: req.explorer.preloadedState,
spriteContent: sprite.stringify()
Expand All @@ -157,22 +124,6 @@ server.get('*', (req: any, res) => {
server.listen(port, () => {
console.debug(`App is listening on port ${port}!`);

generateImages()
.then(() => {
console.debug('images generated...');
})
.catch((e) => {
console.debug(e);
});

setInterval(() => {
generateImages()
.then(() => {
console.debug('images generated...');
})
.catch((e) => {
console.debug(e);
});
}, 1000 * 60 * 10);
runImageGeneration();
});

5 changes: 5 additions & 0 deletions server/pages/address.page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@ const render = (req: any, res: any, next: any) => {

next();
});
})
.catch(() => {
req.explorer.hasError = true;

next();
});
};

Expand Down
5 changes: 5 additions & 0 deletions server/pages/block.page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@ const render = (req: any, res: any, next: any) => {
}
};

next();
})
.catch(() => {
req.explorer.hasError = true;

next();
});
};
Expand Down
21 changes: 20 additions & 1 deletion server/pages/chart-image.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export const ChartImage = express.Router();
const cacheRoot = fs.realpathSync(process.cwd()) + '/tmp/cache/';
fs.ensureDirSync(cacheRoot);

export const generateImages = async () => {
const generateImages = async () => {
const chartTypes = [
'total',
'blockchain-size',
Expand Down Expand Up @@ -54,3 +54,22 @@ export const generateImages = async () => {
};

ChartImage.use('/', express.static(cacheRoot));

export const runImageGeneration = () => {
console.debug('[PROGRESS] Generating charts preview images...');

generateImages()
.then(() => {
console.debug('[SUCCESS] Charts preview generated!');

setTimeout(() => {
runImageGeneration();
}, 1000 * 60 * 10);
})
.catch((e) => {
console.debug('[FAILURE] Charts preview generation failed!');
console.debug(e);

runImageGeneration();
});
};
5 changes: 5 additions & 0 deletions server/pages/charts.page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@ ChartPage.get('/*', (req: any, res, next) => {
chart: preloadedState,
};

next();
})
.catch(() => {
req.explorer.hasError = true;

next();
});
});
5 changes: 5 additions & 0 deletions server/pages/data.page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@ DataPage.get('/', (req: any, res, next) => {

req.explorer.preloadedState = { ...req.explorer.preloadedState, ...preloadedState };

next();
})
.catch(() => {
req.explorer.hasError = true;

next();
});
});
5 changes: 5 additions & 0 deletions server/pages/stats.page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@ StatsPage.get('/', (req: any, res, next) => {
}
};

next();
})
.catch(() => {
req.explorer.hasError = true;

next();
});
});
5 changes: 5 additions & 0 deletions server/pages/transaction.page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@ const render = (req: any, res: any, next: any) => {
}
};

next();
})
.catch(() => {
req.explorer.hasError = true;

next();
});
};
Expand Down
8 changes: 6 additions & 2 deletions server/preloader.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ Preloader.get('*', (req: any, res, next) => {

const apiPreloadedState = apiReducer(apiInitialState, {
payload: {
data: apiData,
data: apiData
},
type: GET_API_SUCCESS
});
Expand All @@ -39,7 +39,7 @@ Preloader.get('*', (req: any, res, next) => {
...req.explorer.preloadedState,
api: {
...apiPreloadedState,
preloaded: true,
preloaded: true
},
settings: {
isScriptsDisplayed: true,
Expand All @@ -51,6 +51,10 @@ Preloader.get('*', (req: any, res, next) => {
}
};

next();
})
.catch(() => {
req.explorer.hasError = true;
next();
});
});
3 changes: 2 additions & 1 deletion server/server.html.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export const serverHtml = ({ body, assets, helmet, spriteContent, preloadedState }: { body: string, assets: any, helmet: any, spriteContent: any, preloadedState: any }) => `<!DOCTYPE html><html>
export const serverHtml = ({ body, assets, helmet, spriteContent, preloadedState, hasError }: { body: string, assets: any, helmet: any, spriteContent: any, preloadedState: any, hasError: boolean }) => `<!DOCTYPE html><html>
<head>
<meta charSet='utf-8'/>
<meta name='viewport' content='width=device-width, initial-scale=1, maximum-scale=1, shrink-to-fit=no'/>
Expand All @@ -21,6 +21,7 @@ export const serverHtml = ({ body, assets, helmet, spriteContent, preloadedState
JSON.stringify(preloadedState)
.replace(/</g, '\\\u003c')
}
window.__HAS_ERROR__ = ${ hasError ? 1 : 0};
</script>

<script src='/${assets['main.js']}'></script>
Expand Down

0 comments on commit 11fcd25

Please sign in to comment.