Polymer / pwa-starter-kit

Starter templates for building full-featured Progressive Web Apps from web components.
https://pwa-starter-kit.polymer-project.org
2.36k stars 431 forks source link

[Improvement] Don't dispatch redux action during watcher installation #312

Closed Yuen92 closed 4 years ago

Yuen92 commented 5 years ago

During the load of the application (my-app.js) in the firstUpdated() we install some watcher in order to call redux action when :

When we register them we dispatch the action uselessly.

Do you have a way to don't dispatch the action during the watcher installation? Or does the enhancement should be at utility level? I mean move the issue/request directly in pwa-helpers ?

Expected Result : The 3 actions UPDATE_PAGE, UPDATE_DRAWER_STATE, UPDATE_OFFLINE will not be performed during the application loading (only the first one isn't due to the watcher installation) image

keanulee commented 5 years ago

The initial route/network/viewport state needs to be set somehow, and I'd be interested to see alternative approaches to this (my experience with Redux before this project is limited).

Yuen92 commented 5 years ago

Sorry @keanulee my bad for the initial route. As you said we need the initial UPDATE_PAGE to define the route from the window.document.location.pathname : image

But for the drawer state we can avoid the initial UPDATE_DRAWER_STATE since we already set the state in the src/reducers/app.js with the constant INITIAL_STATE. const INITIAL_STATE = { page: '', offline: false, drawerOpened: false, snackbarOpened: false, }; So that's why the state doesn't change during the 3 UPDATE_DRAWER_STATE action : image

Same for UPDATE_OFFLINE.

If we are able to call only action needed during the initial loading then we are able to reuse the State from Redux for an anylitics purpose using Google Tag Manager/Google Analytics. Then it will be easier to explain to business people all events. We will have something like : UPDATE_PAGE => pageview UPDATE_DRAWER_STATE => events

If we still have some unecessary UPDATE_DRAWER_STATE so we have to manage them inside actions/app.js.

Note : This improvement is really minor but it seems cleaner if we can avoid these store.dispatch. In term of javascript execution optimisation we are talking less than 1ms on mobile (CPU 4xslowdown) : image image

The way it's mesured : image

stale[bot] commented 4 years ago

This project is no longer under development and will be transitioning to a read-only repo. Thank you for your contributions.