Home > Back-end >  Use Redux Offline with RTK-Query
Use Redux Offline with RTK-Query

Time:06-14

Has anyone been successful in marrying Redux Offline with RTK Query?

I'm unsure how to decorate RTK query mutations with offline metadata, as described in the RTK Query docs:

const registerUser = (name, email) => ({
  type: 'REGISTER_USER',
  payload: { name, email },
  meta: {
    offline: {
      // the network action to execute:
      effect: { url: '/api/register', method: 'POST', body: `name=${name}&email=${email}`, headers: { 'content-type': 'application/x-www-form-urlencoded' } },
      // action to dispatch when effect succeeds:
      commit: { type: 'REGISTER_USER_COMMIT', meta: { name, email } },
      // action to dispatch if network action fails permanently:
      rollback: { type: 'REGISTER_USER_ROLLBACK', meta: { name, email } }
    }
  }
});

Basically I want to add mutations (POST requests) that failed due to the device being offline to a queue, which will be processed if the device is online again.

CodePudding user response:

Author of RTK Query here: RTK Query does not have any mechanisms to integrate with Redux Offline, and I have never worked with Redux Offline myself. So unless Redux Offline has any such mechanisms to integrate with RTK Query, it's probably just never been implemented by anyone and those two are independent systems without any relation between them.

It's probably a better idea to look into a Web Worker that does this for you? Not 100% sure if that's the best way though - I never had an "offline" use case/requirement myself.

  • Related