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

HMR super slow with React project since 15.6.0 #14580

Closed
bdebon opened this issue Jan 24, 2023 · 18 comments · Fixed by #14915
Closed

HMR super slow with React project since 15.6.0 #14580

bdebon opened this issue Jan 24, 2023 · 18 comments · Fixed by #14915
Assignees
Labels
outdated scope: react Issues related to React support for Nx type: bug

Comments

@bdebon
Copy link

bdebon commented Jan 24, 2023

Current Behavior

Up until our last migration to 15.6.0 (we were in 15.5.1), adding a console.log somewhere in the code was compiling in less than a second and was added to our browser project without any refresh.
Since the update to 15.6.0 the compilation takes something like 8s and force a refresh of our browser page.

Expected Behavior

We would like the compilation to take the same time that before the migration and that the page is not "forced-refresh".

Github Repo

https://github.com/Qovery/console

Steps to Reproduce

  1. fetching a branch before the migration (eg: main), rm -rf node_modules yarn.lock && yarn
  2. add a console.log anywhere and see how long the terminal takes to compile
  3. do the same from a branch with the migration on (ex: staging)

Nx Report

>  NX   Report complete - copy this into the issue template

   Node : 19.0.1
   OS   : darwin arm64
   yarn : 1.22.17
   
   nx : 15.0.3
   @nrwl/angular : Not Found
   @nrwl/cypress : 15.0.3
   @nrwl/detox : Not Found
   @nrwl/devkit : 15.0.3
   @nrwl/esbuild : Not Found
   @nrwl/eslint-plugin-nx : 15.0.3
   @nrwl/expo : Not Found
   @nrwl/express : Not Found
   @nrwl/jest : 15.0.3
   @nrwl/js : 15.0.3
   @nrwl/linter : 15.1.0
   @nrwl/nest : Not Found
   @nrwl/next : Not Found
   @nrwl/node : Not Found
   @nrwl/nx-cloud : 15.0.2
   @nrwl/nx-plugin : Not Found
   @nrwl/react : 15.0.3
   @nrwl/react-native : Not Found
   @nrwl/rollup : 15.0.3
   @nrwl/schematics : Not Found
   @nrwl/storybook : 15.0.3
   @nrwl/web : 15.0.3
   @nrwl/webpack : 15.0.3
   @nrwl/workspace : 15.0.3
   typescript : 4.8.4
   ---------------------------------------
   Local workspace plugins:
   ---------------------------------------
   Community plugins:

Failure Logs

No response

Additional Information

No response

@AgentEnder AgentEnder added the scope: react Issues related to React support for Nx label Jan 24, 2023
@nemonemi
Copy link

Hi bdebon, do you, by any chance, extend the webpack configuration, in any fashion?

I have noticed the same behavior. Here's the ticket where I've reported it at #13125

@bdebon
Copy link
Author

bdebon commented Jan 25, 2023

Hello @nemonemi, I'm affraid wa have not touched anything about webpack configuration. The only module.exports references in the project I found was about tailwind configurations. We have this babel.config.json at the root of our project but not sure it is what you're mentioning:


module.exports = {
  ...rootMain,
  core: { ...rootMain.core, builder: 'webpack5' },
  stories: [...rootMain.stories, '../src/**/**/*.stories.mdx', '../src/**/**/*.stories.@(js|jsx|ts|tsx)'],
  addons: [...rootMain.addons, '@nrwl/react/plugins/storybook'],
  staticDirs: ['../src/lib'],
  webpackFinal: async (config, { configType }) => {
    // apply any global webpack configs that might have been specified in .storybook/main.js
    if (rootMain.webpackFinal) {
      config = await rootMain.webpackFinal(config, { configType })
    }

    // add your own webpack tweaks if needed

    return config
  },
}

@PointSingularity
Copy link

Have the same issue. The webpack config is the default one.

@nemonemi
Copy link

nemonemi commented Jan 25, 2023

@bdebon, in the example repo, that I've added to my ticket I've showcased how a simple custom Webpack extension breaks the HMR.
When logging the inherited Webpack config:

import { merge } from 'webpack-merge';

