NickPiscitelli / Glider.js

A fast, lightweight, dependency free, native scrolling carousel alternative!
https://nickpiscitelli.github.io/Glider.js
MIT License
3.27k stars 298 forks source link

Missing SSR Support #182

Open purecopy opened 3 years ago

purecopy commented 3 years ago

Hi, first things first: I love this lib and really appreciate your work so far! 🙂

I am currently having some problems to make things work with Vite SSG (SvelteKit) and wondered if you can help me out. In the build process I get

TypeError: Cannot set property 'Glider' of undefined

at https://github.com/NickPiscitelli/Glider.js/blob/f5d9ed220674ba01a6de3a4969cbe6059c1b5301/glider.js#L29-L31

I already tried to dynamically import the module without luck.

todo:

purecopy commented 3 years ago

For now I got things working using the Vite external options for SSR.

{
  ssr: {
    external: ['glider-js']
  }
}
rwaness commented 3 years ago

hi ! why load glider on server side ? you need to slide only on client. just load content on server and init glider on client load ;) gl

mylastore commented 3 years ago

external: ['glider-js']

can you tell me where did you add the configuration?

purecopy commented 3 years ago

external: ['glider-js']

can you tell me where did you add the configuration?

@mylastore I used the vite config prop from the svelte kit config obj in svelte.config.js

Edit: svelte-kit docs: https://kit.svelte.dev/docs#configuration-vite vite docs: https://vitejs.dev/guide/ssr.html#ssr-externals

mylastore commented 3 years ago

I used the vite config prop from the svelte kit config obj in svelte.config.js

Edit: svelte-kit docs: https://kit.svelte.dev/docs#configuration-vite vite docs: https://vitejs.dev/guide/ssr.html#ssr-externals

I added this to svelte.config but did not work for me, I am still getting and error any ways I am using this for now

vite: () => ({
    ssr: {
        external: ['glider-js']
    }
})
purecopy commented 3 years ago

@mylastore are you using the noExternal prop?

so you might need to filter external deps also

const external = ['glider-js'],

const config = {
  kit: {
    vite: () => ({
      ssr: {
        noExternal: Object.keys(pkg.dependencies).filter(
          (dep) => !external.includes(dep),
        ),
        external,
      },
    }),
  },
};
mylastore commented 3 years ago

@mylastore are you using the noExternal prop?

What does noExternal mean? I guess no NPM module? Some module created locally?

Never mine above. I added the configuration suggested and now I am getting

pkg is not defined

purecopy commented 3 years ago

pkg is not defined

@mylastore Sorry my bad, I missed it in the previous snippet.

You are most likely missing the import for the package.json like:

with require

const pkg = require('./package.json');

with es6

import path from 'path';
import fs from 'fs';

const pkg = JSON.parse(fs.readFileSync(path.resolve('./package.json')));