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

[WIP] Add dynamic import support #1750

Closed
wants to merge 78 commits into from
Closed

Conversation

arunoda
Copy link
Contributor

@arunoda arunoda commented Apr 18, 2017

With this we are adding TC39 dynamic import support to Next.js

TODO

  • Add Unit tests for SameLoopPromise
  • Add support for async-renderer
  • Add some integration tests
  • Add docs

Basically you could any module asynchronously and do anything you want.
You can use utilities like https://github.com/xtuc/async-reactor without any issues

We've also included some utilities to make your life easier.

You can also try this example app.

1. Create Dynamic Component (Also does SSR)

import dynamic from 'next/dynamic'
const DynamicComponent = dynamic(import('../components/hello'))

export default () => (
  <div>
    <Header />
    <DynamicComponent />
    <p>HOME PAGE is here!</p>
  </div>
)

2. With Custom Loading Component

import dynamic from 'next/dynamic'
const DynamicComponentWithCustomLoading = dynamic(
  import('../components/hello2'),
  {
    loading: () => (<p>...</p>)
  }
)

export default () => (
  <div>
    <Header />
    <DynamicComponentWithCustomLoading />
    <p>HOME PAGE is here!</p>
  </div>
)

3. No SSR Support

import dynamic from 'next/dynamic'
const DynamicComponentWithNoSSR = dynamic(
  import('../components/hello3'),
  { ssr: false }
)

export default () => (
  <div>
    <Header />
    <DynamicComponentWithNoSSR />
    <p>HOME PAGE is here!</p>
  </div>
)

4. With async-reactor

SSR support is not available here

import { asyncReactor } from 'async-reactor'
const DynamicComponentWithAsyncReactor = asyncReactor(async () => {
  const Hello4 = await import('../components/hello4')
  return (<Hello4 />)
})

export default () => (
  <div>
    <Header />
    <DynamicComponentWithAsyncReactor />
    <p>HOME PAGE is here!</p>
  </div>
)

lib/dynamic.js Outdated
super(...args)
this.state = { AsyncComponent: null }
this.isServer = typeof window === 'undefined'
this.loadComponent()
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Implement ssr=false support.

arunoda added 26 commits May 9, 2017 16:24
It was xxxx/page//index.js
Basicaly, now client side routing as they work like
in Next.js including prefetching.
Also added a silent option if someone don't want
those messages.
@arunoda
Copy link
Contributor Author

arunoda commented May 15, 2017

We just took this and it's available with Next.js 3.0.
Check this: https://zeit.co/blog/next3-preview

@arunoda arunoda closed this May 15, 2017
@arunoda arunoda deleted the dynamic-import branch May 15, 2017 19:11
@lock lock bot locked as resolved and limited conversation to collaborators May 16, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants