apollo icon indicating copy to clipboard operation
apollo copied to clipboard

apollo not initialized under spa mode with redirect

Open look416 opened this issue 5 years ago • 2 comments

Version

v4.0.0-rc.19

Reproduction link

https://github.com/look416/nuxt-apollo-error-demo

Steps to reproduce

  1. Set the mode to "universal" under nuxt config
  2. import redirect under auth middleware
  3. check apollo token
  4. call redirect function if token not found
  5. yarn generate
  6. check it using simple http server

What is expected ?

  1. The page is rendered successfully

What is actually happening?

  1. Error popping: Screenshot 2019-12-19 at 12.01.07 PM.png

Additional comments?

As of now, I am managed to bypass the issue by either two ways:

  1. setting the mode to "spa"
  2. instead of importing redirect, use the app.router to do the redirection
This bug report is available on Nuxt community (#c271)

look416 avatar Dec 19 '19 04:12 look416

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

enigmatix avatar Aug 06 '20 02:08 enigmatix

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

enigmatix avatar Aug 13 '20 00:08 enigmatix