Skip to content

Commit

Permalink
Merge pull request #5 from storyblok/fix/ssr-compatible
Browse files Browse the repository at this point in the history
fix: prevent crashing on SSR
  • Loading branch information
alexjoverm authored Feb 2, 2022
2 parents 61a0c83 + 4efef30 commit 0372d5c
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 11 deletions.
22 changes: 12 additions & 10 deletions lib/modules/bridge.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,20 @@ let loaded = false;
const callbacks = [];

export const loadBridge = (src) => {
// Way to make sure all event handlers are called after loading
window.storyblokRegisterEvent = (cb) => {
if (window.location === window.parent.location) {
console.warn("You are not in Draft Mode or in the Visual Editor.");
return;
}
return new Promise((resolve, reject) => {
if (typeof window === "undefined") return;

if (!loaded) callbacks.push(cb);
else cb();
};
// Way to make sure all event handlers are called after loading
window.storyblokRegisterEvent = (cb) => {
if (window.location === window.parent.location) {
console.warn("You are not in Draft Mode or in the Visual Editor.");
return;
}

if (!loaded) callbacks.push(cb);
else cb();
};

return new Promise((resolve, reject) => {
if (document.getElementById("storyblok-javascript-bridge")) return;

const script = document.createElement("script");
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@
"prettier"
],
"env": {
"browser": true
"browser": true,
"es6": true
},
"ignorePatterns": "dist/",
"parserOptions": {
Expand Down

0 comments on commit 0372d5c

Please sign in to comment.