Skip to content

Dynamic imports #728

Closed
Closed

Description

@rauchg

We should make it really easy to load modules dynamically.
For example, to only load some code client side:

componentDidMount () {
  import('./something').then(() => {
    // awesomeness!
  })
}

Activity

  1. arunoda commented on Jan 10, 2017

    @arunoda
    Contributor

    @rauchg is this for pages or for anything?

  2. rauchg commented on Jan 10, 2017

    @rauchg
    MemberAuthor

    @arunoda anything. Might require whitelisting in config though

  3. impronunciable commented on Jan 10, 2017

    @impronunciable
    Contributor

    This already works

  4. arunoda commented on Jan 10, 2017

    @arunoda
    Contributor

    @impronunciable oh really :)

  5. arunoda commented on Jan 10, 2017

    @arunoda
    Contributor

    I got an error. May be we need to bake something more into that.
    screen shot 2017-01-11 at 2 57 55 am

  6. impronunciable commented on Jan 10, 2017

    @impronunciable
    Contributor

    Wow, it works on the dev server but not when build + start 🤔

  7. arunoda commented on Jan 10, 2017

    @arunoda
    Contributor

    @impronunciable May be dev server serves necessary API endpoints. We may need to allow them in the production for exported chunks.

  8. impronunciable commented on Jan 10, 2017

    @impronunciable
    Contributor

    2017-01-10 18 31 39

    that could be

  9. arunoda commented on Jan 10, 2017

    @arunoda
    Contributor

    I think this could be easily doable. May be this is the same functionality as System.import in webpack.

  10. mxstbr commented on Jan 20, 2017

    @mxstbr
    Contributor

    Webpack supports this out of the box and it works just like System.import, see webpack/webpack#3413

  11. impronunciable commented on Jan 20, 2017

    @impronunciable
    Contributor

    @mxstbr We are using the webpack version but need to adapt our server to allow it

  12. self-assigned this
    on Mar 8, 2017
  13. arunoda commented on Mar 8, 2017

    @arunoda
    Contributor

    Notes:

    We need to support SSR with dynamic imports too. In that case, let's say user imported the component inside getInitialProps and pass it to the page via props. We need to support that case allow to do SSR.

    Since we don't execute getInitialProps in the client for the first time, we need to find a way to pass down the component from the Server to client.

  14. jesseditson commented on Mar 28, 2017

    @jesseditson
    Contributor

    Super excited for this to land - is https://github.com/zeit/next.js/compare/import-then getting PR'd any time soon so I can follow along on that thread?

  15. arunoda commented on Mar 28, 2017

    @arunoda
    Contributor

    @jesseditson yes. We are doing a PR soon.

  16. mtford90 commented on Apr 6, 2017

    @mtford90

    Excited for this.

    I've found that if server-side rendering with firebase you need to use firebase-admin and google-storage which are node libraries. Wouldn't want to load these in the client and so it would be nice to be able to load these dynamically if isServer is true when getInitialProps is called.

  17. neeraj022 commented on May 12, 2017

    @neeraj022

    I was trying to import a client side library (react-leaflet), but was getting errors when this library was getting loaded on the server side for first time page rendering. Making the library to load conditionally was a easy option.
    But while conditionally loading the library on the client side, webpack tries to make the bundle again and I get a error for the first load, thereafter it works well.

    Any workaround for this for the time being?

    Code :

    render()
    {
      console.log('Render called');
      let map=(<div></div>);
    
      //ui side check
      if(typeof window !== 'undefined' &&
      window.document && window.document.createElement){
        let reactleaflet=require('react-leaflet');
        let Map=reactleaflet.Map;
        let Marker=reactleaflet.Marker;
        let Popup=reactleaflet.Popup;
        let TileLayer=reactleaflet.TileLayer;
        //const { Map, TileLayer, Marker, Popup }=ReactLeaflet;
    
    
        map = (
        <Map center={this.state.centerPosition} zoom={this.state.zoomLevel}>
          <TileLayer
            url='http://{s}.tile.osm.org/{z}/{x}/{y}.png'
            attribution='&copy; <a href="http://osm.org/copyright">OpenStreetMap</a> contributors'
          />
          {this.state.markers.map((marker)=>{
            return (<Marker key={marker.key} position={[marker.position.lat, marker.position.lng]}>
              <Popup>
                <span>{marker.description}</span>
              </Popup>
            </Marker>);
          })}
    
        </Map>
      );
    }
    
    
    return(map)
    }
    
  18. frol commented on May 23, 2017

    @frol
    Contributor

    It seems that this issue is resolved in Next.js 3.0, isn't it?

  19. arunoda commented on May 23, 2017

    @arunoda
    Contributor
  20. locked as resolved and limited conversation to collaborators on May 11, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions