Defer getServerSideProps on client-side navigation
See original GitHub issueFeature request
Is your feature request related to a problem? Please describe.
When clicking in a link to a page that uses getServerSideProps
, the navigation will be kept in hold until the server response is complete, then the navigation will start. I want to be able to defer the getServerSideProps
data-fetching until the client-side navigation has been completed. Similar to the behavior of getStaticProps
with fallback: true
.
Describe the solution you’d like
- When directly accessing the page (e.g., typing the URL and pressing Enter), it should be server-side rendered
- When navigating to the page from another page, the navigation should happen immediately. The page will be rendered with a loading state. When the navigation is completed, the
gSSP
request should be fired to fetch the content and send it through the page props.
I should be able to opt-in this behavior with a flag in the return of gSSP
. The router should have a flag to tell if the page is fetching data:
// pages/example.js
function Post({ post }) {
const router = useRouter()
// If the page data is not yet fetched, this will be displayed
// initially until getServerSideProps() finishes running
if (router.isFetchingAfterDefer) {
return <div>Loading...</div>
}
// Render post...
}
export async function getServerSideProps(context) {
return {
props: { ... },
deferOnClientSideNavigation: true,
}
}
Describe alternatives you’ve considered
Using getInitialProps
/getServerSideProps
to detect if it is client-side navigation and send empty props, then repeat the fetch logic in the component code
Issue Analytics
- State:
- Created 3 years ago
- Reactions:231
- Comments:38 (7 by maintainers)
Top Results From Across the Web
Defer getServerSideProps on client-side navigation #32243
I want to be able to defer the getServerSideProps data-fetching until the client-side navigation has been completed. Similar to the behavior of ...
Read more >Nextjs getInitialProps blocked the page rendering in client side?
When navigating between pages using next/link instead of executing getServerSideProps in the browser Next.js will do a fetch to the server ...
Read more >Data Fetching: getServerSideProps - Next.js
When you request this page on client-side page transitions through next/link or next/router , Next.js sends an API request to the server, which...
Read more >Data fetching strategies in NextJS | The Codest
This method also is called on in every client-side navigation so ... The getServerSideProps method looks very similar to getStaticProps.
Read more >Best practices to increase the speed for Next.js apps
Use server-side rendering ; async function getServerSideProps({context}) ; const data = await ; // Returning the fetched data return ; function ...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Hello everyone 👋
I was facing this problem in my project and I ended up using this as a workaround (hacky as hell but seems to work):
I wrote a proposal/RFC for an server-side only version of
getServerSideProps
,getInitialServerSideProps
(ran only on the initial request, subsequent client-side navigation will have to do data fetching manually, for instance, with Apollo). I’m interested to hear if this would be a solution for the issues described here.I’m working on a working prototype too, will share when ready.