You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Is your feature request related to a problem? Please describe.
Next is unable to handle data types that are not pure JSON. The most common use case is a Date but in order to use it in Next, we have to manually convert values to pure JSON (a string representation of a date or ms from epoch) on the server, fetch them as JSON in getInitialProps then transform them somewhere in the Component.
Describe the solution you'd like
Add a transformInitialProps callback that transforms pure JSON props into any JavaScript object.
For example, if we fetch a date range from the server encoded as ms we might have a transformInitialProps like this:
This gets much more interesting and much more powerful if you can add it to all pages automatically. Maybe we can have a way to add it to the _document.js page.
EJSON is a library that automatically supports dates in JSON through an encoding and also allows you to extend it to support any custom data type you like. It was originally designed for Meteor which also had to deal with shipping dates from server to client.
NextJS doesn't have to be opinionated about using EJSON but this gives everybody the option to support the date type and other custom types. The beauty is that you don't have to write custom transformInitialProps functions for each page if you can set a global one. Then you just make sure to encode everything in a standard JSON compatible encoding like EJSON.
Describe alternatives you've considered
Do the conversion somewhere in the component. The problem is having redundant code on every page. Easy to make mistakes. Not as clean because it doesn't feel standardized.
Additional context
A few thoughts:
When first using NextJS, the inability to pass date values which is common since it's a base type in databases felt like a bug. Having an immediate way to help people and provide an example in the documentation could alleviate that feeling.
NextJS is opinionated about some things. Might be worthwhile to consider one day adding EJSON support as a default so we get built-in date support. That's just a thought for the future though. I think the immediate value is to add without built-in EJSON support. Others might prefer other encoding schemes.
The text was updated successfully, but these errors were encountered:
We're not planning to change the way that getInitialProps works at this point. Introducing another static method would introduce more complexity for users for very minimal benefit (eg using Date which can be generated in other ways).
Feature request
Is your feature request related to a problem? Please describe.
Next is unable to handle data types that are not pure JSON. The most common use case is a Date but in order to use it in Next, we have to manually convert values to pure JSON (a string representation of a date or ms from epoch) on the server, fetch them as JSON in
getInitialProps
then transform them somewhere in the Component.Describe the solution you'd like
Add a
transformInitialProps
callback that transforms pure JSON props into any JavaScript object.For example, if we fetch a date range from the server encoded as
ms
we might have atransformInitialProps
like this:This gets much more interesting and much more powerful if you can add it to all pages automatically. Maybe we can have a way to add it to the
_document.js
page.EJSON is a library that automatically supports dates in JSON through an encoding and also allows you to extend it to support any custom data type you like. It was originally designed for Meteor which also had to deal with shipping dates from server to client.
NextJS doesn't have to be opinionated about using EJSON but this gives everybody the option to support the date type and other custom types. The beauty is that you don't have to write custom
transformInitialProps
functions for each page if you can set a global one. Then you just make sure to encode everything in a standard JSON compatible encoding like EJSON.Describe alternatives you've considered
Do the conversion somewhere in the component. The problem is having redundant code on every page. Easy to make mistakes. Not as clean because it doesn't feel standardized.
Additional context
A few thoughts:
When first using NextJS, the inability to pass date values which is common since it's a base type in databases felt like a bug. Having an immediate way to help people and provide an example in the documentation could alleviate that feeling.
NextJS is opinionated about some things. Might be worthwhile to consider one day adding EJSON support as a default so we get built-in date support. That's just a thought for the future though. I think the immediate value is to add without built-in EJSON support. Others might prefer other encoding schemes.
The text was updated successfully, but these errors were encountered: