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.

apollo / getInitialProps firing w/ Next static export

See original GitHub issue

I’m trying next export in an app that uses apollo client to load content from our CMS. When navigating from page to page via <Link> or imperative Router actions w/ prefetching, getInitialProps fires on page load & emits a call to my graphql API.

  • I have searched the issues of this repository and believe that this is not a duplicate.

Expected Behavior

I’d hoped that the static export process would ‘pre-hydrate’ the apollo cache such that the data for a route is packed into the HTML/JS of the route payload. When loading a route, then, no calls should be fired to the content API at all (we don’t have any dynamic content).

This works fine when fetching the response w/ curl or something, and when navigating directly to a route, no calls are fired to our API endpoint (per the Network tab).

My expectation is that no API calls would be fired when loading subsequent routes accessed via <Link prefetch>

Current Behavior

When clicking a prefetch link (or using the imperative Router.prefetch), getInitialProps is fired on route transition & I get a ‘loading’ state.

If I use vanilla <a>, the transition happens as expected and no API call is fired. But, there’s no prefetching w/ this option, so not really ideal.

Your Environment

Tech Version
next 4.14
node 8.6
OS win10 w/ WSL bash
browser chrome 62
etc

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:4
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
timneutkenscommented, Dec 30, 2018

You could use preval to inline the cache into the bundles: https://github.com/zeit/next.js/tree/canary/examples/with-babel-macros

0reactions
dimitrinicolascommented, Mar 21, 2019

I encountered the same problem, preval is a great solution. I would like to inform those who would like to use it to call an API, that it is possible by using sync-request.

const body = preval`
  const syncRequest = require('sync-request');
  const res = syncRequest('GET', 'https://example.com')
  module.exports = res.getBody('utf8');
`;
Read more comments on GitHub >

github_iconTop Results From Across the Web

Updating Apollo with Nextjs because of getStaticProps and ...
You use it in getStaticProps to fetch your stuff and extract the cache and pass it as props. export const getStaticProps = async...
Read more >
Apollo query within NextJS's getServerSideProps?
Can you help me with my problem. I can create another stackoverflow question if needed. I'm trying to force a 404 status code...
Read more >
getInitialProps - Data Fetching - Next.js
getInitialProps enables server-side rendering in a page and allows you to do initial data population, it means sending the page with the data...
Read more >
next - npm
Notice that to load data when the page loads, we use getInitialProps which is an async static method. It can asynchronously fetch anything...
Read more >
getstaticprops not passing props - You.com | The AI Search ...
Apollo Client doesn't need to be referenced in the getStaticProps ... If you use next export with getInitialProps there is another problem that...
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