Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid fails from serverside renderings #3909

Merged
merged 1 commit into from
Mar 21, 2017

Conversation

khorolets
Copy link
Contributor

Hi!

I'm using Chart.js in my project and I also use server-side rendering. And if my page contains Chart.js rendering is failing (expectedly). I tried to wrap usage of Chart.js with something like:

if (typeof window !== 'undefined') {
 // use Chart.js
}

But it still fails because Chart.js has been imported before and has tried to use window already.

I came up with the fix in PR, please take a look.

Here are some links to issues with the problem similar to mine:

P.S. Thanks for your great work!

@khorolets khorolets force-pushed the avoid_serverside_rendering_fails branch 4 times, most recently from 5c20ce9 to c29003e Compare February 15, 2017 12:34
@@ -669,6 +669,11 @@ module.exports = function(Chart) {
};
// Request animation polyfill - http://www.paulirish.com/2011/requestanimationframe-for-smart-animating/
helpers.requestAnimFrame = (function() {
if (typeof window === 'undefined') {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure how I feel about this change. I understand that it's rendering the chart synchronously if window is not defined, but that can be achieved by setting the animation duration to 0. @simonbrunel thoughts?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this use case (server side rendering) should rely on a new platform (e.g. platform.node) but not sure how it would be implemented. For now it may be easier to merge these (harmless) changes if it helps projects to integrate Chart.js.

@khorolets are these the only checks needed in order to have Chart.js running server side? What about the other specific HTMLCanvasElement, CanvasRenderingContext2D, CanvasGradient or even the iframe?

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We (@khorolets and I) don't use Chart.js to render charts on the server, that being said, we wrap the code calling Chart.js rendering into guards to avoid the execution on the server. HOWEVER, without this patch, we cannot have Chart.js in our bundle because Chart.js initializes some of its parts right away.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about the other specific HTMLCanvasElement, CanvasRenderingContext2D, CanvasGradient or even the iframe?

Since we don't encounter any other problems, I believe those parts are not triggered on script evaluation, and that is fine with our server side.

Copy link
Member

@simonbrunel simonbrunel Mar 19, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Makes sense, can you rebase and resolve conflicts?

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@simonbrunel I have already asked @khorolets to do that in a private chat, but I believe he is already offline for today, so we will address this the first thing tomorrow morning.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@etimberg are you good to merge those changes? We can still plan a better handling of window by moving all references into the dom platform.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am good with these changes.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@simonbrunel I've resolved the conflicts.

@khorolets khorolets force-pushed the avoid_serverside_rendering_fails branch from c29003e to 4961223 Compare March 20, 2017 07:08
@etimberg etimberg merged commit da1d1ca into chartjs:master Mar 21, 2017
@AlexKvazos
Copy link

When will this be pushed into a release? Desperately need this.

@trevordmiller
Copy link

Also need this

@etimberg
Copy link
Member

etimberg commented May 3, 2017

@AlexKvazos @trevordmiller we're working to get a 2.6 release out soon (it kinda got a bit bigger than anticipated and @simonbrunel and I have gotten busy). I think once we have docs deployed we should be good to go. I'm hoping in the next week or so

@simonbrunel simonbrunel added this to the Version 2.6 milestone May 7, 2017
@trevordmiller
Copy link

@etimberg Thank you, that will be very helpful to have this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants