1

I am planning to use jsplumb in my app

my current code looks like this

<template>
  <client-only>
    <div id="canvas"></div>
  </client-only>
</template>

<script>
import { jsPlumb } from 'jsplumb'

export default {
  name: 'JsPlumb',
  auth: false,
  mounted() {
    jsPlumb.ready(function () {})
  }
}
</script>

when I run my app, this throws an error of document is not defined. I already found out that jsplumb wont work on SSR. How can I achieve it to use import for jsplumb that it wont throw an error. Is there a dynamic import in nuxt that only imports the library in non-ssr?

kissu
  • 40,416
  • 14
  • 65
  • 133
Rashid
  • 1,700
  • 1
  • 23
  • 56
  • Hi for some reason even after following the steps you have mentioned it does not seem to work and I am getting `document is not defined` error. I have posted my question here can you please help me out with this? https://stackoverflow.com/q/69814456/7584240 – BATMAN_2008 Nov 02 '21 at 20:20

2 Answers2

0

Exact same question as here: https://stackoverflow.com/a/67751550/8816585

<script>
import { jsPlumb } from 'jsplumb' // client-side library only, no SSR support

export default {
  mounted() {
    if (process.client) {
      // your JS code here like >> jsPlumb.ready(function () {})
    }
  },
}
</script>

I've edited the answer there to have all the needed details.

kissu
  • 40,416
  • 14
  • 65
  • 133
  • Hi for some reason even after following the steps you have mentioned it does not seem to work and I am getting `document is not defined` error. I have posted my question here can you please help me out with this? https://stackoverflow.com/q/69814456/7584240 – BATMAN_2008 Nov 02 '21 at 20:20
0

If anyone else if trying even after process.client not working then try process.browser because I guess the client has been changed. Best way is to do console.log(process) and check the variables present in that and based on that make the decision.

<script>
  console.log(process)
  console.log(process.browser)
</script>
kissu
  • 40,416
  • 14
  • 65
  • 133
BATMAN_2008
  • 2,788
  • 3
  • 31
  • 98
  • `process.browser` is some deprecated old thing tho, not sure to what it fallbacks. https://nuxtjs.org/docs/concepts/server-side-rendering#window-or-document-undefined – kissu Nov 03 '21 at 09:07