Replies: 6 comments 4 replies
-
|
regrading error handling, it concerns me a bit that there's difference in rendering between streaming and non-streaming. Wouldn't it be possible to render the error page content instead, just like you would do with non-streaming rendering? I feel like it would be more beneficial to educate users on the following pattern instead: async function getServerSideProps() {
return {
props: async () => {
try {
const data = await getData(user);
return { data };
} catch (error) {
return { error: error.message }
}
},
}
}This has the benefit that the error handling behavior can be defined in the type definitions of the page properties and be enforced by a type checker both in |
Beta Was this translation helpful? Give feedback.
-
|
hi, what are the implications of this for |
Beta Was this translation helpful? Give feedback.
-
|
Have there been any updates on this RFC? It looks like aspects of this api design are partially supported in Next today, however it seems like it may only eagerly handle 404's and redirects. We experimented with this a bit in dev but we didn't notice anything different from our regular setup returning an object of props instead of an async function, however we'd be really interested to see this feature be fully supported! |
Beta Was this translation helpful? Give feedback.
-
|
I've updated the body of the RFC with our latest thinking. Our goal with these changes is to put anyone who adopts this RFC in React 17 ahead on adopting React 18. @Janpot I would love if you could provide feedback on the latest iteration. |
Beta Was this translation helpful? Give feedback.
-
|
Thank you everyone who read and participated in this RFC. Your feedback has been greatly appreciated! When we first designed this RFC, React 18 and React Server Components support in Next.js was further away. Now, however, they are much closer and we have a clearer picture of the user journey. In light of that new information, we've decided that it does not make sense to further pursue this proposal at this time. Keep an eye out for updates!
|
Beta Was this translation helpful? Give feedback.
-
|
Hi, Streaming support as documented here has landed as part of the |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Goals
Non-Goals
Background
The
getServerSidePropsAPI allows an application to fetch the data required for a page component by returning a serializable value in{ props: ... }. This value will be passed to the page component for Server-Side Rendering.The
getServerSidePropsfunction can also be used to perform routing by throwing an error, or returning a{ notFound: true }or{ redirect: ... }object.Unfortunately, this coupling of routing and data fetching concerns means that Next.js must always wait until all of the data for a page is fetched before it can begin to flush anything, just in case a redirect happens to be returned. In many cases, this causes several hundred of milliseconds delay to metrics like TTFB, FCP, and LCP.
In React 18, these issues are effectively addressed using Suspense and Server Components. However, it may take some time before these are available and applications adopt them, so we sought a shorter term, incremental solution that more apps could benefit from today.
Proposal
We propose the ability for
getServerSidePropsfunctions to separate data fetching concerns from routing, so that Next.js can begin flushing the document earlier, allowing the browser to discover critical resources like CSS and fonts sooner.Today, an application might write
getServerSidePropslike so:There is just one
Promisethat must wait for both data fetching and routing to complete. To separate these concerns and make them more granular, Next.js could allow individual properties to be generated with separateasyncfunctions. These functions would resolve to serializable values. Applications could then writegetServerSidePropslike so:The framework would then be able to flush early parts of the document (like the
<head>tag) as soon asgetServerSidePropsresolves. Then it could flush the rest of the document when all of theAsyncProps resolve.The
AsyncPropObjectThe primary addition is the
AsyncPropobject that can be created ingetServerSidePropsand consumed via itsread()method:The
read()method will only return data when everyAsyncPropobject included ingetServerSidePropssuccessfully resolves. In every other case, including while anyAsyncPropis pending, the return value isundefined.For non-crawler User Agents, incoming requests will then be handled as follows:
AsyncProps. If an error occurs, Next.js will render a500error as usual.</head>with a200status code (to allow the browser to begin fetching resources), andAsyncProps will begin executing.If Next.js detects that the User Agent is a crawler, it will wait until the final response is ready, and send it, along with the appropriate
200or500status code.Other Changes Required
Access to res in
getServerSidePropsIn some cases, it’s possible to access the
resobject for a short period of time aftergetServerSidePropsresolves, but before Next.js sends it. With this proposal, Next.js will throw an error if you try to access theresobject aftergetServerSidePropsresolves. These cases should migrate to middleware instead.Use of
next/headWhen streaming, Next.js will flush
<head>early. For this reason, it's important to move tags required for the first paint such that they will be rendered as direct children of the page,App, orDocumentcomponents.Sometimes, tags such as
<title>will be driven by anAsyncProp. In these cases, you should make sure to include a default/fallback in one of the aforementioned components. We recommend theAppcomponent to ease any future migration to React 18:Use of
next/script<Script>components using thebeforeInteractivestrategy should be moved to be direct children of theApporDocumentcomponent. There is an RFC in progress to supportbeforeInteractiveon a per-page basis in a more limited form.Functional custom Document and CSS-in-JS libraries
As with React 18 streaming, applications with a custom
Documentcomponent will need to perform a trivial migration to a functionalDocumentcomponent in order to usegetServerSidePropsstreaming. More details will be provided soon.React 18 Migration
When used with React 18, the
read()method ofAsyncPropwill suspend while data is pending, instead of returningundefined. Applications can add<Suspense>boundaries as usual to customize these loading states.Changelog
AsyncPropfor an improved React 18 migration storySpecial Thanks
Special thanks to
Beta Was this translation helpful? Give feedback.
All reactions