1 | # Isomorphy
|
2 | Collection of helpers to deal with isomorphic aspects of the code.
|
3 |
|
4 | **Why?** — Most of our ReactJS code should be isomorphic, i.e. it should
|
5 | be functional both when executed at the client-side (in browser), and when
|
6 | executed at the server-side (in NodeJS), without extra care of the caller.
|
7 | In some cases, it demands to explicitely check, where the code is executed, and
|
8 | proceed depending on that. This module provides functions that allow to do such
|
9 | checks, and to get some additional information about the currently running code.
|
10 |
|
11 | **Important Notes:**
|
12 | - All functionality of this module relies on `topcoder-react-utils`
|
13 | configuration / client initialization / server code beign used in your
|
14 | project;
|
15 | - `isDevBuild()` and `isProdBuild()` functions return dev/prod mode of the
|
16 | currently executed code. While the server launched in dev mode always serve
|
17 | dev version of the frontend code, the server launched in prod mode can serve
|
18 | either prod, or dev versions of the code, depending on which of them was build
|
19 | before by the caller. Note that the mode depends on the value `BABEL_ENV`
|
20 | environment variable (at the build time, for the front-end code; or at
|
21 | the present time, for the server-side code). `NODE_ENV` variable, in turn,
|
22 | defines the runtime environment, which can be configured via
|
23 | [`config`](./config-utils.md).
|
24 |
|
25 | [Example](#example)
|
26 |
|
27 | ### Reference
|
28 |
|
29 | - **`buildTimestamp()`** — Returns build timestamp of the frontend JS
|
30 | bundle, in form of ISO date/time string. At the server-side it will be the
|
31 | timestamp of bundle being served by the server.
|
32 | - **`isClientSide()`** — Returns `true` if executed at client-side
|
33 | (in browser); `false` otherwise.
|
34 | - **`isDevBuild()`** — Returns `true` if development version of the code
|
35 | is running; `false` otherwise.
|
36 | - **`isProdBuild()`** — Returns `true` if the production version of the
|
37 | code is running; `false` otherwise.
|
38 | - **`isServerSide()`** — Returns `true` if executed at the server-side
|
39 | (in NodeJS); `false` otherwise.
|
40 |
|
41 | ### Example
|
42 | ```js
|
43 | import { isomorphy } from 'topcoder-react-utils';
|
44 |
|
45 | if (isomorphy.isClientSide()) {
|
46 | // Do some useful staff here, if executed at the client-side.
|
47 | }
|
48 | ```
|