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

Composition: Fix various CORS issues. #21682

Merged
merged 2 commits into from
Mar 20, 2023
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
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 @@ -179,28 +175,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