Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(hmr): accept hot updates for modules above page templates #29752

Merged
merged 10 commits into from
Feb 27, 2021

Conversation

pieh
Copy link
Contributor

@pieh pieh commented Feb 24, 2021

Description

After dropping react-hot-loader we need to accept hot updates ourselves. We do so today for page templates but not above those. Anything imported by gatsby-browser files is above page templates in webpack's hierarchy and so those are not handled.

TODO:

  • add tests to e2e/development-runtime
  • try to get rid of explicit re-rerending of root and hopefuly let fast-refresh do this job for us

Related Issues

system-ui/theme-ui#1440 (not in our repo, but pretty much our issue)

@gatsbot gatsbot bot added the status: triage needed Issue or pull request that need to be triaged and assigned to a reviewer label Feb 24, 2021
@pieh pieh added topic: core Relates to Gatsby's core (e.g. page loading, reporter, state machine) and removed status: triage needed Issue or pull request that need to be triaged and assigned to a reviewer labels Feb 24, 2021
Comment on lines 775 to 790
// this is a bit hacky - exclude expect string or regexp or array of those
// so this is tricking ReactRefreshWebpackPlugin that we provide RegExp
exclude: {
test: modulePath => {
// when it's not coming from node_modules we treat it as a source file.
if (!vendorRegex.test(modulePath)) {
return false
}

// If the module uses Gatsby as a dependency
// we want to treat it as src because of shadowing
return !modulesThatUseGatsby.some(module =>
modulePath.includes(module.path)
)
},
} as RegExp,
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This feels very hacky and if we would go with this - I'd probably suggest pinning @pmmmwh/react-refresh-webpack-plugin to exact version as this only implement test method of RegExp and this might break spectacularly if other methods would be used in future versions.

Ideally we could ask for support of function for exclude, but this is also weird because that webpack plugin use helpers provided by webpack ( https://github.com/webpack/webpack/blob/04a7d052b5c04628257ec23244f234622c7f394c/lib/ModuleFilenameHelpers.js#L230-L257 ) which just don't handle functions (weird part to me is that rules do, so why would that helper not support is a bit baffling)

Comment on lines +115 to +117
function RootWrappedWithOverlayAndProvider() {
return (
<FastRefreshOverlay>
<StaticQueryStore>{rootWrappedWithWrapRootElement}</StaticQueryStore>
</FastRefreshOverlay>
)
}

export default RootWrappedWithOverlayAndProvider
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Name our Root Component :)

Comment on lines -163 to -164
const preferDefault = m => (m && m.default) || m
const Root = preferDefault(require(`./root`))
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This was causing some weird module dependency chains and it wasn't exactly playing nice with HMR - hot updates for gatsby-browser modules were declined even tho I was accepting them on line 23 (for ./api-runner-browser).

I'm not exactly sure why it was done this way (delayed commonjs import) :/

@pieh pieh force-pushed the v3/accept-hot-update-above-page-templates branch from ec4b6d2 to 7e1a0f4 Compare February 26, 2021 10:44
@pieh pieh force-pushed the v3/accept-hot-update-above-page-templates branch from 81b6e7d to 168a79c Compare February 26, 2021 15:38
@pieh pieh marked this pull request as ready for review February 26, 2021 17:01
@gatsbot
Copy link

gatsbot bot commented Feb 26, 2021

Danger run resulted in 1 fail and 1 markdown; to find out more, see the checks page.

Generated by 🚫 dangerJS

Comment on lines +26 to +28
export const wrapRootElement = ({ element }) => (
<WrapRootElement element={element} />
)
Copy link
Contributor Author

@pieh pieh Feb 27, 2021

Choose a reason for hiding this comment

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

This is actually important - whatever we pass to wrapRootElement (or wrapPageElement) is NOT treated as component. We just straight call this API (and not use React.createElement) - that's because this is gatsby-browser API hook and we also pass pluginOptions as second parameter. Because of above this is actually not represented as React Component in react tree and react-refresh won't actually apply HMR to it (even tho webpack plugin would treat Wrapper as react component and register it as such). On top of problems there are multiple exports in gatsby-browser and that is also no-no for react-refresh.

Above is also reason why we can't use hooks in there etc

This might need some additional notes in https://www.gatsbyjs.com/docs/reference/config-files/gatsby-browser/#wrapPageElement (and maybe migration guide?)

Copy link
Contributor

@sidharthachatterjee sidharthachatterjee left a comment

Choose a reason for hiding this comment

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

This is a great change. Nice work @pieh

@sidharthachatterjee sidharthachatterjee merged commit 55778eb into master Feb 27, 2021
@sidharthachatterjee sidharthachatterjee deleted the v3/accept-hot-update-above-page-templates branch February 27, 2021 15:49
vladar pushed a commit that referenced this pull request Feb 28, 2021
* fix(hmr): accept hot updates for modules above page templates

* actually use fast-refresh for gatsby-browser and shadowed theme-ui config

* exclude actually need to be instance of RegExp

* tmp

* more tmp

* init navigation after loader is set

* cleanup tmp code

* init navigation after resources for current page are loaded

* remove commented out code

* revert devtool change

(cherry picked from commit 55778eb)
vladar pushed a commit that referenced this pull request Feb 28, 2021
#29835)

* fix(hmr): accept hot updates for modules above page templates

* actually use fast-refresh for gatsby-browser and shadowed theme-ui config

* exclude actually need to be instance of RegExp

* tmp

* more tmp

* init navigation after loader is set

* cleanup tmp code

* init navigation after resources for current page are loaded

* remove commented out code

* revert devtool change

(cherry picked from commit 55778eb)

Co-authored-by: Michal Piechowiak <misiek.piechowiak@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
topic: core Relates to Gatsby's core (e.g. page loading, reporter, state machine)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants