Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Nuxt avoid import of client-side script for server-side rendering

Tags:

vue.js

nuxt.js

In my nuxt.js application, I have a script that imports an NPM package which is only compatible with browser contexts (it references document, location, window, etc.)

Is there a way to exclude this from SSR?

import thing from "@vendor/thing"; // causes `document not defined` error
export default showThing(){
 if (process.client) {
    thing();
 }
}

I can use the method with process.client but this file is still imported in my components.

like image 861
d-_-b Avatar asked Apr 22 '26 00:04

d-_-b


1 Answers

You could import it dynamically rather than in every context.

As explained in my answer here: https://stackoverflow.com/a/67825061/8816585

In your example, that would be something like this

export default showThing(){
  if (process.client) {
    const thing = await import('@vendor/thing')
    thing()
  }
}
like image 151
kissu Avatar answered Apr 23 '26 13:04

kissu