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
  • 24,311
  • 11
  • 36
  • 67
Rashid
  • 1,609
  • 19
  • 52
  • 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
  • 24,311
  • 11
  • 36
  • 67
  • 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
  • 24,311
  • 11
  • 36
  • 67
BATMAN_2008
  • 1,766
  • 14
  • 47
  • `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