Git Product home page Git Product logo

Comments (24)

gregberge avatar gregberge commented on August 10, 2024 1

I think I could make it possible by adding a require.resolveWeak('./Bar') like React Loadable does. But right now I do not want to couple it with Webpack.

I will give you a solution, you can do it if you want:

The loadableState you get has a tree properties, the solution for you is to walk the tree and try to get the full path of the module.

If you got the full path of the module, you can easily load it client side.

The actual problem is that you only get the relative path of the module, so you can't tell to Webpack "Load my module". Do you see the problem?

from loadable-components.

gregberge avatar gregberge commented on August 10, 2024

You can compare client modules and server modules and see what's going on.

from loadable-components.

wellyshen avatar wellyshen commented on August 10, 2024

@neoziro

If I export my routes config as a function, loadable-components not work:

import loadable from 'loadable-components';

import type { Dispatch } from './types';
import { fetchUsersIfNeeded } from './actions/users';
import { fetchUserIfNeeded } from './actions/user';
import { Loading, Error } from './components/index';

export default () => [
  {
    path: '/',
    exact: true,
    component: loadable(() => import('./containers/Home'), {
      LoadingComponent: Loading,
      ErrorComponent: Error
    }), // Add your route here
    loadData: (dispatch: Dispatch) =>
      Promise.all([
        dispatch(fetchUsersIfNeeded()) // Register your server-side call action(s) here
      ])
  },
  {
    path: '/UserInfo/:id',
    component: loadable(() => import('./containers/UserInfo'), {
      LoadingComponent: Loading,
      ErrorComponent: Error
    }),
    loadData: (dispatch: Dispatch, params: Object) =>
      Promise.all([dispatch(fetchUserIfNeeded(params.id))])
  },
  {
    component: loadable(() => import('./containers/NotFound'), {
      LoadingComponent: Loading,
      ErrorComponent: Error
    })
  }
];

If I export my routes config as an array, loadable-components works:

import loadable from 'loadable-components';

import type { Dispatch } from './types';
import { fetchUsersIfNeeded } from './actions/users';
import { fetchUserIfNeeded } from './actions/user';
import { Loading, Error } from './components/index';

export default [
  {
    path: '/',
    exact: true,
    component: loadable(() => import('./containers/Home'), {
      LoadingComponent: Loading,
      ErrorComponent: Error
    }), // Add your route here
    loadData: (dispatch: Dispatch) =>
      Promise.all([
        dispatch(fetchUsersIfNeeded()) // Register your server-side call action(s) here
      ])
  },
  {
    path: '/UserInfo/:id',
    component: loadable(() => import('./containers/UserInfo'), {
      LoadingComponent: Loading,
      ErrorComponent: Error
    }),
    loadData: (dispatch: Dispatch, params: Object) =>
      Promise.all([dispatch(fetchUserIfNeeded(params.id))])
  },
  {
    component: loadable(() => import('./containers/NotFound'), {
      LoadingComponent: Loading,
      ErrorComponent: Error
    })
  }
];

Very wired... If I remove loadable-components and use non loadable-components both exports way work.

from loadable-components.

gregberge avatar gregberge commented on August 10, 2024

Hmm it could be an issue in the tree traverser can you give a reproducible example on CodeSandbox?

from loadable-components.

wellyshen avatar wellyshen commented on August 10, 2024

@neoziro

Sure, you can see my error reproduce here.

And here's the error that I encounter:
2018-02-11 4 38 38

from loadable-components.

max-mykhailenko avatar max-mykhailenko commented on August 10, 2024

Can I hope that this problem will be fixed in a nearest few days?

from loadable-components.

wellyshen avatar wellyshen commented on August 10, 2024

@max-mykhailenko Did you also encounter this problem?

from loadable-components.

max-mykhailenko avatar max-mykhailenko commented on August 10, 2024

Not yet, I always look at issues before start using any lib and my case is more complex: I want to write something like this

const component = import('./Books');
const What = loadable(async () => {
  const [{ default: Books }, { default: books }] = await Promise.all([
    component,
    ...getCalledActionsAndGetPromisesArrayBack(store),
   //  or  ...getActionsFilesPromisesArrayBack(store),
  ])

  return props => <Books {...props} books={books} />
})

from loadable-components.

gregberge avatar gregberge commented on August 10, 2024

Right now, I don't have time to investigate it but I think this is a problem in the tree traverser. If someone could give a look I will be glad!

from loadable-components.

max-mykhailenko avatar max-mykhailenko commented on August 10, 2024

@wellyshen I recommend to change structure and export routes without function. After big workaround with many loadable components and react-hot-loader I can say that it's only one possible way to do that.

from loadable-components.

wellyshen avatar wellyshen commented on August 10, 2024

@neoziro Any update for this issue?

from loadable-components.

gregberge avatar gregberge commented on August 10, 2024

Not on my side.

from loadable-components.

wellyshen avatar wellyshen commented on August 10, 2024

@neoziro Did you mean the issue isn't caused by this library?

from loadable-components.

gregberge avatar gregberge commented on August 10, 2024

No I mean I haven’t time to investigate it yet. I think that the tree traversal is not working for your specific case. Do you have a complete example so I could try to see exactly the source of the problem?

from loadable-components.

wellyshen avatar wellyshen commented on August 10, 2024

@neoziro Sure, you can see my error reproduce here and run the branch to see what's going on.

from loadable-components.

gregberge avatar gregberge commented on August 10, 2024

Your static route configuration looks a bit tricky. I will try to run your project probably this week end, I keep you up to date!

from loadable-components.

wellyshen avatar wellyshen commented on August 10, 2024

Yea I use the route config of react-router for SSR

from loadable-components.

JulienPradet avatar JulienPradet commented on August 10, 2024

I thought I had the same issue so I looked at it a bit.

In order to build the client modules object (the ones in the warning), the library needs to run all the loadable calls.

Therefore when you call loadComponents you must have created all your loadable Components beforehand. In your specific case @wellyshen you defer them later when you render your AppContainer. That's why it fails to register the loadable components since no render has been done yet. And that's also why the version without a function works fine.

So in my opinion, that's not a bug, it's by design :)

(For my issue, it was because I had several instances of loadable-components bundled in my code.)

from loadable-components.

gregberge avatar gregberge commented on August 10, 2024

Seems fixed https://github.com/wellyshen/react-cool-starter/blob/error-reproduce-loadable-components/src/routes.js#L16

from loadable-components.

gregberge avatar gregberge commented on August 10, 2024

@JulienPradet you are right, the issue was that loadable components were created lazily.

from loadable-components.

wellyshen avatar wellyshen commented on August 10, 2024

@neoziro My goal is to wrap loadable-components in a function like this for implementing async reducers. But loadable-components will throw error when it's wrapped in a function.

from loadable-components.

gregberge avatar gregberge commented on August 10, 2024

OK, this is not possible for now. It will not work with SSR mode.

from loadable-components.

wellyshen avatar wellyshen commented on August 10, 2024

Alright, I got it. Thanks πŸ‘

from loadable-components.

wellyshen avatar wellyshen commented on August 10, 2024

Okay, I will survey it when I have time and ask you questions about it man πŸ‘

from loadable-components.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.