module.exports = (config: any) => {
  console.log(config); // I have noticed that HMR comes configured properly as "hot: true", but the HMR, after the merge, doesn't work

  return merge(config, {
    module: {
      rules: [
        {
          test: /\.pdf$/i,
          use: ['file-loader'],
        },
      ],
    },
  });
};

As far as I've observed, the output of the merge does not change the hot: true configuration in any fashion.

@bdebon
Copy link
Author

bdebon commented Jan 25, 2023

Yes I saw that, and I understand that it could be an interesting lead, but I have not touched anything webpack related in my project... Let's see if other people get the same issue after migrating, for now, I postponed a little bit our migration.
If you want I try anything else tell me and I will!

@PointSingularity
Copy link

PointSingularity commented Jan 26, 2023

I think I maybe figured what is the root cause when I noticed my ReactQuery DevTools not showing up:

The process.env.NODE_ENV is undefined, which leads to HMR being disabled as it is running in production mode.
That is maybe why everything is so slow.
Possibly other env variables are also undefined, haven't checked.

Edit: there is already an issue about this #14547

@JackMorrissey
Copy link

I think I maybe figured what is the root cause when I noticed my ReactQuery DevTools not showing up:

The process.env.NODE_ENV is undefined, which leads to HMR being disabled as it is running in production mode. That is maybe why everything is so slow.

Edit: there is already an issue about this #14547

Thanks for the hint. I was able to manually set the development variable in our package.json start script and HMR is behaving as expected. Good workaround for the time being.

package.json

{
  "scripts": {
    "start": "cross-env NODE_ENV=development nx serve our-app"
    ...
  }
}

@PointSingularity
Copy link

I think I maybe figured what is the root cause when I noticed my ReactQuery DevTools not showing up:
The process.env.NODE_ENV is undefined, which leads to HMR being disabled as it is running in production mode. That is maybe why everything is so slow.
Edit: there is already an issue about this #14547

Thanks for the hint. I was able to manually set the development variable in our package.json start script and HMR is behaving as expected. Good workaround for the time being.

package.json

{
  "scripts": {
    "start": "cross-env NODE_ENV=development nx serve our-app"
    ...
  }
}

@JackMorrissey No problem, glad you found a workaround 👍

@nemonemi
Copy link

After updating from 15.4.2 to 15.6.2 I can confirm the exact behavior of not having the NODE_ENV set.
Of course, it can be set manually, but, I would say, this is a regression in previously working behavior.

Also, for me, this version either changes nothing, since with the extended webpack config, the full-page-reload occurs, instead of HMR.

@PointSingularity
Copy link

Still present as of 15.6.3

@Nightbr
Copy link

Nightbr commented Jan 31, 2023

We are experiencing this issue too in 15.6.3

Workaround, add in workspace root .local.env with NODE_ENV=development

@bheftel
Copy link

bheftel commented Feb 3, 2023

After updating from 15.4.2 to 15.6.2 I can confirm the exact behavior of not having the NODE_ENV set. Of course, it can be set manually, but, I would say, this is a regression in previously working behavior.

Also, for me, this version either changes nothing, since with the extended webpack config, the full-page-reload occurs, instead of HMR.

@nemonemi - I had this same issue, but setting NODE_ENV and updating my custom webpack config to follow the new example in https://nx.dev/packages/webpack/documents/webpack-config-setup#configure-webpack-for-react-projects fixed it.

Side note for anyone that cares, I only have a custom webpack conf to add fallbacks for nodejs core modules since 15.4.6 also removed the polyfills that had been included. I get that that makes it consistent with what webpack 5 does, but it was a breaking change included in a patch version release.

@cgutierrezpa
Copy link

cgutierrezpa commented Feb 5, 2023

We are experiencing this issue too in 15.6.3

Workaround, add in workspace root .local.env with NODE_ENV=development

This doesn't work for me. Every time I try to add NODE_ENV, either via env file or directly in the script call like NODE_ENV=development nx serve app or cross-env NODE_ENV=development nx serve app (as suggested by @PointSingularity), it unsets the variable again and doesn't reach the React app. Any ideas why this could happen? I'm using 15.6.2, but also tried with 15.4.6 (first version introducing the problem with NODE_ENV resolving to undefined in React) and other 15.5.x versions and still no effect.

