Skip to content

Commit

Permalink
Improve addons
Browse files Browse the repository at this point in the history
  • Loading branch information
cdedreuille committed Jul 9, 2024
1 parent e268d3e commit ae21836
Show file tree
Hide file tree
Showing 2 changed files with 110 additions and 6 deletions.
104 changes: 104 additions & 0 deletions apps/frontpage/app/addons/sitemap.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
import { MetadataRoute } from 'next';
import { fetchAddonsQuery, gql } from '../../lib/fetch-addons-query';
import { validateResponse } from '@repo/utils';

type AddonValue = string;

interface AddonsData {
addons: { name: string }[];
}

export default async function sitemap(): Promise<MetadataRoute.Sitemap> {
/**
* TODO: Move this to a shared location, @repo/utils?
* Canonical source: apps/addon-catalog/lib/fetch-addons-data.ts
*/
async function fetchAddonsData() {
try {
let value: AddonValue[] = [];
async function fetchPartialData(skip: number = 0) {
const data = await fetchAddonsQuery<AddonsData, { skip: number }>(
gql`
query Addons($skip: Int!) {
addons(limit: 30, skip: $skip) {
name
}
}
`,
{
variables: { skip },
},
);

validateResponse(() => data.addons);

const { addons } = data;

value = [...value, ...addons.map(({ name }) => name)];

if (addons.length > 0) await fetchPartialData(skip + addons.length);

return value;
}

return await fetchPartialData();
} catch (error) {
// @ts-expect-error - Seems safe
throw new Error(`Failed to fetch addons data: ${error.message}`);
}
}

type TagValue = string;

interface TagsData {
tags: { name: string }[];
}

/**
* TODO: Move this to a shared location, @repo/utils?
* Canonical source: apps/addon-catalog/lib/fetch-tags-data.ts
*/
async function fetchTagsData({
isCategory,
}: {
isCategory?: boolean;
} = {}) {
try {
let value: TagValue[] = [];
async function fetchPartialData(skip: number = 0) {
const data = await fetchAddonsQuery<
TagsData,
{ isCategory: boolean; skip: number }
>(
gql`
query TagNames($isCategory: Boolean!, $skip: Int!) {
tags(isCategory: $isCategory, limit: 30, skip: $skip) {
name
}
}
`,
{
variables: { isCategory: Boolean(isCategory), skip },
},
);

validateResponse(() => data?.tags);

const { tags } = data;

value = [...value, ...tags.map(({ name }) => name)];

if (tags.length > 0) await fetchPartialData(skip + tags.length);

return value;
}

return await fetchPartialData();
} catch (error) {
// @ts-expect-error - Seems safe
throw new Error(`Failed to fetch addons data: ${error.message}`);
}
}

return [];
}
12 changes: 6 additions & 6 deletions apps/frontpage/app/sitemap.xml
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
<sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
<sitemap>
<loc>https://storybook.js.org/sitemap/docs/sitemap.xml</loc>
<loc>https://storybook.js.org/docs/sitemap.xml</loc>
</sitemap>
<sitemap>
<loc>https://storybook.js.org/sitemap/addons/sitemap.xml</loc>
<loc>https://storybook.js.org/addons/sitemap.xml</loc>
</sitemap>
<sitemap>
<loc>https://storybook.js.org/sitemap/recipes/sitemap.xml</loc>
<loc>https://storybook.js.org/recipes/sitemap.xml</loc>
</sitemap>
<sitemap>
<loc>https://storybook.js.org/sitemap/blog/sitemap.xml</loc>
<loc>https://storybook.js.org/blog/sitemap.xml</loc>
</sitemap>
<sitemap>
<loc>https://storybook.js.org/sitemap/showcase/sitemap.xml</loc>
<loc>https://storybook.js.org/showcase/sitemap.xml</loc>
</sitemap>
<sitemap>
<loc>https://storybook.js.org/sitemap/tutorials/sitemap.xml</loc>
<loc>https://storybook.js.org/tutorials/sitemap.xml</loc>
</sitemap>
</sitemapindex>

0 comments on commit ae21836

Please sign in to comment.