Skip to content

Commit

Permalink
Merge pull request #26304 from storybookjs/norbert/composition-loadin…
Browse files Browse the repository at this point in the history
…g-state-fix

Core: Fix composition of storybooks on same origin
  • Loading branch information
ndelangen authored Mar 5, 2024
2 parents 6a0c827 + 8cbb06d commit 20c596e
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 8 deletions.
8 changes: 8 additions & 0 deletions code/lib/channels/src/postmessage/getEventSourceUrl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,14 @@ export const getEventSourceUrl = (event: MessageEvent) => {
// try to find the originating iframe by matching it's contentWindow
// This might not be cross-origin safe
const [frame, ...remainder] = frames.filter((element) => {
try {
return (
element.contentWindow?.location.origin === (event.source as Window).location.origin &&
element.contentWindow?.location.pathname === (event.source as Window).location.pathname
);
} catch (err) {
// continue
}
try {
return element.contentWindow === event.source;
} catch (err) {
Expand Down
10 changes: 7 additions & 3 deletions code/lib/manager-api/src/lib/events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,13 @@ export const getEventMetadata = (context: Meta, fullAPI: API) => {
const { source, refId, type } = context;
const [sourceType, sourceLocation] = getSourceType(source!, refId);

const ref =
refId && fullAPI.getRefs()[refId] ? fullAPI.getRefs()[refId] : fullAPI.findRef(sourceLocation!);

let ref: API_ComposedRef | undefined;
if (refId || sourceType === 'external') {
ref =
refId && fullAPI.getRefs()[refId]
? fullAPI.getRefs()[refId]
: fullAPI.findRef(sourceLocation!);
}
const meta = {
source,
sourceType,
Expand Down
8 changes: 4 additions & 4 deletions code/lib/manager-api/src/modules/refs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@ import type { ModuleFn } from '../lib/types';

const { location, fetch } = global;

const findFilename = /(\/((?:[^\/]+?)\.[^\/]+?)|\/)$/;

export interface SubState {
refs: API_Refs;
}
Expand Down Expand Up @@ -75,8 +73,10 @@ export const getSourceType = (source: string, refId?: string) => {
const { origin: localOrigin, pathname: localPathname } = location;
const { origin: sourceOrigin, pathname: sourcePathname } = new URL(source);

const localFull = `${localOrigin + localPathname}`.replace(findFilename, '');
const sourceFull = `${sourceOrigin + sourcePathname}`.replace(findFilename, '');
const localFull = `${localOrigin + localPathname}`.replace('/iframe.html', '').replace(/\/$/, '');
const sourceFull = `${sourceOrigin + sourcePathname}`
.replace('/iframe.html', '')
.replace(/\/$/, '');

if (localFull === sourceFull) {
return ['local', sourceFull];
Expand Down
2 changes: 1 addition & 1 deletion code/lib/manager-api/src/tests/refs.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ vi.mock('@storybook/global', () => {
// Add additional variations of global.location mock return values in this array.
// NOTE: The order must match the order that global.location is called in the unit tests.
const edgecaseLocations = [
{ origin: 'https://storybook.js.org', pathname: '/storybook/index.html' },
{ origin: 'https://storybook.js.org', pathname: '/storybook/iframe.html' },
];
// global.location value after all edgecaseLocations are returned
const lastLocation = { origin: 'https://storybook.js.org', pathname: '/storybook/' };
Expand Down

0 comments on commit 20c596e

Please sign in to comment.