{"action":"created","issue":{"url":"https://api.github.com/repos/heroku/react-refetch/issues/40","labels_url":"https://api.github.com/repos/heroku/react-refetch/issues/40/labels{/name}","comments_url":"https://api.github.com/repos/heroku/react-refetch/issues/40/comments","events_url":"https://api.github.com/repos/heroku/react-refetch/issues/40/events","html_url":"https://github.com/heroku/react-refetch/issues/40","id":127350973,"number":40,"title":"Offline Support","user":{"login":"hnordt","id":1625399,"avatar_url":"https://avatars.githubusercontent.com/u/1625399?v=3","gravatar_id":"","url":"https://api.github.com/users/hnordt","html_url":"https://github.com/hnordt","followers_url":"https://api.github.com/users/hnordt/followers","following_url":"https://api.github.com/users/hnordt/following{/other_user}","gists_url":"https://api.github.com/users/hnordt/gists{/gist_id}","starred_url":"https://api.github.com/users/hnordt/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/hnordt/subscriptions","organizations_url":"https://api.github.com/users/hnordt/orgs","repos_url":"https://api.github.com/users/hnordt/repos","events_url":"https://api.github.com/users/hnordt/events{/privacy}","received_events_url":"https://api.github.com/users/hnordt/received_events","type":"User","site_admin":false},"labels":[{"url":"https://api.github.com/repos/heroku/react-refetch/labels/discussion","name":"discussion","color":"d4c5f9"}],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":3,"created_at":"2016-01-19T02:29:17Z","updated_at":"2016-01-19T16:25:24Z","closed_at":null,"body":"I'm working on a project that needs offline support and I don't want to use Flux, because all state is saved on server-side and I don't wanna duplicate/manage local state.\r\n\r\nWould be awesome if we can provide our own implementation of `handleResponse`, so we can implement something like this:\r\n\r\n```js\r\n// myOwnHandleResponse.js\r\n\r\nimport localforage from 'localforage';\r\n\r\nconst getRequestId = request => {\r\n  // TODO: md5 or something like that :P\r\n  return `${request.url}.${request.method}.${JSON.stringify(request.headers)}`;\r\n};\r\n\r\n// On response success update offline cache and return json\r\n// On response failure warn and return the cached json\r\nconst handleResponse = (request, response) => {\r\n  const requestId = getRequestId(request);\r\n  if (response.status >= 200 && response.status < 300) { // Or response.ok\r\n    const json = response.json();\r\n    // localforage.setItem() resolves the value that was set :)\r\n    return localforage.setItem(requestId, json).catch(error => {\r\n      console.warn('Failed to update offline cache', { requestId, request, error });\r\n      return json;\r\n    });\r\n  } else {\r\n    return localforage.getItem(requestId);\r\n  }\r\n};\r\n\r\nexport default handleResponse;\r\n```\r\n\r\nWe'll need to implement a timeout in React Refetch and implement a way to handle POST/PUT/DELETE requests.\r\nIt's just the initial idea. Toughts?"},"comment":{"url":"https://api.github.com/repos/heroku/react-refetch/issues/comments/172906354","html_url":"https://github.com/heroku/react-refetch/issues/40#issuecomment-172906354","issue_url":"https://api.github.com/repos/heroku/react-refetch/issues/40","id":172906354,"user":{"login":"hnordt","id":1625399,"avatar_url":"https://avatars.githubusercontent.com/u/1625399?v=3","gravatar_id":"","url":"https://api.github.com/users/hnordt","html_url":"https://github.com/hnordt","followers_url":"https://api.github.com/users/hnordt/followers","following_url":"https://api.github.com/users/hnordt/following{/other_user}","gists_url":"https://api.github.com/users/hnordt/gists{/gist_id}","starred_url":"https://api.github.com/users/hnordt/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/hnordt/subscriptions","organizations_url":"https://api.github.com/users/hnordt/orgs","repos_url":"https://api.github.com/users/hnordt/repos","events_url":"https://api.github.com/users/hnordt/events{/privacy}","received_events_url":"https://api.github.com/users/hnordt/received_events","type":"User","site_admin":false},"created_at":"2016-01-19T16:25:24Z","updated_at":"2016-01-19T16:25:24Z","body":"@ryanbrainard it's not so simple, offline POST/PUT/DELETE requests need to be queued, so we can dispatch them when we back online. We'll need to do something like:\r\n\r\n```js\r\nconst handleRequest = request => {\r\n  if (request.method === 'POST' && isOffline) {\r\n    return localforage.setItem('queue', [...queue, request]);\r\n  }\r\n  // ...\r\n};\r\n```"}}