vue-nl2br icon indicating copy to clipboard operation
vue-nl2br copied to clipboard

window undefined

Open usb248 opened this issue 6 years ago • 12 comments

I recently upgraded my version of vue-nl2br and i get an error : window undefined. Please fix this please. (i need to use this component for SSR)

Thanks

usb248 avatar Jul 25 '19 21:07 usb248

A workaround for SSR (nuxt) is to load vue-nl2br with

import Nl2br from 'vue-nl2br/src/main'

Works for me!

HugoHeneault avatar Jul 31 '19 09:07 HugoHeneault

thanks @HugoHeneault but it doesn't work :

import Nl2br from 'vue-nl2br/src/main'
Vue.component('nl2br', Nl2br)

i get an error :

SyntaxError
Unexpected identifier

usb248 avatar Jul 31 '19 10:07 usb248

Where does this error come from? Browser or server? Which nuxt version do you use?

HugoHeneault avatar Jul 31 '19 10:07 HugoHeneault

Server during the compilation, i use the last version 2.8.1..

usb248 avatar Jul 31 '19 10:07 usb248

Weird. It's working for me :/ Do you have babel set up ? Have a look there: https://fr.nuxtjs.org/api/configuration-build/

HugoHeneault avatar Jul 31 '19 10:07 HugoHeneault

strange...yeah see :

  build: {
    cssSourceMap: false,
    extractCSS: process.env.NODE_ENV == 'production',
    plugins: [
      new VuetifyLoaderPlugin()
    ],
    transpile: ['vuetify/lib'],
    babel: {
      presets({ isServer }) {
        return [
          [
            require.resolve('@nuxt/babel-preset-app'),
            {
              targets: isServer
                ? { node: 'current' }
                : { browsers: ['last 2 versions'], ie: 11 },
              corejs: { version: 3 }
            }
          ]
        ]
      }
    }
  }

usb248 avatar Jul 31 '19 11:07 usb248

You need to add vue-nl2br to the transpiled libs. 🧐

HugoHeneault avatar Jul 31 '19 11:07 HugoHeneault

Why i need to transpile vue-nl2br ? (it works now !)

usb248 avatar Jul 31 '19 11:07 usb248

Most of the time you're loading transpiled libs. But this one doesn't work with your env. So you're loading the sources, but nuxt isn't able to load es6 sources, so you need to transpile them to something it can run. :)

HugoHeneault avatar Jul 31 '19 11:07 HugoHeneault

hmmm, so, for example, i use lru-cache for client side cache request in nuxt. In 4.x version there is an ES5 version of the package, but in 5.X, there is no anymore. If i want to use 5.x version (https://github.com/isaacs/node-lru-cache/blob/master/index.js), i just need to add 'lru-cache' in transpile array in nuxt config file ?

usb248 avatar Jul 31 '19 12:07 usb248

Hmmm i tested in IE11, the script fails (vue-nl2br) in client side.... Capture Capture2

usb248 avatar Jul 31 '19 12:07 usb248

Two years later, but I still came across this issue. As 'window' is only defined on the client side in Nuxt, you may consider putting the nl2br component into a plugin that is only loaded on the client side:

  • create plugins/nl2br.js
import Vue from 'vue'
import Nl2br from 'vue-nl2br'

Vue.component('Nl2br', Nl2br)
  • in nuxt.config.js add a respective item into the plugin array
plugins: [
    { src: '@/plugins/nl2br', mode: 'client' },
],

Nl2Br can then be used as <nl2br ... /> in whichever component you want.

Lecraminos avatar Jan 27 '22 13:01 Lecraminos