Skip to content
This repository was archived by the owner on Nov 18, 2024. It is now read-only.

Enable HMR #550

Merged
merged 2 commits into from
Apr 9, 2019
Merged

Enable HMR #550

merged 2 commits into from
Apr 9, 2019

Conversation

willdurand
Copy link
Member

@willdurand willdurand commented Apr 9, 2019

Fixes #275


I started from scratch, trying to understand whether CRA officially
supports HMR (hint: it is not as clear as it should be). In the end, I
added some code to enable HMR for React [1] and Redux [2]. It's similar
to what I've done for addons-frontend in the past, albeit much simpler.

Creating src/reducers/index.tsx is required so that we can track
changes on all reducers at once.


[1]: I tested by live editing a React component, which is updated in FF
without reloading the page. It's hot patched instead
[2]: I tested by adding a new prop to a reducer and it works too (it can
be monitored in the redux devtools)

@willdurand willdurand requested a review from kumar303 April 9, 2019 13:58
@codecov-io
Copy link

codecov-io commented Apr 9, 2019

Codecov Report

Merging #550 into master will increase coverage by 0.1%.
The diff coverage is 100%.

Impacted file tree graph

@@            Coverage Diff            @@
##           master     #550     +/-   ##
=========================================
+ Coverage   97.93%   98.03%   +0.1%     
=========================================
  Files          38       39      +1     
  Lines         967      968      +1     
  Branches      213      214      +1     
=========================================
+ Hits          947      949      +2     
+ Misses         19       18      -1     
  Partials        1        1
Impacted Files Coverage Δ
src/test-helpers.tsx 98.52% <ø> (ø) ⬆️
src/components/Navbar/index.tsx 100% <ø> (ø) ⬆️
src/components/LinterProvider/index.tsx 100% <ø> (ø) ⬆️
src/pages/Browse/index.tsx 100% <ø> (ø) ⬆️
src/pages/Compare/index.tsx 100% <ø> (ø) ⬆️
src/components/App/index.tsx 100% <ø> (ø) ⬆️
src/components/FileTree/index.tsx 100% <ø> (ø) ⬆️
src/components/VersionChooser/index.tsx 100% <ø> (ø) ⬆️
src/configureStore.tsx 75% <100%> (+5.76%) ⬆️
src/reducers/index.tsx 100% <100%> (ø)
... and 1 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 1b48cad...45c6c1c. Read the comment docs.

Copy link
Contributor

@kumar303 kumar303 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you look to see what's going on here?

STR:

The component updates but the tree collapses. It looks like it's replaying all Redux actions starting with initialState. It's not reloading the page, so that's good, but resetting the state defeats the purpose of HMR. Any idea what's going on?

@willdurand
Copy link
Member Author

Could you look to see what's going on here?

I'll try to fix it but I bet this is: reduxjs/redux#1940

@kumar303
Copy link
Contributor

kumar303 commented Apr 9, 2019

Hmm, that issue suggest that it is unfixable, no?

@willdurand
Copy link
Member Author

Hmm, that issue suggest that it is unfixable, no?

It seems it should just work, but it does not. We could maybe backup the state before hot reloading.

@kumar303
Copy link
Contributor

kumar303 commented Apr 9, 2019

We could maybe backup the state before hot reloading.

I suppose that will work. Creating a root reducer with a predefined state is well supported. I guess it's all still a workaround for facebook/create-react-app#6503 but that's OK.

@willdurand
Copy link
Member Author

Actually, I think this is an issue with our app and not with the HMR setup.

@willdurand willdurand requested a review from kumar303 April 9, 2019 16:42
@willdurand
Copy link
Member Author

Also, we can't really backup the state before applying the HMR patch. Redux does not allow that.

@kumar303
Copy link
Contributor

kumar303 commented Apr 9, 2019

I think this is an issue with our app and not with the HMR setup.

Can you file it with STR so we don't forget to fix it?

Copy link
Contributor

@kumar303 kumar303 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

r+, ship it. Thanks!

I tested component changes, style changes, and reducer changes. It all seems to work but none of our components can handle a re-mount with existing state 😂 so I'm not totally sure if HMR is behaving how we want it to.

It's somewhat surprising to me that HMR re-mounts components that are already on the screen. I thought it was only supposed to update them. In any case, that's fine because we can deal with it. We can fix each component to reflect Redux state correctly.

I didn't think we needed all components to reflect Redux state for HMR support but that makes sense now that I think about it. I knew we would need it for SSR but we don't need SSR so we've been lax about reflecting Redux state.

@willdurand
Copy link
Member Author

Can you file it with STR so we don't forget to fix it?

I filed #551

@willdurand willdurand merged commit d75a64f into master Apr 9, 2019
@willdurand willdurand deleted the hmr branch April 9, 2019 17:23
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants