Skip to content

Commit

Permalink
Merge pull request #247 from storyblok/feat/load-bridge-when-visual-e…
Browse files Browse the repository at this point in the history
…ditor

feat: load the bridge only on Visual Editor
  • Loading branch information
Dawntraoz authored Feb 28, 2023
2 parents b90ebaa + ac6590f commit 8c887c3
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 3 deletions.
2 changes: 1 addition & 1 deletion lib/cypress/e2e/index.cy.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ describe("@storyblok/js", () => {

describe("Bridge", () => {
it("Is loaded by default", () => {
cy.visit("http://localhost:5173/");
cy.visit("http://localhost:5173/?_storyblok_tk[timestamp]=1677494658");
cy.get(".with-bridge").click();
cy.get("#storyblok-javascript-bridge").should("exist");
});
Expand Down
9 changes: 7 additions & 2 deletions lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,13 @@ export const storyblokInit = (pluginOptions: SbSDKOptions = {}) => {
result = { ...result, ...pluginFactory(options) };
});

// Load bridge
if (bridge !== false) {
/*
** Load bridge if you are on the Visual Editor
** For more security: https://www.storyblok.com/faq/how-to-verify-the-preview-query-parameters-of-the-visual-editor
*/
const isServer = typeof window === "undefined";
const inEditor = !isServer && window.location?.search?.includes('_storyblok_tk');
if (bridge !== false && inEditor) {
loadBridge(bridgeLatest);
}

Expand Down
3 changes: 3 additions & 0 deletions playground/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
</head>
<body>
<div>Hello</div>
<button class="fake-visual-editor" onclick="window.location.href = '/?_storyblok_tk[timestamp]=1675712641'">
Fake being inside Visual Editor
</button>
<button class="with-bridge" onclick="initWithBridge()">
storyblokInit With Bridge
</button>
Expand Down

0 comments on commit 8c887c3

Please sign in to comment.