ocaml / v3.ocaml.org-rescript

The next implementation of ocaml.org, built on OCaml, ReScript, NextJS, and Tailwind.
https://v3.ocaml.org
75 stars 10 forks source link

Explicitly serialize data returned from `getStaticProps` #345

Closed ghost closed 3 years ago

ghost commented 3 years ago

Currently, we rely on developers remembering to always use ReScript types which are guaranteed to serialize to and deserialize from JSON when defining types that will be used in the return value of a getStaticProps method. One such type that doesn't provide guarantees is the ReScript option type. Let's explicitly model the constraints that NextJS imposes on getStaticProps. We may also automate performing serialization and deserialization as well, but such automation is not mandatory for the first PR addressing this issue.

We want to change our NextJS bindings. We want the return type of GetStaticProps.t to require the result of the Promise to be a Json.t. We also want to constrain the parameter of the type passed into each NextJS page module's make function to be a Json.t. Once we have these constraints in place, we need to either add a call to encode at the end of each getStaticProps implementation or make use of a functor to define getStaticProps given the definition of a function like getStaticPropsImpl which takes care of invoking encode. Similarly, we need to perform the reverse operation as a first step in the implementation of make.

Note: It would be easiest to work on this issue after #344 is complete.

agarwal commented 3 years ago

It would be easiest to work on this issue after #344 is complete

Might be okay to not wait for that issue. We can incrementally enforce this signature on pages as it becomes feasible. And this signature might well help force #344 to get done.

ghost commented 3 years ago

438 uses some low level Javascript methods to ensure the subset of json that is produced from ReScript/OCaml, and sent through NextJS Javascript processing, will flow through all processing without throwing an exception during serializing or deserializing. As a result, we no longer are aiming to implement custom json encoders and decoders for each data type.