apollo / getInitialProps firing w/ Next static export
See original GitHub issueI’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:
- Created 6 years ago
- Reactions:4
- Comments:5 (2 by maintainers)
Top GitHub Comments
You could use
preval
to inline the cache into the bundles: https://github.com/zeit/next.js/tree/canary/examples/with-babel-macrosI 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.