vue-slider-component icon indicating copy to clipboard operation
vue-slider-component copied to clipboard

SSR compatibility

Open acupofspirt opened this issue 6 years ago • 23 comments

Hi, is it SSR compatible? Because I got some errors like "document is not defined" when using rendering in Node environment (no NUXT).

acupofspirt avatar Sep 28 '17 14:09 acupofspirt

Sorry, the component does not support the SSR.

NightCatSama avatar Sep 28 '17 14:09 NightCatSama

@NightCatSama, I solved that with importing plugin source file like

 import cVueSlider from 'vue-slider-component/src/vue2-slider.vue'

and in webpack config

{
   test: /\.vue?$/,
   exclude: /node_modules\/(?!vue-slider-component)/,
   loader: 'vue-loader'
}

This approach works because if we let vue-server-renderer/server-plugin work with raw(not minified and compiled) .vue files with

acupofspirt avatar Sep 28 '17 17:09 acupofspirt

Thank you for your sharing, this will help other people using this component.

NightCatSama avatar Sep 29 '17 03:09 NightCatSama

+1 for SSR support. We are also using this in a project and require SSR. Any plans to make it work?

jordanranson avatar Oct 05 '17 21:10 jordanranson

acupofspirt: when trying to follow your recipe for SSR, I get the following error:

error during render : .../node_modules/vue-slider-component/src/vue2-slider.vue:1 (function (exports, require, module, __filename, __dirname) {

where the error is unexpected token '<'. Does the

jdenekat avatar Nov 16 '17 18:11 jdenekat

I ran into the same unexpected token '<' error and got everything working using the following:

  1. Create a plugin
// @/plugins/vue-slider-component.js

import Vue from 'vue'
import VueSlider from 'vue-slider-component'

Vue.component('vue-slider', VueSlider)
  1. Register the plugin, with ssr set to "false"
// @/nuxt.config.js

module.exports = {
  ...
  build: {
    vendor: [
      'vue-slider-component'
    ],
    ...
    plugins: [
      { src: '~/plugins/vue-slider-component', ssr: false }
    ]
  }
}
  1. Use the component in a template
<template>
  <vue-slider>
  </vue-slider>
</template>

<script>
  export default {
  ...
  // don't register the vue-slider component
  }
</script>

Notes:

  • You do NOT need to nest it in a component (i.e. you don't need to install something like "vue-no-ssr")
  • You do NOT need any of that "if (process.browser)" stuff
  • You should NOT import or register the component in the script within your single file template since it's already being declared globally

Hope this helps!

dfeinberg5 avatar Dec 25 '17 04:12 dfeinberg5

+1

fixcik avatar Mar 05 '19 00:03 fixcik

On 3.0.16 SSR still didn't work and creating a plugin seemed like a lot more work in my current project. I was getting "document is not defined" like OP, but then I tried something that worked for me.

If I lazy load the vue-slider component then the my page works, but it would still complain about 'document is not defined' in cli only, but the page loads. So I then I conditionally loaded vue-slider component with a variable to render only if `typeof window !== 'undefined' && window.document' And it works! The slider doesn't load instantly, but this is a good fix for now.

<template>
  <no-ssr>
    <vue-slider v-if="renderComponent" v-model="range" />
  </no-ssr>
</template>

<script>
import NoSSR from 'vue-no-ssr'
const VueSlider = () => import('vue-slider-component')

export default {
  components: {
    VueSlider,
    'no-ssr': NoSSR
  },
  data () {
    return {
      renderComponent: false
    }
  },
  beforeDestroy () {
    this.renderComponent = false
  },
  mounted () {
    if (typeof window !== 'undefined' && window.document) {
      this.renderComponent = true
    }
  },
</script>

tonyisworking avatar Mar 27 '19 17:03 tonyisworking

@tonyisworking In version 3.0.18 I added a file that extracted css.

You can use it like this:

import VueSlider from 'vue-slider-component/dist-css/vue-slider-component.umd.min'
import 'vue-slider-component/dist-css/vue-slider-component.css'
import 'vue-slider-component/theme/default.css'

NightCatSama avatar Mar 27 '19 19:03 NightCatSama

Hi @NightCatSama. I'm trying to use version 3.0.25 with SSR in Nuxt (2.6.1)

So far I included this in nuxt.config.js

module.exports = {
    plugins: [
      '~/plugins/vue-slider-component'
    ]
}

And then in '~/plugins/vue-slider-component.js'

import Vue from 'vue'

import VueSlider from 'vue-slider-component/dist-css/vue-slider-component.umd.min.js'
import 'vue-slider-component/dist-css/vue-slider-component.css'
import 'vue-slider-component/theme/default.css'

Vue.component('vue-slider', VueSlider)

But then when I use it like <vue-slider></vue-slider> It gives me the document is not defined error. Previously I was using it wrapped in <no-ssr></no-ssr> and everything was running OK (but obviously with a delay when loading the page). Any hint about what I am doing wrong?

Thanks in advance!

Jesus82 avatar Apr 08 '19 11:04 Jesus82

I didn't use plugins, nor did I use the no-ssr tag, which works fine.

I am using [email protected].

@Jesus82

NightCatSama avatar Apr 08 '19 12:04 NightCatSama

@Jesus82 [email protected] tested no problem, I just used it in pages/index.vue.

image

image

NightCatSama avatar Apr 08 '19 12:04 NightCatSama

Hi, confirmed that it works OK both as a plugin as described in and including it locally in a page or in a component. (my local copy was stuck in the object not defined error, but after opening it in another tab, everything worked as expected).

Thanks!

Jesus82 avatar Apr 08 '19 12:04 Jesus82

Hi, I am using Vue-Storefront (wich uses SSR) and integrated the vue-slider-component the way @NightCatSama suggested

import VueSlider from 'vue-slider-component/dist-css/vue-slider-component.umd.min'
import 'vue-slider-component/dist-css/vue-slider-component.css'
import 'vue-slider-component/theme/default.css'

This works good, the slider is working.

But Chrome spams this notification in the console:

Unable to preventDefault inside passive event listener invocation.
in vue-slider-component.umd.min.js

In an other project this solution is suggested: touch-action: none on the drag element.

I tried to add this in the browser but seems not to change anything.

I am using the vue-slider-component in an other project (without SSR) without this issue. So i guess it might be a problem of the integration of the component in the way i described above?

I appreciate every hint.

NicolasHofmairT23 avatar Jul 05 '19 08:07 NicolasHofmairT23

@NicolasHofmairT23 Are you using it on the mobile side or on the PC side?

Maybe mousemove also needs to add passive: false?

https://github.com/NightCatSama/vue-slider-component/blob/master/lib/vue-slider.tsx#L347-L348

NightCatSama avatar Jul 05 '19 13:07 NightCatSama

@NicolasHofmairT23 This is a VueStorefront issue -> In your index.js for your theme remove the import for " import '@vue-storefront/core/lib/passive-listeners' " and the errors disappear.

tonyisworking avatar Jul 05 '19 15:07 tonyisworking

@NightCatSama Thanks for your advice. I just noticed the error log when i was in desktop mode. When I activated the mobile emulator in chrome the error did not appear.

@tonyisworking Thank you. This solved my problem. It was caused by vue-storefront as you said. See here, too https://github.com/DivanteLtd/vue-storefront/issues/2572

NicolasHofmairT23 avatar Jul 08 '19 15:07 NicolasHofmairT23

None of the above examples fully work for me with Gridsome stack, but I've mixed them and here is a combination that pretty much works in every env I've tested:

  1. Create plugin @dfeinberg5 said + import assets offered by @NightCatSama
import Vue from 'vue'
import VueSlider from 'vue-slider-component'

import 'vue-slider-component/dist-css/vue-slider-component.css'
import 'vue-slider-component/theme/default.css'

Vue.component('VueSlider', VueSlider)
  1. Import it in Gridsome Client API
export default function (Vue, { router, head, isClient }) {
  Vue.use(import('~/plugins/vue-slider-component.js'))
}
  1. Import VueSlider as a lazy component
export default {
    components: {
        VueSlider: () => import('vue-slider-component/dist-css/vue-slider-component.umd.min.js')
    }
}
  1. Use VueSlider component without ClientOnly wrapper

I'm not sure why it works only if I mix plugin with component, but I'm just glad that it finally works. Maybe it will be helpful for someone in the future.

dzikoysk avatar May 08 '20 13:05 dzikoysk

@NightCatSama I can get it to work with @nuxt-amp after adding the code

// import component
import VueSlider from 'vue-slider-component/dist-css/vue-slider-component.umd.min.js'
import 'vue-slider-component/dist-css/vue-slider-component.css'

// import theme
import 'vue-slider-component/theme/default.css'

i get the slider but it does not respond to drags. No errors!!! Screenshot from 2020-09-04 23-02-20

what could be the issue??

xaander1 avatar Sep 04 '20 20:09 xaander1

Importing this plugin result in this warning:

DevTools failed to load source map: Could not load content for http://localhost:3000/default.css.map: HTTP error: status code 404, net::ERR_HTTP_RESPONSE_CODE_FAILURE

import Vue from 'vue';

import 'vue-slider-component/dist-css/vue-slider-component.css';
import 'vue-slider-component/theme/default.css';

import VueSlider from 'vue-slider-component/dist-css/vue-slider-component.umd.min.js';

Vue.component('VueSlider', VueSlider);

Changing import 'vue-slider-component/theme/default.css' to import 'vue-slider-component/lib/theme/default.scss'; removed the warning as I'm already using SCSS.

mrleblanc101 avatar Aug 03 '21 14:08 mrleblanc101

@NightCatSama after some research, i found primary ssr falling problem:

when you importing styles, like this:

import './styles/slider.scss'
import './styles/dot.scss'
import './styles/mark.scss'

compiler add code from ./node_modules/vue-style-loader

code from node_modules/vue-style-loader contains document to load css by js, calling document on ssr leads to falling

simple fix can look as remove imports:

import './styles/slider.scss'
import './styles/dot.scss'
import './styles/mark.scss'

and moving all styles to other entrypoint.

Other simple fix - use ./dist-css/vue-slider-component.umd.min.js as main file in package.json because of dist-css moves all styles to separate file

Can you replace file path("main") in package.json?

rx-837 avatar Jan 19 '23 08:01 rx-837

Using version v3.2.24 I am still running into the same issues as others above trying to get this to work in Nuxt 3. Does anyone have a non hacky solution to using this in an SSR environment (Nuxt 3) while also using Vue 3 composition API such as <script setup> syntax?

Attempting to import from dist-css like mentioned here and in some comments above: https://nightcatsama.github.io/vue-slider-component/#/?hash=server-side-rendering-ssr, I am still having no success and am running into the document is not defined issue appearing to stem from vue-style-loader.

Updated: tried the v4 beta but that does not work out of the box either with SSR.

KyleBrown-804 avatar Jan 17 '24 00:01 KyleBrown-804

Using version v3.2.24 I am still running into the same issues as others above trying to get this to work in Nuxt 3. Does anyone have a non hacky solution to using this in an SSR environment (Nuxt 3) while also using Vue 3 composition API such as <script setup> syntax?

Attempting to import from dist-css like mentioned here and in some comments above: https://nightcatsama.github.io/vue-slider-component/#/?hash=server-side-rendering-ssr, I am still having no success and am running into the document is not defined issue appearing to stem from vue-style-loader.

Updated: tried the v4 beta but that does not work out of the box either with SSR.

I install 4.1.0-beta.7 on Nuxt 3.10.2 this is work!

<script setup lang="ts"> import VueSlider from 'vue-slider-component/dist-css/vue-slider-component.umd.min.js' import 'vue-slider-component/dist-css/vue-slider-component.css' <style lang="scss"> @import 'vue-slider-component/lib/theme/material.scss';

fenn1ks avatar Apr 23 '24 09:04 fenn1ks