next.js: Dynamic import() fails in next 5.xx

I have been using this piece of code to asynchronously load one of heavy component on my client side. It was working like a charm:

const DynamicChart = dynamic( import('../components/LineChart/LineChart'), { loading: () => <SectionPlaceholder><Loader /></SectionPlaceholder>, ssr: false, }, );

Since update to v5.0.0 I get webpack error, but only when I open the parent page from frontend route. When I directly load from server, it get’s loaded. When I get it once (from first load) and go back to this page on frontend, it works too.

This is the error I get: image

I tried import(...).then(...).catch(...) but none of chains were triggered.

The problem is that I can’t reproduce it always.

I’m also using next-routes if that matters

I assume that problem occurs because of undefined part in webpack. Any ideas how to debug the cause?

My package dependencies:

 "dependencies": {
    "babel-plugin-styled-components": "^1.5.0",
    "dotenv": "^5.0.0",
    "dotenv-webpack": "^1.5.4",
    "enzyme": "^3.3.0",
    "eslint-plugin-jest": "^21.7.0",
    "express": "^4.16.2",
    "glamor": "^2.20.40",
    "glamorous": "^4.11.4",
    "htmlescape": "^1.1.1",
    "isomorphic-fetch": "^2.2.1",
    "lodash": "^4.17.5",
    "next": "^5.0.0",
    "next-redux-wrapper": "^1.3.5",
    "next-routes": "^1.3.0",
    "polished": "^1.9.2",
    "react": "^16.2.0",
    "react-dom": "^16.2.0",
    "react-extras": "^0.5.0",
    "react-icons": "^2.2.7",
    "react-markdown": "^3.2.0",
    "react-modal": "^3.1.12",
    "react-no-ssr": "^1.1.0",
    "react-redux": "^5.0.6",
    "react-sizeme": "^2.3.6",
    "recharts": "^1.0.0-beta.10",
    "redux": "^3.7.2",
    "redux-logger": "^3.0.6",
    "redux-thunk": "^2.2.0",
    "reselect": "^3.0.1",
    "socket.io": "^2.0.4",
    "socket.io-client": "^2.0.4",
    "yargs": "^11.0.0"
  },
  "devDependencies": {
    "@storybook/addon-actions": "^3.3.12",
    "@storybook/addon-info": "^3.3.12",
    "@storybook/addon-links": "^3.3.12",
    "@storybook/addons": "^3.3.12",
    "@storybook/react": "^3.3.12",
    "@types/enzyme": "^3.1.8",
    "@types/jest": "^22.1.1",
    "@types/next": "^2.4.7",
    "@types/node": "^9.4.1",
    "@types/react": "^16.0.36",
    "@types/recharts": "^1.0.13",
    "@types/reselect": "^2.2.0",
    "@types/socket.io": "^1.4.31",
    "@types/storybook__react": "^3.0.6",
    "babel-core": "^6.26.0",
    "babel-eslint": "^7.2.3",
    "babel-plugin-recharts": "^1.1.1",
    "babel-plugin-transform-decorators-legacy": "^1.3.4",
    "babel-preset-env": "^1.6.1",
    "babel-preset-es2015": "^6.24.1",
    "babel-preset-stage-0": "^6.24.1",
    "babili-webpack-plugin": "^0.1.2",
    "console-probe": "^2.0.4",
    "create-component-app": "^1.6.2",
    "eslint": "^4.17.0",
    "eslint-config-airbnb": "^16.1.0",
    "eslint-plugin-babel": "^4.1.2",
    "eslint-plugin-import": "^2.8.0",
    "eslint-plugin-jsx-a11y": "^6.0.3",
    "eslint-plugin-react": "^7.6.1",
    "eslint-plugin-styled-components-config": "0.0.2",
    "jest": "^22.1.4",
    "prop-types": "^15.6.0",
    "redux-devtools-extension": "^2.13.2",
    "webpack-bundle-analyzer": "^2.10.0"
  }
  • I have searched the issues of this repository and believe that this is not a duplicate.

