localnerve/flux-react-example-sw

Update/Fix Init

Closed this issue · 0 comments

I'm still unsure about exactly what needs to happen here. This captures the ideas.
This exists because init runs from cached pages with stale app state. That is broken.
Found because csrfToken was always wrong (stale) after first run.

Init should only be able to run under specific conditions:

  1. When the service worker starts up, provided there is data in the init IDBObjectStore.
    2. From the page, provided the page is not served from cache.
    (Nope, this still needs to happen to ensure app state updates are retrieved if the app is online)
    (If the app is offline, updates won't happen, the old state will still be there, deferredPosts will still get queued, etc)

    This can happen from the route handler. The route handler should be modified to update state when these requests happen.

Move IDBObjectStore updates from the main init sequence to inside init.routes fetch, but only extract app.state from the response representing the current location. This should always update the init IDBObjectStore with the latest app.state from the server, and remove any need to test for online-ness.

This also removes the need to send a payload to the init command - except that the equivalent will need to be read/returned from the init.routes, where init.routes runs first in the init sequence.
Maybe always first run init.routes just for the current location. This will yield the desired app.state, update init IDBObjectStore if online. If offline, read the init IDBObjectStore and return the result. From there, the rest of routes and backgrounds can run (the rest of the init sequence).

There are some good ideas here that can lead to other gains, but this specific issue was fixed by simply round-tripping the csrf token for mainNav route requests (initiated by init.routes in service worker).