Skip to content

Commit

Permalink
Fetch both index.json and stories.json from refs
Browse files Browse the repository at this point in the history
  • Loading branch information
tmeasday committed Jun 21, 2022
1 parent f33375b commit 48a0bb9
Show file tree
Hide file tree
Showing 2 changed files with 258 additions and 218 deletions.
53 changes: 27 additions & 26 deletions lib/api/src/modules/refs.ts
Original file line number Diff line number Diff line change
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

0 comments on commit 48a0bb9

Please sign in to comment.