Skip to content

Commit 217cbcd

Browse files
author
Amir Tocker
committed
Support server-side rendering.
1 parent e518974 commit 217cbcd

File tree

2 files changed

+30
-14
lines changed

2 files changed

+30
-14
lines changed

src/Util/requestAnimationFrame.js

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,23 @@
77

88
var lastTime = 0;
99
var vendors = ['ms', 'moz', 'webkit', 'o'];
10-
var requestAnimationFrame = window.requestAnimationFrame;
11-
var cancelAnimationFrame = window.cancelAnimationFrame;
12-
for (var x = 0; x < vendors.length && !requestAnimationFrame; ++x) {
13-
requestAnimationFrame = window[vendors[x] + 'RequestAnimationFrame'];
14-
cancelAnimationFrame = window[vendors[x] + 'CancelAnimationFrame']
15-
|| window[vendors[x] + 'CancelRequestAnimationFrame'];
16-
}
10+
var requestAnimationFrame;
11+
var cancelAnimationFrame;
1712

13+
if(typeof window != "undefined"){
14+
requestAnimationFrame = window.requestAnimationFrame;
15+
cancelAnimationFrame = window.cancelAnimationFrame;
16+
for (var x = 0; x < vendors.length && !requestAnimationFrame; ++x) {
17+
requestAnimationFrame = window[vendors[x] + 'RequestAnimationFrame'];
18+
cancelAnimationFrame = window[vendors[x] + 'CancelAnimationFrame']
19+
|| window[vendors[x] + 'CancelRequestAnimationFrame'];
20+
}
21+
}
1822
if (!requestAnimationFrame)
1923
requestAnimationFrame = function (callback, element) {
2024
var currTime = new Date().getTime();
2125
var timeToCall = Math.max(0, 16 - (currTime - lastTime));
22-
var id = window.setTimeout(function () { callback(currTime + timeToCall); },
26+
var id = setTimeout(function () { callback(currTime + timeToCall); },
2327
timeToCall);
2428
lastTime = currTime + timeToCall;
2529
return id;

src/components/Image/Image.js

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,14 @@ function compareObjects(o, p) {
3030
return true;
3131
}
3232

33+
function isElement(value) {
34+
return value != null && value.nodeType === 1 && isObjectLike(value);
35+
}
36+
37+
function isObjectLike(value) {
38+
return value != null && typeof value == 'object';
39+
}
40+
3341
export default class Image extends CloudinaryComponent {
3442
constructor(props, context) {
3543
function defaultBreakpoints(width, steps = 100) {
@@ -44,7 +52,11 @@ export default class Image extends CloudinaryComponent {
4452
}
4553

4654
get window() {
47-
return (this.element && this.element.ownerDocument) ? (this.element.ownerDocument.defaultView || window) : window;
55+
let windowRef = null;
56+
if(typeof window !== "undefined"){
57+
windowRef = window
58+
}
59+
return (this.element && this.element.ownerDocument) ? (this.element.ownerDocument.defaultView || windowRef) : windowRef;
4860
}
4961
shouldComponentUpdate( nextProps, nextState){
5062
return !( compareObjects(this.props, nextProps) && compareObjects(this.state, nextState));
@@ -94,7 +106,7 @@ export default class Image extends CloudinaryComponent {
94106
this.element = undefined;
95107
if (this.listener) {
96108
this.listener.cancel();
97-
this.window.removeEventListener('resize', this.listener);
109+
this.window && this.window.removeEventListener('resize', this.listener);
98110
}
99111
this.listener = undefined;
100112
}
@@ -103,10 +115,10 @@ export default class Image extends CloudinaryComponent {
103115
if (nextState.responsive) {
104116
const wait = firstDefined(nextProps.responsiveDebounce, nextContext.responsiveDebounce, 100);
105117
if (this.listener) {
106-
this.window.removeEventListener('resize', this.listener);
118+
this.window && this.window.removeEventListener('resize', this.listener);
107119
}
108120
this.listener = debounce(this.handleResize, wait);
109-
this.window.addEventListener('resize', this.listener);
121+
this.window && this.window.addEventListener('resize', this.listener);
110122
}
111123
}
112124

@@ -125,8 +137,8 @@ export default class Image extends CloudinaryComponent {
125137
var containerWidth, style;
126138
containerWidth = 0;
127139
let element = this.element;
128-
while (((element = element != null ? element.parentNode : void 0) instanceof Element) && !containerWidth) {
129-
style = this.window.getComputedStyle(element);
140+
while (isElement((element = element != null ? element.parentNode : void 0)) && !containerWidth) {
141+
style = this.window ? this.window.getComputedStyle(element) : '';
130142
if (!/^inline/.test(style.display)) {
131143
containerWidth = Util.width(element);
132144
}

0 commit comments

Comments
 (0)