pexea12 / vue-image-lightbox

A Vue component to display an image gallery lightbox
347 stars 80 forks source link

SSR options #31

Closed Markvandersteen closed 6 years ago

Markvandersteen commented 6 years ago

Hello thanks for the package, already using it in one of my projects. I do have a question however.

Is there a way to use this package with ssr/nuxt? If so it would be awesome if the readme contained an example, if not is something that is going to be added?

imcvampire commented 6 years ago

Hi @Markvandersteen,

I use this package with SSR and it works perfectly! What problem do you face now?

Just import it, use it as normal, you don't need any special code!

Markvandersteen commented 6 years ago

screen shot 2017-11-27 at 16 32 19 At the moment i'm getting some window not defined errors.

Markvandersteen commented 6 years ago

I got it working, i did need some extra code to make it work with nuxtjs.

miottid commented 6 years ago

Hi, @Markvandersteen what is the extra code you've added in order to get it work.

lexcode commented 6 years ago

Hey @Markvandersteen I am getting the same issue, could you paste your extra code solution in nuxt please?

lexcode commented 6 years ago

I found it,

plugins/lightbox.js

if (process.BROWSER_BUILD) {
  const VueTouch = require('vue-touch')
  Vue.use(VueTouch, { name: 'v-touch' })
}

nuxt.config.js

build: {
    vendor: ['vue-touch'],
}
Markvandersteen commented 6 years ago

@dmiotti @lexcode My approach was to use the System.import function. When the component is mounted. I import the things I need.



 async loadLightBox() {
        Promise.all([
          System.import('vue-touch'),
          System.import('vue-lazyload'),
          System.import('vue-image-lightbox')
        ]).then((modules) => {
          const VueTouch = modules.shift(),
            VueLazyLoad = modules.shift(),
            LightBox = modules.shift();

          Vue.use(VueTouch, {name: 'v-touch'});
          Vue.use(VueLazyLoad);
          Vue.component('lightbox', LightBox);
          this.lightboxReady = true;
        });
      },
    },
    async mounted() {
      this.loadLightBox();
    }
`
sven-ra commented 5 years ago

Edited for updated nuxt and vue-image-lightbox

An easy way to use it in Nuxt is like this:

Make a plugin file:

import Vue from 'vue'
import VueLazyLoad from 'vue-lazyload'
import LightBox from 'vue-image-lightbox'
require('vue-image-lightbox/dist/vue-image-lightbox.min.css')

Vue.use(VueLazyLoad)
Vue.component('light-box', LightBox)

Add the plugin to nuxt.config with SSR disabled:

plugins: [
  {
    src: '~/plugins/vue-lightbox-plugin.js',
    ssr: false
   }
 ]

Now you can use the 'light-box' component globally. It works only after you have mounted your component:

<template>
  <client-only>
    <light-box :images="lightBoxImages" />
  </client-only>
</template>