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

fix(ssr): prevent execute it in a not-browser environment #173

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 14 additions & 2 deletions src/layout.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,14 @@

'use strict';

const canUseDOM = !!(
typeof window !== 'undefined'
&& window.document
&& window.document.createElement
);

// setImmediate
if (!window.setImmediate) {
if (canUseDOM && !window.setImmediate) {
window.setImmediate = (function setupSetImmediate() {
return (
window.msSetImmediate ||
Expand Down Expand Up @@ -80,7 +86,7 @@ if (!window.setImmediate) {
})();
}

if (!window.clearImmediate) {
if (canUseDOM && !window.clearImmediate) {
window.clearImmediate = (function setupClearImmediate() {
return (
window.msClearImmediate ||
Expand All @@ -98,6 +104,9 @@ if (!window.clearImmediate) {

// Check if WordCloud can run on this browser
var isSupported = (function isSupported() {
if (!canUseDOM) {
return true;
}
var canvas = document.createElement('canvas');
if (!canvas || !canvas.getContext) {
return false;
Expand Down Expand Up @@ -127,6 +136,9 @@ var isSupported = (function isSupported() {
// Find out if the browser impose minium font size by
// drawing small texts on a canvas and measure it's width.
var minFontSize = (function getMinFontSize() {
if (!canUseDOM) {
return;
}
if (!isSupported) {
return;
}
Expand Down