klarna / electron-redux

Use redux in the main and browser processes in electron
MIT License
743 stars 94 forks source link

Actions from main process not received on app start using preload script #254

Closed sapkra closed 3 years ago

sapkra commented 4 years ago

At the first start of the application I'm writing the active user of the authentication cookie to the redux store. But when my app initially loaded the activeUser value is still null (the default value of the store in the main process). Only after a reload it is working fine.

What I have found out so far:

  1. The main process is initialize with an empty activeUser
  2. The preload is dispatching an action which will only be send to the main process
  3. The main process is receiving the action and is changing the main store
  4. The main process is sending back the action to the renderer
  5. The renderer is not receiving the action via the ipcRenderer.on() listener
  6. After a reload the renderer will receive the action

Solution 1 (recommended)

One solution (and the best IMO) would be not to ignore the actions within the renderers, but to ignore them instead in the forwardToRenderer function because then you don't have to forward them from the main process back to the renderer that was originally the root of that action which will also speed up the app a little bit. To achieve this, it would be necessary to add an identifier to the renderers.

Solution 2 (workaround)

The second solution would be to wait when the window is loaded and then send the event actions. The problem would be that this could easily lead to a slower UX and also to flickering of the ui during the first render, just in case you have exactly this problem. https://github.com/klarna/electron-redux/blob/c6f8521cbc0c29a0efd9795486b477e40834d40e/packages/electron-redux/src/middleware/forwardToRenderer.js#L20 This line could be replaced with something similar to the following one:

if (contents.isLoadingMainFrame()) {
  contents.on('did-finish-load', () => {
    contents.send('redux-action', rendererAction);
  });
} else {
  contents.send('redux-action', rendererAction);
}

Desktop:

sapkra commented 3 years ago

Some fixes are now applied in v2 but the new version introduced a new related problem which will be solved within #278.