For context, I'm using the following config in project.json for the serve and build commands (none get the NODE_ENV var in the above versions, unlike in version <= 15.4.5 where it works):

    "build": {
      "executor": "@nrwl/webpack:webpack",
      "options": {
        "outputPath": "dist/apps/app",
        "index": "apps/app/src/index.html",
        "main": "apps/app/src/main.tsx",
        "polyfills": "apps/app/src/polyfills.ts",
        "tsConfig": "apps/app/tsconfig.app.json",
        "assets": ["apps/app/src/favicon.ico", "apps/app/src/assets"],
        "styles": [],
        "scripts": [],
        "webpackConfig": "apps/app/webpack.config.js",
        "skipNxCache": true
      },
      "configurations": {
        "production": {
          "fileReplacements": [
            {
              "replace": "apps/app/src/environments/environment.ts",
              "with": "apps/app/src/environments/environment.prod.ts"
            }
          ],
          "optimization": true,
          "outputHashing": "all",
          "sourceMap": false,
          "extractCss": true,
          "namedChunks": false,
          "extractLicenses": true,
          "vendorChunk": false,
          "budgets": [
            {
              "type": "initial",
              "maximumWarning": "2mb",
              "maximumError": "5mb"
            }
          ]
        }
      },
      "outputs": ["{options.outputPath}"]
    },
    "serve": {
      "executor": "@nrwl/webpack:dev-server",
      "options": {
        "buildTarget": "app:build"
      },
      "configurations": {
        "production": {
          "buildTarget": "app:build:production"
        },
        "development": {
          "buildTarget": "app:build:development"
        }
      },
      "defaultConfiguration": "development"
    },

@nemonemi
Copy link

nemonemi commented Feb 10, 2023

@bdebon , the suggestion from @bheftel and @jaysoo from my ticket resolved the HMR to start working again, and not to have a hard reload of the screen.
const { withReact } = require('@nrwl/react');

However, as your title says, it is still very slow. It takes around 15 sec for HMR to apply changes.

p.s. I am experiencing this on the v15.4.2.

Node : 16.14.0
OS : linux x64
yarn : 3.3.1

nx : 15.4.2
@nrwl/angular : Not Found
@nrwl/cypress : 15.4.2
@nrwl/detox : Not Found
@nrwl/devkit : 15.4.2
@nrwl/esbuild : Not Found
@nrwl/eslint-plugin-nx : 15.4.2
@nrwl/expo : Not Found
@nrwl/express : Not Found
@nrwl/jest : 15.4.2
@nrwl/js : 15.4.2
@nrwl/linter : 15.4.2
@nrwl/nest : Not Found
@nrwl/next : Not Found
@nrwl/node : Not Found
@nrwl/nx-cloud : Not Found
@nrwl/nx-plugin : Not Found
@nrwl/react : 15.4.2
@nrwl/react-native : Not Found
@nrwl/rollup : 15.4.2
@nrwl/schematics : 8.12.11
@nrwl/storybook : 15.4.2
@nrwl/web : 15.4.2
@nrwl/webpack : 15.4.2
@nrwl/workspace : 15.4.2
@nrwl/vite : 15.4.2
typescript : 4.8.4

@jaysoo
Copy link
Member

jaysoo commented Feb 10, 2023

I'll look into it. The problem could be the wrong env being set in the executor.

@jaysoo
Copy link
Member

jaysoo commented Feb 10, 2023

We'll patch it for 15.6 and it'll be in the next 15.7 release as well.

@bdebon
Copy link
Author

bdebon commented Feb 14, 2023

Thank you so much, guys! I finally made migration this morning, and it worked like a charm! HMR is back and as fast as it used to be!

@github-actions
Copy link

This issue has been closed for more than 30 days. If this issue is still occuring, please open a new issue with more recent context.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Mar 16, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
outdated scope: react Issues related to React support for Nx type: bug
Projects
None yet
Development

Successfully merging a pull request may close this issue.

9 participants