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

Dynamic imports #728

Closed
rauchg opened this issue Jan 10, 2017 · 18 comments
Closed

Dynamic imports #728

rauchg opened this issue Jan 10, 2017 · 18 comments
Assignees

Comments

@rauchg
Copy link
Member

rauchg commented Jan 10, 2017

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

componentDidMount () {
  import('./something').then(() => {
    // awesomeness!
  })
}
This was referenced Jan 10, 2017
@arunoda
Copy link
Contributor

arunoda commented Jan 10, 2017

@rauchg is this for pages or for anything?

@rauchg
Copy link
Member Author

rauchg commented Jan 10, 2017

@arunoda anything. Might require whitelisting in config though

@impronunciable
Copy link
Contributor

This already works

@arunoda
Copy link
Contributor

arunoda commented Jan 10, 2017

@impronunciable oh really :)

@arunoda
Copy link
Contributor

arunoda commented Jan 10, 2017

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

@impronunciable
Copy link
Contributor

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

@arunoda
Copy link
Contributor

arunoda commented Jan 10, 2017

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

@impronunciable
Copy link
Contributor

2017-01-10 18 31 39

that could be

@arunoda
Copy link
Contributor

arunoda commented Jan 10, 2017

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

@mxstbr
Copy link
Contributor

mxstbr commented Jan 20, 2017

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

@impronunciable
Copy link
Contributor

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

@arunoda
Copy link
Contributor

arunoda commented Mar 8, 2017

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.

@jesseditson
Copy link
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?

@arunoda
Copy link
Contributor

arunoda commented Mar 28, 2017

@jesseditson yes. We are doing a PR soon.

@mtford90
Copy link

mtford90 commented Apr 6, 2017

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.

@neeraj022
Copy link

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)
}

@frol
Copy link
Contributor

frol commented May 23, 2017

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

@arunoda
Copy link
Contributor

arunoda commented May 23, 2017

@frol Yep. Thanks.
See more: https://zeit.co/blog/next3-preview

@arunoda arunoda closed this as completed May 23, 2017
@lock lock bot locked as resolved and limited conversation to collaborators May 11, 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

No branches or pull requests

9 participants