Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to disable cache #890

Closed
LijuanZ opened this issue May 25, 2018 · 9 comments

Comments

Projects
None yet
3 participants
@LijuanZ
Copy link

commented May 25, 2018

Hello, I'm using octokit in my node.js SPA and have trouble to disable cache in the requests.

I tried to get the whole branch list and then create a new branch and fetch branch list again in one of my work flows and these requests happen in less than 1min. The new branch does not exist in my second call of fetching branch list because browser cached the results. I find there is header of Cache-Control: public, max-age=60, s-maxage=60 in the response and believe this is the cause. It works fine if I disable cache in my browser or slow down the work flow to take more than 1min.

I've tried set Cache-Control header in my request to disable cache but failed because of error "Request header field cache-control is not allowed by Access-Control-Allow-Headers in preflight response."

Is there a way to force octokit to make the request no matter there is cache or not?

Thanks!

@gr2m

This comment has been minimized.

Copy link
Collaborator

commented May 25, 2018

Hi @LijuanZ thanks for opening the issue!

I’m out right now and can’t help much, but you can pass custom headers to every method, maybe try to pass a header that prevents caching, I thing GitHub’s Rest API docs have some info on that. I’ll have a look tomorrow if you are still stuck on it

@LijuanZ

This comment has been minimized.

Copy link
Author

commented May 25, 2018

Hi @gr2m I'm still stuck...Didn't find any cache related custom header in API docs or maybe I'm looking at wrong place. Can you help take a look when have time?

Thanks!

@gr2m

This comment has been minimized.

Copy link
Collaborator

commented May 25, 2018

Okay I'm on it

@gr2m gr2m self-assigned this May 25, 2018

@gr2m

This comment has been minimized.

Copy link
Collaborator

commented May 25, 2018

is your code public somewhere?

@LijuanZ

This comment has been minimized.

Copy link
Author

commented May 25, 2018

No it's not public. But basically it calls octokit.repos.getBranches(callContext) to get branch list, and then octokit.gitdata.createReference(callContext) to create new branch, and then calls octokit.repos.getBranches(callContext) again. All these requests are from my client side code.

The new branch is created successfully but the second getBranches call doesn't have it because browser reads it from cache.

@gr2m

This comment has been minimized.

Copy link
Collaborator

commented May 25, 2018

Usually one would do something like this

  await client.repos.getBranches({
    owner: 'gr2m',
    repo: 'sandbox',
    headers: {
      'cache-control': 'no-cache'
    }
  })

But turns out that header is not allowed for cross origin requests. You will see an error like this:

image

I’ll check on that with the AP team. For the time being you can use the If-None-Match header as described here: https://developer.github.com/v3/#conditional-requests. If you leave the value empty then it will never match and the server will never return a cached response:

  const {data: branches2} = await client.repos.getBranches({
    owner: 'gr2m',
    repo: 'sandbox',
    headers: {
      'If-None-Match': ''
    }
  })

Does that work for you?

@gr2m gr2m added the question label May 25, 2018

@LijuanZ

This comment has been minimized.

Copy link
Author

commented May 25, 2018

Yeah, cache-control header was my first attempt but failed on preflight. If-None-Match header with empty eTag does the trick for me! Thank you very much for the help!

@gr2m

This comment has been minimized.

Copy link
Collaborator

commented May 25, 2018

Thanks for checking it, glad it works now. Feel free to add comments if you have any follow up questions :)

@gr2m gr2m closed this May 25, 2018

@mddanishyusuf

This comment has been minimized.

Copy link

commented Apr 30, 2019

@gr2m Thanks for the solution. It's saved my day. :)

Yes, 'If-None-Match': '' is worked.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.