Skip to content

Conversation

@imranolas
Copy link
Contributor

The previous PR removed the window.parent check, and in hindsight it should've kept it to preserve the function interface. This change reimplements it whilst defending against CSP errors.

This comment was marked as outdated.

@imranolas imranolas requested review from alvarosabu and Copilot July 4, 2025 14:16
@imranolas imranolas merged commit 2afa7cc into main Jul 4, 2025
4 checks passed
@imranolas imranolas deleted the fix/csp-iframe branch July 4, 2025 14:16
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR reintroduces a guard in storyblokRegisterEvent to warn when not in Draft Mode or the Visual Editor (based on URL search), and updates tests accordingly.

  • Add a check in loadBridge to console.warn and exit early if _storyblok is missing from window.location.search.
  • Update tests to mock window.location.search instead of window.parent and add a new case for the warning.
  • Replace old iframe-based setup with URL‐search‐based logic in two existing tests.

Reviewed Changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.

File Description
packages/js/src/bridge.ts Insert guard in storyblokRegisterEvent to warn & return when the URL lacks ?_storyblok.
packages/js/src/index.test.ts Switch mocks from window.parent to window.location.search, plus add a test for the warning.
Comments suppressed due to low confidence (1)

packages/js/src/index.test.ts:417

  • Add a test for the scenario where the bridge is already loaded (loaded === true) to verify that callbacks registered after load are invoked immediately.
    it('should execute callbacks in order of registration', async () => {

// Mock being in an iframe
Object.defineProperty(window, 'parent', {
value: { location: { href: 'http://different-origin.com' } },
Object.defineProperty(window, 'location', {
Copy link

Copilot AI Jul 4, 2025

Choose a reason for hiding this comment

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

Consider restoring the original window.location (e.g. in an afterEach) after mocking it, to prevent state leaks between tests.

Copilot uses AI. Check for mistakes.

// Way to make sure all event handlers are called after loading
window.storyblokRegisterEvent = (cb: () => void) => {
if (!window.location.search.includes('_storyblok')) {
Copy link

Copilot AI Jul 4, 2025

Choose a reason for hiding this comment

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

[nitpick] Consider using URLSearchParams (e.g., new URL(window.location.href).searchParams.has('_storyblok')) for clearer intent and robust parsing.

Suggested change
if (!window.location.search.includes('_storyblok')) {
if (!new URL(window.location.href).searchParams.has('_storyblok')) {

Copilot uses AI. Check for mistakes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant