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

Docs2 core: Fetch index.json for composition #18521

Merged
merged 3 commits into from
Jun 21, 2022
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
55 changes: 28 additions & 27 deletions lib/api/src/modules/refs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ async function handleRequest(request: Response | false): Promise<SetRefData> {

const json = await response.json();

if (json.stories) {
if (json.entries || json.stories) {
return { storyIndex: json };
}

Expand Down Expand Up @@ -177,18 +177,34 @@ export const init: ModuleFn<SubAPI, SubState, void> = (
const query = version ? `?version=${version}` : '';
const credentials = isPublic ? 'omit' : 'include';

// 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
// if the ref actually exists by simply checking `stories.json` w/ credentials.

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

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

if (!storiesFetch.ok && !isPublic) {
Object.assign(loadedData, { ...index, ...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
// if the ref actually exists by simply checking `stories.json` w/ credentials.
loadedData.error = {
message: dedent`
Error: Loading of ref failed
Expand All @@ -202,21 +218,6 @@ export const init: ModuleFn<SubAPI, SubState, void> = (
Please check your dev-tools network tab.
`,
} as Error;
} else if (storiesFetch.ok) {
const [storyIndex, metadata] = await Promise.all([
handleRequest(storiesFetch),
handleRequest(
fetch(`${url}/metadata.json${query}`, {
headers: {
Accept: 'application/json',
},
credentials,
cache: 'no-cache',
}).catch(() => false)
),
]);

Object.assign(loadedData, { ...storyIndex, ...metadata });
}

const versions =
Expand Down
Loading