json-server cache the data?

Created on 31 Aug 2015  Â·  22Comments  Â·  Source: typicode/json-server

I run json-server by json-server --watch db.json.

I modified the db.json, then the terminal shows db.json has changed, reloading...,( it shows twice..) everything looks well.

But when i got the data by AJAX. The response not changed.
At first, i thought my chrome may cache the data, so add time to the URL like this posts?_limit=1441026369153. But it doesn't work.

Then i use my safari to get the data, i get the new data. I refresh my chrome, still not work.

Wow, after i write this bug report, i refresh my chrome. I got the new data.....O__O "…
~(~ ̄▽ ̄)~

+_+... OS: Windows 10 Pro 64, Browser: Chrome 44.0.2403.157 m, what's wrong with my operation.
Etag? Expire=-1? Cache-Control=no-cache? Everything looks normal.
Thanks~~ O(∩_∩)O

Most helpful comment

To resurrect an ancient thread - NOTHING I do will get json-server to acknowledge changes to the db.json file. I have closed all browsers etc. (which is a pain enough in a workflow...). REST calls change the data, that is fine but how do I reset json-server to use what is in the db.json file??

All 22 comments

Hi @zhoukekestar,

Data is not cached by json-server. So, I don't really know why you have that. In Chrome DevTools, I would try disabling cache.

Out of curiosity, what IDE are you using (it's for db.json has changed, reloading... being shown twice)?

Twice gif:
twice

Cahce... o(╯□╰)o
cache

I check everything i can:
Check headers: no-cache, Expire, ETag?
Add random string to the end of url.
Disable cache in my chrome devtools.

That's strange, especially because in safari you get --addsomething but not in Chrome.

o(╯□╰)o.......

I have just experienced this exact same issue. No combination of cache clearing/disabling and reloading in Chrome will yield the latest data, Safari loads the same data and sees the latest. Restart json-server and every client gets the latest. I will see what I can turn up but this is baffling!

Edit: Safari exhibits the same behaviour so it seems not to be a browser issue

I have the same issue. I have another Javascript changing the json file and the server does not update the content unless I restart the server. It makes a snapshot of the file and them runs the server?

@rodrigoduranna

I have the same issue. I have another Javascript changing the json file and the server does not update the content unless I restart the server. It makes a snapshot of the file and them runs the server?

I have same problem.

I still have this issue in 0.8.4, @typicode have you been able to reproduce this issue?
I'm on Mac OS X 10.11.2 with node 5.2.0

Have you tried running json-server without the --watch flag?

I have but that avoids the issue entirely - the problem is that I want --watch to update the data that is served by the API. At the moment it is only new clients that get the updated data - somehow clients that have already been served the data before it was updated continue to get the old data as if json-server is caching it per-client somehow. It is browser independent, and not a browser caching issue.

I do think it's fixed now.

The problem was that when reloading json-server, I was using close() to close the previous instance and start a new one. But close() actually closes the server only after all the connections have been closed.

Since browsers send a Connection: keep-alive header, the connection to the previous instance, with old data, was kept being used. And that's why also when opening a different browser, the new instance was used.

Let me know if you still have problems.

Fantastic, thank you. I will give it a try as soon as I can.

You're welcome, thank you all for reporting this tricky issue and for your patience ^_^

WOW ! ~ Good job.

Works for me :)

Great :)

To resurrect an ancient thread - NOTHING I do will get json-server to acknowledge changes to the db.json file. I have closed all browsers etc. (which is a pain enough in a workflow...). REST calls change the data, that is fine but how do I reset json-server to use what is in the db.json file??

Oh my, that was a long time ago. I really can't recall, sorry!

On 29 Mar 2018 23:48, sigmazen notifications@github.com wrote:

@kirstyannepollockhttps://github.com/kirstyannepollock Did you solve this? I've just hit the same issue ... POSTed to db.json which is successful but if I try to GET it by id it does not come back (even though I can see it in the file)

—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHubhttps://github.com/typicode/json-server/issues/177#issuecomment-377382755, or mute the threadhttps://github.com/notifications/unsubscribe-auth/AOd3AvXxqb2ZNqzisR0AojvJlxvNXapCks5tjVangaJpZM4F1CBj.

Two things which helped my caching woes:

  1. adding router.db.read('db.json'); in my router.render (see docs) when I did any manual updates to my db.json with lowdb

  2. adding nocache (e.g. server.use(nocache())). This adds no-cache headers to your responses so you don't get 304 Not Modified statuses from your requests

Hello everyone,
I this issues with my react app. json-server serves something else other than what I have in the db.json

`Resources
http://localhost:3001/posts
http://localhost:3001/comments
http://localhost:3001/profile

Home
http://localhost:3001`

posts and profile does not exists in my db.json data.

GET http://localhost:3001/dishes 404 (Not Found)

I need help with this please.

Hello everyone,
I this issues with my react app. json-server serves something else other than what I have in the db.json

`Resources
http://localhost:3001/posts
http://localhost:3001/comments
http://localhost:3001/profile

Home
http://localhost:3001`

posts and profile does not exists in my db.json data.

GET http://localhost:3001/dishes 404 (Not Found)

I need help with this please.

SOLVED IT.

I FORGOT TO CHANGE THE DIRECTORY WHERE THE JSON SERVER WAS INSTALLED, CHANGING DIRECTORY SOLVED IT FOR ME.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

TXRRNT picture TXRRNT  Â·  4Comments

goldmont picture goldmont  Â·  3Comments

0plus1 picture 0plus1  Â·  3Comments

Isanderthul picture Isanderthul  Â·  3Comments

bahmutov picture bahmutov  Â·  3Comments