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

props for dynamic (like @loadable) #8818

Closed
kuznetsov-online opened this issue Sep 22, 2019 · 8 comments
Closed

props for dynamic (like @loadable) #8818

kuznetsov-online opened this issue Sep 22, 2019 · 8 comments

Comments

@kuznetsov-online
Copy link

kuznetsov-online commented Sep 22, 2019

Feature request

Can you add props for dynamic (like @loadable)?

import loadable from '@loadable/component'
const AsyncPage = loadable(props => import(`./${props.page}`))
function MyComponent() {
  return (
    <div>
      <AsyncPage page="Home" />
      <AsyncPage page="Contact" />
    </div>
  )
}
@timneutkens
Copy link
Member

dynamic imports are meant to be statically analyzable (no template strings). If you do import(./${props.page}) what will be bundled is the directory the file is in + the whole tree of files that is under that directory. Which in 99% of cases is not what you want. It would bundle every single javascript file into a separate chunk.

@kuznetsov-online
Copy link
Author

kuznetsov-online commented Sep 22, 2019

@timneutkens, but why it's working in @loadable!

I try to find a solution for next + globalize. It needs async loading cldr-data and messages before start rendering a page and getInitialProps is bad for this.

For example:

const Page = () => {
  const {formatMessage} = useGlobalize()
  return <div>{formatMessage('page')}</div>
}

const loadMessages = async (lang, page) => {
  globalize.loadMessages({[lang]: await import(`../messages/${lang}/${page}.json`)})
}

export default dynamic(async () => {
  await loadMessages('en', 'page')
  return Page
})

It works, but it needs props for transmitting language settings.

@timneutkens
Copy link
Member

@timneutkens, but why it's working in @Loadable!

It's not, it has the exact behavior outlined here: #8818 (comment)

@kuznetsov-online
Copy link
Author

@timneutkens do you see my example? it works very well (messages for globalize in chunks)

@timneutkens
Copy link
Member

Like I said you'll overbundle + slow down compilation because every single file will be bundled in that messages directory, even the files that you don't want to bundle.

You can do what you wanted to do by using static imports

@kuznetsov-online
Copy link
Author

@timneutkens ok, but if we change import to fetch?

For example:

const Page = () => {
  const {formatMessage} = useGlobalize()
  return <div>{formatMessage('page')}</div>
}

const loadMessages = async (lang, page) => {
  globalize.loadMessages({[lang]: await fetch('/messages/${lang}/${page}.json')})
}

export default dynamic(async () => {
  await loadMessages('en', 'page')
  return Page
})

but it needs props for transmitting language settings.

@DavideDaniel
Copy link

dynamic imports are meant to be statically analyzable (no template strings). If you do import(./${props.page}) what will be bundled is the directory the file is in + the whole tree of files that is under that directory. Which in 99% of cases is not what you want. It would bundle every single javascript file into a separate chunk.

Sure, but if you are using other components inside that tree that also use dynamic loading and are lazy loading other components the code splitting should naturally have separate chunks which wouldn't be needed for the initial entry.

@balazsorban44
Copy link
Member

This issue has been automatically locked due to no recent activity. If you are running into a similar issue, please create a new issue with the steps to reproduce. Thank you.

@vercel vercel locked as resolved and limited conversation to collaborators Jan 30, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants