apollo
apollo copied to clipboard
apollo not initialized under spa mode with redirect
Version
Reproduction link
https://github.com/look416/nuxt-apollo-error-demo
Steps to reproduce
- Set the mode to "universal" under nuxt config
- import redirect under auth middleware
- check apollo token
- call redirect function if token not found
- yarn generate
- check it using simple http server
What is expected ?
- The page is rendered successfully
What is actually happening?
- Error popping:
Additional comments?
As of now, I am managed to bypass the issue by either two ways:
- setting the mode to "spa"
- instead of importing redirect, use the app.router to do the redirection
I'm also having this issue and it's driving me mad. I didn't find the issue when googling due to the error being in a screenshot, so pasting here in the hope that others experiencing the same issue will jump in:
TypeError: Cannot read property 'defaultClient' of undefined
at eval (apollo-module.js?0d2d:45)
at _callee2$ (index.js?f26e:62)
at tryCatch (runtime.js?96cf:62)
at Generator.invoke [as _invoke] (runtime.js?96cf:296)
at Generator.prototype.<computed> [as next] (runtime.js?96cf:114)
at asyncGeneratorStep (asyncToGenerator.js?1da1:3)
at _next (asyncToGenerator.js?1da1:25)
For us, this shows whenever we try to use the SPA fallback or SPA mode, both in yarn dev and production. Universal/generated pages are fine, however the SPA fallback is a key part of our delivery strategy.
I can't say when exactly this started happening, but I can confirm my current nuxt version is 2.14.0 and my nuxt-apollo version is 4.0.0-rc13.1
From inspection, it looks like the NUXT object isn't hydrating successfully.
I see this declared in the page in SPA mode and seems to reflect the state when trying to reference the apollo client:
<script>window.__NUXT__={config:{},staticAssetsBase:void 0}</script>
And this is where the error is being triggered in apollo-module.js
if (!process.server) {
defaultCache.restore(window.__NUXT__ ? window.__NUXT__.apollo.defaultClient : null)
}
As you can see, if apollo isn't defined it causes the error.
Any help or insight into this issue would be greatly appreciated!!
Looks like this issue is actually resolved in the latest release candidate, and updating to at least 4.0.1-rc.3 should resolve it.
I like to think it's because of me somehow. See this commit for reference: https://github.com/nuxt-community/apollo-module/commit/640ba3e6e543e368cebbb0b707983e4e61856ba2