Jump to content


Scroll Trigger React

Recommended Posts


I am trying to get the Scroll Trigger to work on my gatsby website. I am a beginner at both Gatsby/React and GSAP so forgive my ignorance. I created a sandbox of how I did this in Gatsby. It works on development build like here on this sandbox but not when I try to go production build. I get this error "failed Building static HTML for pages". Does anyone know what I am doing wrong? Thanks!


Link to comment
Share on other sites

Hi and welcome to the GreenSock forums.


Unfortunately the sample you provided corresponds to a project based on create react app and not Gatsby, so I can't tell you exactly the source of your problem. It could be an issue with Gatsby's server side approach to generate the static files and a document reference somewhere in your code, it could also be an issue with some GraphQL query in a page or component.


Could you create a github repo with your project so we can take a look at it?


Also can you copy and paste the entire error in the command line when you run the npm run build command?


Happy Tweening!!!

  • Like 2
Link to comment
Share on other sites

Hi Rodrigo and thank you so much for your response!

Here is my repo: 


And this the entire error:

failed Building static HTML for pages - 0.822s

 ERROR #95313

Building static HTML failed for path "/"

See our docs page for more info on this error: https://gatsby.dev/debug-html

  TypeError: _toArray is not a function

  - render-page.js:3746 ScrollTrigger.init

  - render-page.js:3707 new ScrollTrigger

  - render-page.js:4317 Function../node_modules/gsap/ScrollTrigger.js.ScrollTrigger.create

  - render-page.js:4872 _scrollTrigger

  - render-page.js:6362 new Timeline

  - render-page.js:7978 Object.timeline

  - render-page.js:11046 new indexPage

  - react-dom-server.node.production.min.js:36 d

  - react-dom-server.node.production.min.js:39 $a

  - react-dom-server.node.production.min.js:44 a.b.render

  - react-dom-server.node.production.min.js:44 a.b.read

  - react-dom-server.node.production.min.js:54 renderToString

  - render-page.js:618 Module../.cache/static-entry.js.__webpack_exports__.default

  - render-html.js:28

  - debuggability.js:384 Promise._execute

  - promise.js:518 Promise._resolveFromExecutor

npm ERR! errno 1
npm ERR! gatsby-starter-hello-world@0.1.0 deploy: `gatsby build && gh-pages -d public -b master`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the gatsby-starter-hello-world@0.1.0 deploy script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

npm ERR! A complete log of this run can be found in:
npm ERR!     *\npm-cache\_logs\*-debug.log


Link to comment
Share on other sites

Not sure if this will help but I think the issue may be due to tree shaking. Another GSAP user had an issue with the Draggable plugin and fixed it by using a useEffect hook (link below). It might be worth posting a comment on a Gatsby forum also as I believe you should be able to register the plugin in the gatsby-node.js file.


Link to similar issue


Hope this helps or at least points you in the right direction. 



  • Like 2
Link to comment
Share on other sites

Mhh... perhaps Andy is right, since you are using class components, you could move this particular part to the componentDidMount() method:

this.tl = gsap.timeline({
  paused: true,
  scrollTrigger: {
    trigger: this.logoTrigger, // start the animation when ".box" enters the viewport (once)
    scrub: true,
    markers: true,

And in your constructor leave just the definition of tl:

this.tl = null;

In case you need to reference it somewhere else in your component.

  • Like 2
Link to comment
Share on other sites

Gatsby's documention.




Some of your code references “browser globals” like window or document. If this is your problem you should see an error above like “window is not defined”. To fix this, find the offending code and either a) check before calling the code if window is defined so the code doesn’t run while Gatsby is building (see code sample below) or b) if the code is in the render function of a React.js component, move that code into a componentDidMount lifecycle or into a useEffect hook, which ensures the code doesn’t run unless it’s in the browser.


So you either have to check.

if (typeof window !== "undefined") {


Or you have to put registerPlugin stuff in useEffect or componentDidMount.


Also, you might need to do this temp work around.




  • Like 4
Link to comment
Share on other sites

Woop woop! I love this community and I love ScrollTrigger 😄 It worked when I included both registerPlugin and the temporary workaround inside the if statement.

import { gsap } from "gsap"
import { ScrollTrigger } from "gsap/ScrollTrigger"

if (typeof window !== `undefined`) {
  gsap.core.globals("ScrollTrigger", ScrollTrigger)


  • Like 4
Link to comment
Share on other sites

Here is a simple component for context and reference:


import React from "react";
import styles from "./index.module.scss";

import gsap from "gsap";
import { ScrollTrigger } from "gsap/ScrollTrigger";

if (typeof window !== `undefined`) {
  gsap.core.globals("ScrollTrigger", ScrollTrigger)

class Index extends React.Component {
  constructor(props) {
    this.container = null;
    this.trigger = null;
    this.tl = gsap.timeline({
      paused: true,
      scrollTrigger: {
        trigger: this.trigger,
        scrub: true,
        markers: true
  componentDidMount() {
    this.tl.to(this.container, {
      x: 100,
      duration: 2
  render() {
    return (
        <div className={styles.container}>
          <div className={styles.header} ref={div => (this.container = div)}>
        <div className={styles.trigger} ref={div => (this.trigger = div)}>

export default Index;


.container {
  position: fixed;

.header {
  position: absolute;
  top: 40%;
  left: 50%;

.trigger {
  position: absolute;
  bottom: -50em;


  • Like 3
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.