question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Defer getServerSideProps on client-side navigation

See original GitHub issue

Feature 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:closed
  • Created 3 years ago
  • Reactions:231
  • Comments:38 (7 by maintainers)

github_iconTop GitHub Comments

41reactions
muhammetsaitcommented, Mar 31, 2021

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

const isServerReq = req => !req.url.startsWith('/_next');

function Post({ post }) {
  // Render post...
}

export async function getServerSideProps({ req }) {
  const initData = isServerReq(req) ? await fetch(...) : null;

  return {
    props: {
      initData,
    },
  }
}

31reactions
jeroenvisser101commented, Sep 30, 2021

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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found