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.
-
Goals
Non-Goals
Background
The
getServerSideProps
API 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
getServerSideProps
function 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
getServerSideProps
functions 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
getServerSideProps
like so:There is just one
Promise
that 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 separateasync
functions. These functions would resolve to serializable values. Applications could then writegetServerSideProps
like so:The framework would then be able to flush early parts of the document (like the
<head>
tag) as soon asgetServerSideProps
resolves. Then it could flush the rest of the document when all of theAsyncProp
s resolve.The
AsyncProp
ObjectThe primary addition is the
AsyncProp
object that can be created ingetServerSideProps
and consumed via itsread()
method:The
read()
method will only return data when everyAsyncProp
object included ingetServerSideProps
successfully resolves. In every other case, including while anyAsyncProp
is pending, the return value isundefined
.For non-crawler User Agents, incoming requests will then be handled as follows:
AsyncProp
s. If an error occurs, Next.js will render a500
error as usual.</head>
with a200
status code (to allow the browser to begin fetching resources), andAsyncProp
s 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
200
or500
status code.Other Changes Required
Access to res in
getServerSideProps
In some cases, it’s possible to access the
res
object for a short period of time aftergetServerSideProps
resolves, but before Next.js sends it. With this proposal, Next.js will throw an error if you try to access theres
object aftergetServerSideProps
resolves. These cases should migrate to middleware instead.Use of
next/head
When 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
, orDocument
components.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 theApp
component to ease any future migration to React 18:Use of
next/script
<Script>
components using thebeforeInteractive
strategy should be moved to be direct children of theApp
orDocument
component. There is an RFC in progress to supportbeforeInteractive
on 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
Document
component will need to perform a trivial migration to a functionalDocument
component in order to usegetServerSideProps
streaming. More details will be provided soon.React 18 Migration
When used with React 18, the
read()
method ofAsyncProp
will suspend while data is pending, instead of returningundefined
. Applications can add<Suspense>
boundaries as usual to customize these loading states.Changelog
AsyncProp
for an improved React 18 migration storySpecial Thanks
Special thanks to
Beta Was this translation helpful? Give feedback.
All reactions