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

Core: Fix composition of storybooks on same origin #26304

Merged
merged 5 commits into from
Mar 5, 2024
Merged
Show file tree
Hide file tree
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
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
}
ndelangen marked this conversation as resolved.
Show resolved Hide resolved
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!);
}
ndelangen marked this conversation as resolved.
Show resolved Hide resolved
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(/\/$/, '');
ndelangen marked this conversation as resolved.
Show resolved Hide resolved

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' },
ndelangen marked this conversation as resolved.
Show resolved Hide resolved
];
// global.location value after all edgecaseLocations are returned
const lastLocation = { origin: 'https://storybook.js.org', pathname: '/storybook/' };
Expand Down
Loading