Expected Behavior

Component gets loaded every time

Current Behavior

Sometimes component is not loaded and webpack tries to load something with undefined in name

About this issue

  • Original URL
  • State: closed
  • Created 9 years ago
  • Reactions: 17
  • Comments: 45 (14 by maintainers)

Commits related to this issue

Most upvoted comments

The reason I’m saying it’ll be solved when webpack 4 lands, is that I’m reworking the whole next/dynamic implementation so that it does nothing “special” and will just use default webpack import() which it doesn’t currently. This will allow for setting the chunk name comment etc. And also solve this issue.

Regarding a timeline, we’ll release it whenever it’s ready. I’m currently working on this.

This issue will be fixed with webpack 4.

Playing around with 6.0.0-canary.3 and I can confirm it’s still the same.

This bug seems to have been underrated. I would like to emphasise that such bug makes the dynamic imports feature unreliable and therefore useless for serious projects. Thus, I would suggest reestimating it’s importance for this project.

If it is not possible to foresee a fix for it in the short term, which is totally fine and understandable, we should then hide it in the docs to avoid more frustration and more issues being reported from new users trying it out.

@timneutkens would you be the right person to take a look at that?

@mjanssen I just tried it out and it works.

Try using:

import dynamic from 'next/dynamic'

import './lib'

const Flickity = dynamic(import('flickity'), {ssr: false}))

The idea with the workaround is to make sure that import('flickity') is in all your pages so that when loading any SSR page, the manifest.js will contain references to all dynamic chunks it will need in the future.

You can add the dynamic component imports to pages/_document.js.

I am only experience this in dev-mode, and here its happening on each new route in the app:

  • Start with npm run dev, and visit localhost:3000 (ok)
  • Navigate to /route-b (fails with error above)
  • Hard refresh /route-b (ok)
  • Navigate to /route-c (fails with error above)
  • Hard refresh /route-c (ok)

Each route in my app has a dynamic require for the component used. Get the same error in the examples with-dynamic-import by initially visit localhost:3000/about and the click the Home-link. But as said, only in dev-mode.

Found the cause https://github.com/webpack/webpack/issues/4842#issuecomment-407437764.

Webpack only adds the require.ensure function that is used to load chunks if the entry point contains an import.

So when you visit a page directly which contains an import (somewhere in its dep graph), then you have the ensure function in your runtime.

But if you visit a page directly that doesn’t, then when you navigate to another page, it will compile a “non entry point” chunk and the static/commons/runtime.js will not contain this ensure function.

When you visit the dynamic page though it will fail the first time, but the new static/common/runtime.js will be generated with the runtime.js, so the next time you visit a page, you will have your ensure and dynamic will work.

Here is an example of both runtimes: https://gist.github.com/vjpr/a60945cd51068558a9b0cb2b744cef2f

Anyone has a temporary solution ?

@arunoda If you can give me a few pointers on where to start, I can take a stab at debugging the issue.

Hi ! I encounter the related issue when working with react-pdf which cannot be rendered outside the client. So my first thought was to use dynamic.

I think I solved the problem with getInitialProps function. Here is a snippet :

class PdfViewer extends React.PureComponent {
  constructor(props) {
    super(props);
  }

  render() {
    if (this.props.isServer)
      return (<Loader />)

    return (
      <div>
        <div className="document">
          <Document
            file={data}
            rotate={rotate}
            onLoadSuccess={this.onDocumentLoad}
            >
            <Page pageNumber={pageNumber} scale={scale}/>
          </Document>
        </div>
     )
  }
}

PdfViewer.getInitialProps = async function(context) {
  const isServer = typeof window === 'undefined'
  return { isServer }
}

I hope this will help ! Still looking for a proper solution with dynamic feature

I’m currently experiencing this on version 5.0.0, does anyone know if it’s fixed in newer versions or have more recent info?