splide icon indicating copy to clipboard operation
splide copied to clipboard

Gatsby import

Open konstantinschuette opened this issue 5 years ago • 7 comments

Hello,

so I tried the import with Gatsby but, because its SSR, it can't access the functions needed from the client side. I followed along the steps on the react page: https://splidejs.com/integration-react-splide/

But I only get this error:

ReferenceError: self is not defined
    at Object../node_modules/@splidejs/splide/dist/js/splide.esm.js

Could anyone help me set up Splide with Gatsby?

konstantinschuette avatar Feb 24 '21 23:02 konstantinschuette

I have the same error, did you resolve this?

subgero avatar Mar 03 '21 15:03 subgero

Nope. I now use the embla carousel, another project. Maybe I dont understand the fundamentals of Gatsby but I cant get it to work.

konstantinschuette avatar Mar 03 '21 15:03 konstantinschuette

This solution works for me, in gatsby-node.js file:

exports.onCreateWebpackConfig = ({ stage, loaders, actions }) => {
  if (stage === "build-html") {
    actions.setWebpackConfig({
      module: {
        rules: [
          {
            test: /@splidejs/,
            use: loaders.null(),
          },
        ],
      },
    })
  }
}

Then, in your component add the next lines:

if (typeof window === "undefined") {
    return <p>Server Render</p>
  }

Src: link

subgero avatar Mar 03 '21 15:03 subgero

Thx, @subgero, that helps. To add the return statement within render-method of the component where the Splide-component is rendered crucial. I'd recommend to add this info to the main docs, here: https://splidejs.com/integration-react-splide/

ch12r avatar Mar 20 '21 07:03 ch12r

@ch12r it would be great but I dont own the project, Im not so smart! :/.

subgero avatar Mar 23 '21 01:03 subgero

Am I correct in thinking that this essentially forces Splider to render client side?

Either way, the self is not defined error relates to self not being available to NodeJs.

I have posted a fix here: #252, as the same issue interrupts Gridsome build. The end result is a fully working SSR rendered splider.

animald avatar Apr 23 '21 12:04 animald

Close the issue due to the major version update. Feel free to create a new issue or open a new discussion.


Solved the SSR reference error in v3. I made the Splide site by Gatsby. The react component is coming soon, but you can use Splide by creating your component.

NaotoshiFujita avatar Oct 09 '21 17:10 NaotoshiFujita