Next.js: cache reset doesn't work in next 7.0.0 with-apollo-auth

Created on 21 Sep 2018  路  6Comments  路  Source: vercel/next.js

Examples bug report

Example name

with-apollo-auth

Describe the bug

I need to refresh the page after performing login/register/logout to see the result.

To Reproduce

Steps to reproduce the behavior, please provide code snippets or a repository:

  1. yarn create next-app --example with-apollo-auth with-apollo-auth-app
  2. update dependencies #5242
  3. yarn dev
  4. try to login with valid credentials

Expected behavior

After login I should see the logged in page.

System information

  • OS: Linux
  • Browser (if applies) Chrome
  • Version of Next.js: 7.0.0

Additional context

I think it's connected to this piece of code:

// Force a reload of all the current queries now that the user is
// logged in
client.cache.reset().then(() => {
  redirect({}, '/')
})

It seems the reset is not performed, as I need to refresh the page to see the desired result (same with register, logout).

good first issue

Most helpful comment

@ghost and @okankayhan I also found the same behavior. I believe what is happening is withApollo.getInitialProps is responsible for providing the token to withApollo.ctor but since withApollo.getInitialProps does not re-run, the token is not provided in getToken(). Using the sample project, I replaced

constructor (props) {
      super(props)
      // `getDataFromTree` renders the component first, the client is passed off as a property.
      // After that rendering is done using Next's normal rendering pipeline
      this.apolloClient = initApollo(props.apolloState, {
        getToken: () => props.token
      })
    }

With:

constructor (props) {
      super(props)
      // `getDataFromTree` renders the component first, the client is passed off as a property.
      // After that rendering is done using Next's normal rendering pipeline
      this.apolloClient = initApollo(props.apolloState, {
        getToken: () => parseCookies().token
      })
    }

This seems to have fixed the issue.

All 6 comments

Taking redirect out of cache reset also doesn't make any change to the behavior.

Cache reset definitely does not reset the cache.
Redirect doesn't redirect from signin to index. It does redirect from index page when you logout though. However since cache is not reset, signin page redirects you back to the index page.

Seeing a related issue: After setting the cookies, I can only get data after manually refreshing the page. Redirects and/or cache resets don't do the trick.

@ghost and @okankayhan I also found the same behavior. I believe what is happening is withApollo.getInitialProps is responsible for providing the token to withApollo.ctor but since withApollo.getInitialProps does not re-run, the token is not provided in getToken(). Using the sample project, I replaced

constructor (props) {
      super(props)
      // `getDataFromTree` renders the component first, the client is passed off as a property.
      // After that rendering is done using Next's normal rendering pipeline
      this.apolloClient = initApollo(props.apolloState, {
        getToken: () => props.token
      })
    }

With:

constructor (props) {
      super(props)
      // `getDataFromTree` renders the component first, the client is passed off as a property.
      // After that rendering is done using Next's normal rendering pipeline
      this.apolloClient = initApollo(props.apolloState, {
        getToken: () => parseCookies().token
      })
    }

This seems to have fixed the issue.

Feel free to send a pull-request @zachariahtimothy

Roger that @timneutkens , I will fix that up today now that mine has been working for a while.

馃檶 Awesome!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

formula349 picture formula349  路  3Comments

swrdfish picture swrdfish  路  3Comments

renatorib picture renatorib  路  3Comments

irrigator picture irrigator  路  3Comments

havefive picture havefive  路  3Comments