reactjs / React.NET

.NET library for JSX compilation and server-side rendering of React components
https://reactjs.net/
MIT License
2.29k stars 937 forks source link

SetTimeout is not supported in server-rendered Javascript. #906

Closed andrewmumblebee closed 5 years ago

andrewmumblebee commented 5 years ago

Please verify these steps before filing an issue, and check them off as you go

I'm using these library versions:

Runtime environment:

I've got everything working with server side rendering of React, however, i've started to add animations to the components using the GSAP library.

This library makes heavy use of setTimeout, and as soon as i even just include it in a component that will be rendered server side, React.Net throws an exception with the error "SetTimeout is not supported in server-rendered Javascript.". The error will also throw any time setTimeout is referenced in my own code.

I noticed this is only thrown if setTimeout is not defined when the server rendering is being performed.

src\React.Core\Resources\shims.js

setTimeout = setTimeout || global.setTimeout;
if (setTimeout === undefined) {
    setTimeout = function() {
        throw new Error('setTimeout is not supported in server-rendered Javascript.');
    }
}

I've tried to add the shim from https://gist.githubusercontent.com/ChadBurggraf/4c4abef515852aa1efee04478a8a8641/raw/8147e2c10567547b9c452382c3366352dc51cb1e/interval-timeout-shim.js which was referenced in #555 but i still get the same setTimeout error.

To elaborate i tried both loading the shim script in my startup.cs config chain.

ReactSiteConfiguration.Configuration
                .SetReuseJavaScriptEngines (true)
                .SetAllowJavaScriptPrecompilation (true)
                .AddScriptWithoutTransform ("~/dist/interval-timeout-shim.js")
                .AddScriptWithoutTransform ("~/dist/app.js");

And i've also tried adding it to my app.js bundle, setting the global object. But it still keeps crashing anytime setTimeout is used.

Any suggestions on how i can get around this issue? Cheers!

andrewmumblebee commented 5 years ago

I did some further work on getting this to work, i used the script-loader package for Webpack to ensure when i imported the interval-timeout-shim.js script it was being set in the global scope so that imported packages would use the shim defined functions.

Now React.net no longer crashes anytime i import a package that requires setTimeout, etc.