Repository navigation
Conditionally calling getServerSideProps? #23642
Unanswered
blainegarrett
asked this question in
Help
Replies: 2 comments 1 reply
|
Hey! You can use a service worker to achieve what you want, but that's an indirect approach. Another solution might be to use Would any of those work for you? |
1 reply
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
tl;dr - Is there a way to conditionally call getServerSideProps based on client state to save roundtrips?
I leverage a client side in-memory store as a way to collect resources loaded from my api in the client. On initial page load, SSR serves up a pre-populated store (and the rendered content) for the page as a starting point for the browsing session. If I navigate to a page that needs data already in the store, I can skip doing a fetch to my api from inside of getInitialProps. This works great. However, utilizing getInitialProps opts me out of static generation.
In a branch, I've switched to using getServerSideProps instead of getInitialProps - things work. However, getServerSideProps gets called for every page with it present. Inside of getServerSideProps, I don't have access to the collected client-side store (since it is run the server) so I can't tell if I have the data already or not and thus hit my api. For my use case, this feels like unnecessary round trips to the server and unnecessary hammering on the api if I already have the data. Previously, I could use logic inside of getInitialProps run on the client to prevent the api call. Now, it seems I have 2 server calls (getServerSideProps and it's call to the api) and no way to prevent it.
Is there a good way to let the client determine if getServerSideProps should be called or not and save the round trip?
If not, is getInitialProps the best way to continue doing what I am trying to do?
All reactions