Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

Isomorphic JavaScript is JavaScript that can run in both the browser and in Node.js on our back-end. The primary advantage of isomorphism is that business logic can be shared between the front-end and back-end, reducing duplication of code. This reduces errors due to code getting out of sync (for example when the validation for the sensor upload csv was different on the front-end and the back-end), and reduces the developer effort required to implement full-stack features. Given the strong benefits of introducing Isomorphism into the code base, this document will explore how to implement it.

...

Updating our code to support shared code

Option #4 from the below list was selected and as of September 2022, the api has been updated to ES6. The following section is being kept for historical context.

The major issue with updating our code to support isomorphism is that the webapp package uses ES6 modules and imports, while the api package uses CommonJS modules and imports. This means that by default, the shared code will not easily support being imported into both the webapp and api. If we write the shared code as ES6 modules with the .mjs extension then it can be imported into the CommonJS modules on the back-end with an asynchronous import() function, however this would lead to us having both synchronous and asynchronous imports in the same file, and would lead to added complexity as a lot more of the codebase would need to be asynchronous by default. If we make the shared code as CommonJS modules, then we can use the import * as foo from 'foo' syntax to import it into ES6, but if there were any calls to require() another file, then the import would fail - this would mean that every single piece of shared logic would need to be in self contained files.

...