Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for transformations for getInitialProps #6917

Closed
thesunny opened this issue Apr 6, 2019 · 1 comment
Closed

Add support for transformations for getInitialProps #6917

thesunny opened this issue Apr 6, 2019 · 1 comment

Comments

@thesunny
Copy link

thesunny commented Apr 6, 2019

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 a transformInitialProps like this:

Home.transformInitialProps = function({startDate, endDate}) {
  return {
    startDate: new Date(startDate),
    endDate: new Date(endDate)
  }
}

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.

Document.transformInitialProps = function(props) {
  return EJSON.fromJSONValue(props)
}

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:

  1. 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.

  2. 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.

@timneutkens
Copy link
Member

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).

@lock lock bot locked as resolved and limited conversation to collaborators Apr 15, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants