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

Static component import not working #191

Open
theonelucas opened this issue Aug 16, 2019 · 7 comments
Open

Static component import not working #191

theonelucas opened this issue Aug 16, 2019 · 7 comments

Comments

@theonelucas
Copy link

According to this line of code, the component can either be a string or a component:

typeof page === 'string' ? import(`./${page}`) : () => page()

But when we try to import the component:

import Page1 from '../components/Page1'

const components = {
  HOME: 'Home',
  PAGE1: Page1,
  [NOT_FOUND]: 'NotFound'
}

An error is being returned:

Error: load is not a function

How the component should be imported?

@tstirrat15
Copy link

Where is your import statement?

@theonelucas
Copy link
Author

Where is your import statement?

import Page1 from '../components/Page1'

@tstirrat15
Copy link

Right, that's a synchronous import that isn't covered by this component. The import() function and the import x from 'y' statement are two different things. To use this component, you need to be using the function:

const ListingPage = universal(import('./listing/components/Root'), { onError: exceptionHandler });

That sort of thing.

@theonelucas
Copy link
Author

So, basically I cannot pass a component already imported to Universal?

@tstirrat15
Copy link

I'm not completely certain of whether you can or not, but I'd question why you'd want to.

An import foo from 'bar' statement says "load this code when this statement is encountered." For a bundler, that means "add this code to the current bundle," and in an environment like node, that means "go get this code right now if it hasn't already been gotten." import statements are hoisted to the top of the file and are executed on the first walk of the code tree.

An import('bar') call says "create a placeholder for an asynchronous import." For a bundler, this means that it marks that spot as the start of a new bundle, and includes code to go fetch that new bundle. In an environment like node, it's an asynchronous require statement, more or less.

My guess is that you're wanting to use this library for code splitting. In that case, if you're using the import statement, you're losing out on the whole reason that you'd want to use this library. Does that make sense?

@theonelucas
Copy link
Author

That makes sense, but what I'm aiming here is to do an incremental code splitting. Some routes, which I would explicitly pass an previously imported component, would just use it, but the other routes with a string as the component would fetch it at runtime. That way I can really leverage the power of code splitting, bundling some pages that will always be used and fetching the others as needed.

@tstirrat15
Copy link

You'll need to refactor the calling code in that case. One way to do it with your example given above would be to replace the string references to components above with the RUC instances.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants