Next.js: Feature request: provide access to build-time initial props at runtime on server and client

Created on 20 Jul 2018  路  6Comments  路  Source: vercel/next.js

Feature request

Is your feature request related to a problem? Please describe.

In some cases, it would be beneficial to run getInitialProps only at build-time on the server; for example, when consuming an API whose data changes infrequently, or requires some kind of secret credentials that cannot be exposed to the client. However, to make this setup work in Next's current state requires major compromises. This is especially an issue for static sites, where one usually cannot just set up a secondary API server to proxy these requests.

Describe the solution you'd like

Add an additional step to the build process which runs getInitialProps (or a new, similar method) and injects the result into the page's bundle, similarly to __NEXT_DATA__ for pre-rendered HTML. This would allow the data to be accessed even if the page in question was not the first one loaded. This functionality could be opt-in, either through additional configuration or with a HoC as withRouter does.

Describe alternatives you've considered

One possible workaround is to forgo the use of next/link and use anchor tags instead, forcing the client to grab the pre-rendered page with the data. Sensitive data could also instead be retrieved at the App level, ensuring it is available for any page. Both of these alternatives require abandoning key features of Next (quick transitions and code-splitting respectively), and thus a solution in core would make Next a more viable option for devs with these use cases.

Additional context

Preliminary discussion prompting this request can be found in #4384 and on Spectrum.

feature request

Most helpful comment

Very close... we've been refining the implementation before we post the RFC!

All 6 comments

Going to close this in favor of a RFC @Timer and @ijjk are drafting.

Hey guys @Timer @ijjk - how soon the RFC mentioned by @timneutkens is going to be available? Would like to check it out.

Couldn't find it in the issues tab, so I assume it's not done yet. There were no updates on thiis feature request for one and a half month, so I'm interested what's the state

Any progress on this? I see the ticket opened in July 2018. Static exporting cannot work as expected without this.

Very close... we've been refining the implementation before we post the RFC!

Calling a function using server.use(someFunction), inside the app.prepare.then() before any app.get calls to get some data that will be sent to a provider for all pages. However, I'm facing this issue

client pings, but there's no entry for page: /
and after a few seconds
build page: /
compiled successfully

What can I do to overcome this issue?

The RFC was posted here: please follow it for this feature!

9524

Was this page helpful?
0 / 5 - 0 ratings

Related issues

formula349 picture formula349  路  3Comments

ghost picture ghost  路  3Comments

havefive picture havefive  路  3Comments

rauchg picture rauchg  路  3Comments

timneutkens picture timneutkens  路  3Comments