Coder Social home page Coder Social logo

Comments (10)

Hahlh avatar Hahlh commented on July 24, 2024

Unfortunately the change doesn't seem to have worked.

Do you use build-time SSR and can successfully deploy?
I will be able to test changes to the code as soon as I am able to develop react-p5 locally 👍

from react-p5.

Gherciu avatar Gherciu commented on July 24, 2024

@Hahlh I think a workaround on that is to just not render canvas or load p5 in SSR mode because it doesn't make sense....it doesn't affect SEO or anything else if we will not render it in SSR

from react-p5.

Gherciu avatar Gherciu commented on July 24, 2024

because the problem here is actually with p5 itself but not with (react-p5 it just renders a

in SSR mode).....but p5 needs access to window etc.....that's why it throws errors

from react-p5.

Hahlh avatar Hahlh commented on July 24, 2024

You would normally implement type checks in your code that prevent errors during build:

Something like:

if (typeof window !== 'undefined') {
    window.doSomething
}

In this case the error occurs directly in react-p5, meaning that I can't do anything about it without changing react-p5:
image

image

I am curious as surely other people must have it running with Gatsby?
Maybe @lambertbrady can weigh in as he proposed the first change to the window check in react-p5?

Did it work for you after the change?
Did you additionally have to wrap all the react-p5 related code in a window-checker or was wrapping those directly referring to window enough?

from react-p5.

Hahlh avatar Hahlh commented on July 24, 2024

Update:

If I comment all react-p5 related code out, it builds again successfully. So something in my code is triggering react-p5 and it in turn fails, because window is undefined.

That should make the question to you, @lambertbrady, solved. Please excuse the unnecessary ping.

from react-p5.

Hahlh avatar Hahlh commented on July 24, 2024

As a side note:

This is what the error logged to the terminal looks like in this case:
image

I am not sure whether that is something that can be improved in react-p5?

from react-p5.

Hahlh avatar Hahlh commented on July 24, 2024

Another update:

If I leave all the code unchanged and only comment out the Sketch component return, the code builds successfully.

So that should mean, that additional window checks are required in react-p5 to prevent calls to p5, that result in a window undefined?

But it seems that p5 should also have fail-safes against this:
image

from react-p5.

Gherciu avatar Gherciu commented on July 24, 2024

@Hahlh as a workaround use dinamyc imports ....just to import this library when window is available othervise no. Example.

if(window) {
const Sketch = require(react-p5)
}

from react-p5.

Hahlh avatar Hahlh commented on July 24, 2024

@Gherciu

I hope you are doing great!

let Sketch

if (typeof window !== "undefined") {
  Sketch = require("react-p5")
}

works, although it's means mixing import and require and although the import should normally be top level?

The problem with this is, that it also somehow changes my setup, meaning where the canvas is placed etc.

Expected result:
image

Actual result:
image

I will now explore two alternative approaches from the Gatsby docs.
Customizing the webpack config to use a dummy module during build and using loadable-components.

If those don't succeed, I may try to force the setup to happen later with a useEffect, although I am not sure whether that will introduce scoping problems.

Thank you again for the fast help, @Gherciu !

from react-p5.

Hahlh avatar Hahlh commented on July 24, 2024

In the example above, using dynamic imports as you suggested, and in using a custom webpack config, the canvas seems to get attached to the wrong parent.

In this case the absolute div on the right side.

Could I do something about that or specify precisely what the parent element should be?


Edit:

The current workaround I am using for attaching the canvas to the correct element, is to conditionally render not only the <SketchComp />, but all children of the main container.
This seems to work for the moment and I will now test it for deploying.

from react-p5.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.