Skip to content

Commit

Permalink
Merge pull request #21682 from storybookjs/tom/21500-fix-cors-issues
Browse files Browse the repository at this point in the history
Composition: Fix various CORS issues.
  • Loading branch information
ndelangen authored Mar 20, 2023
2 parents 8814ac3 + 43d45fd commit 5658390
Show file tree
Hide file tree
Showing 3 changed files with 304 additions and 77 deletions.
3 changes: 2 additions & 1 deletion code/lib/manager-api/src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,8 @@ type ModuleWithoutInit<APIType = unknown, StateType = unknown> = Omit<
>;

export type ModuleFn<APIType = unknown, StateType = unknown, HasInit = false> = (
m: ModuleArgs
m: ModuleArgs,
options?: any
) => HasInit extends true
? ModuleWithInit<APIType, StateType>
: ModuleWithoutInit<APIType, StateType>;
Expand Down
46 changes: 22 additions & 24 deletions code/lib/manager-api/src/modules/refs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ export interface SubAPI {
changeRefState: (id: string, previewInitialized: boolean) => void;
}

export const getSourceType = (source: string, refId: string) => {
export const getSourceType = (source: string, refId?: string) => {
const { origin: localOrigin, pathname: localPathname } = location;
const { origin: sourceOrigin, pathname: sourcePathname } = new URL(source);

Expand Down Expand Up @@ -69,12 +69,8 @@ async function handleRequest(

try {
const response = await request;
if (response === false || response === true) {
return {};
}
if (!response.ok) {
return {};
}
if (response === false || response === true) throw new Error('Unexpected boolean response');
if (!response.ok) throw new Error(`Unexpected response not OK: ${response.statusText}`);

const json = await response.json();

Expand Down Expand Up @@ -180,28 +176,30 @@ export const init: ModuleFn<SubAPI, SubState, void> = (
});
}

const [indexFetch, storiesFetch] = await Promise.all(
const [indexResult, storiesResult] = await Promise.all(
['index.json', 'stories.json'].map(async (file) =>
fetch(`${urlParseResult.url}/${file}${query}`, {
headers,
credentials,
})
)
);

if (indexFetch.ok || storiesFetch.ok) {
const [index, metadata] = await Promise.all([
indexFetch.ok ? handleRequest(indexFetch) : handleRequest(storiesFetch),
handleRequest(
fetch(`${urlParseResult.url}/metadata.json${query}`, {
fetch(`${urlParseResult.url}/${file}${query}`, {
headers,
credentials,
cache: 'no-cache',
}).catch(() => false)
),
]);
})
)
)
);

Object.assign(loadedData, { ...index, ...metadata });
if (!indexResult.indexError || !storiesResult.indexError) {
const metadata = await handleRequest(
fetch(`${urlParseResult.url}/metadata.json${query}`, {
headers,
credentials,
cache: 'no-cache',
}).catch(() => false)
);

Object.assign(loadedData, {
...(indexResult.indexError ? storiesResult : indexResult),
...(!metadata.indexError && metadata),
});
} else if (!isPublic) {
// In theory the `/iframe.html` could be private and the `stories.json` could not exist, but in practice
// the only private servers we know about (Chromatic) always include `stories.json`. So we can tell
Expand Down
Loading

0 comments on commit 5658390

Please sign in to comment.