Next.js: Exclude specific component from server-side bundle, keep only on the client

Created on 2 Jun 2017  ยท  11Comments  ยท  Source: vercel/next.js

Hello there!

I'm facing the problem of rendering maps in a Next application. Specifically Mapbox (haven't tried Google maps yet, although I'm hearing the same issues persist).

The issue is pretty simple: rendering a map component client-side only. Something that is easily achieved in a regular React app by just importing the component and rendering it.

Using 'react-mapbox-gl', I am getting quite a few issues. Window (first and foremost), then imports from the component's dependencies that rely on client-side features.

Mounting the component in componentDidMount is not working (throws the same 'window' errors on initial mount -- and is really ugly...). Tracking something like 'isServer' in the getInitialState yields no results whatsoever (don't remember the exact error but I think it just renders nothing at all). Using the component isn't working because the dependencies of the imported Map component are getting bundled anyway, and they need the client (so it throws). Tried with other components as well (Uber's 'react-map-gl' for example), getting the same result.

I don't have an example to show you because it's just a new barebones next app with the 'react-mapbox-gl' component imported in index.js (or 'react-map-gl'). That's all. ๐Ÿ˜ž From reading around I'm getting the impression that rendering maps server-side appears to be an issue generally.

Looking for any solutions to this. I've looked all over the issues in the repo, and found the suggestions I tried above. Maybe (and probably) it's just me doing something wrong.

Thank you in advance! The library itself rocks! ๐Ÿ‘

Most helpful comment

For libraries like mapbox I've been using:

let mapbox = null;
if (process.browser) {
  mapbox = require('mapboxgl');
}

Then just write your component conditionalized on whether mapbox has a value or not.

All 11 comments

For libraries like mapbox I've been using:

let mapbox = null;
if (process.browser) {
  mapbox = require('mapboxgl');
}

Then just write your component conditionalized on whether mapbox has a value or not.

@MindRave - The example @sedubois links to uses a component to wrap your component in. This ensures that the server does not render it. This way you do not need to worry about conditionally if the component should process and run or not. It also has a mechanic for loading graphic/component display which is nice.

@timneutkens that is a cool solution. But for now only good for those that can upgrade to the v3 beta.

Out of curiosity, in the example above, using the dynamic import, would you just validate that the const is set? That does not seem as clean as wrapping the JSX in the <nossr> component. Do you see a reason to use this over the <nossr>.

Ended up using the dynamic import option from the v3 beta. 'Edge' project, so I don't mind. Works like a charm. Thank you! ๐Ÿ‘

As for the NoSSR, as noted in my original post, I tried it and it didn't work for me. It appears that the Mapbox component was importing stuff that required window all over the place and the compiler simply crashed on import every time. Checking the 'process.browser' with 'require(...)' as suggested by @finneganh worked, but I went with the 'dynamic' import in the end. ๐Ÿ˜‰

@MindRave awesome ๐Ÿ‘Œ

@finneganh where are you putting that piece of code? I tried that and I find that my component is always null.

@lightninglu10 That code is for the library, not the component.

@finneganh Oh.. Do you know if this method works for importing local components?

I haven't tried.

On Fri, Mar 9, 2018 at 12:55 PM, Patrick Lu notifications@github.com
wrote:

@finneganh https://github.com/finneganh Oh.. Do you know if this method
works for importing local components?

โ€”
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
https://github.com/zeit/next.js/issues/2144#issuecomment-371891972, or mute
the thread
https://github.com/notifications/unsubscribe-auth/AAP65QaDYWmIfW0qUpOzhUXxz66B4nhaks5tcsH3gaJpZM4NuN4h
.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

timneutkens picture timneutkens  ยท  3Comments

YarivGilad picture YarivGilad  ยท  3Comments

flybayer picture flybayer  ยท  3Comments

knipferrc picture knipferrc  ยท  3Comments

olifante picture olifante  ยท  3Comments