Skip to content

Commit 515b690

Browse files
committed
chore: minor failsafes and cleanup
1 parent b93e3e2 commit 515b690

File tree

2 files changed

+71
-54
lines changed

2 files changed

+71
-54
lines changed

apps/site/app/[locale]/next-data/api-data/route.ts

Lines changed: 34 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -32,31 +32,40 @@ export const GET = async () => {
3232
authorizationHeaders
3333
);
3434

35-
return gitHubApiResponse.json().then((apiDocsFiles: Array<GitHubApiFile>) => {
36-
// maps over each api file and get the download_url, fetch the content and deflates it
37-
const mappedApiFiles = apiDocsFiles.map(
38-
async ({ name, path: filename, download_url }) => {
39-
const apiFileResponse = await fetch(download_url);
40-
41-
// Retrieves the content as a raw text string
42-
const source = await apiFileResponse.text();
43-
44-
// Removes empty/blank lines or lines just with spaces and trims each line
45-
// from leading and trailing paddings/spaces
46-
const cleanedContent = parseRichTextIntoPlainText(source);
47-
48-
const deflatedSource = deflateSync(cleanedContent).toString('base64');
49-
50-
return {
51-
filename: filename,
52-
pathname: getPathnameForApiFile(name, versionWithPrefix),
53-
content: deflatedSource,
54-
};
55-
}
56-
);
57-
58-
return Promise.all(mappedApiFiles).then(Response.json);
59-
});
35+
// transforms the response into an array of GitHubApiFile
36+
const apiDocsFiles: Array<GitHubApiFile> = await gitHubApiResponse.json();
37+
38+
// prevent the route from crashing if the response is not an array of GitHubApiFile
39+
// and return an empty array instead. This is a fallback for when the GitHub API is not available.
40+
if (!Array.isArray(apiDocsFiles)) {
41+
return Response.json([]);
42+
}
43+
44+
// maps over each api file and get the download_url, fetch the content and deflates it
45+
const mappedApiFiles = apiDocsFiles.map(
46+
async ({ name, path: filename, download_url }) => {
47+
const apiFileResponse = await fetch(download_url);
48+
49+
// Retrieves the content as a raw text string
50+
const source = await apiFileResponse.text();
51+
52+
// Removes empty/blank lines or lines just with spaces and trims each line
53+
// from leading and trailing paddings/spaces
54+
const cleanedContent = parseRichTextIntoPlainText(source);
55+
56+
const deflatedSource = deflateSync(cleanedContent).toString('base64');
57+
58+
return {
59+
filename: filename,
60+
pathname: getPathnameForApiFile(name, versionWithPrefix),
61+
content: deflatedSource,
62+
};
63+
}
64+
);
65+
66+
const data = await Promise.all(mappedApiFiles);
67+
68+
return Response.json(data);
6069
};
6170

6271
// This function generates the static paths that come from the dynamic segments

apps/site/app/[locale]/next-data/page-data/route.ts

Lines changed: 37 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -10,44 +10,52 @@ import { parseRichTextIntoPlainText } from '#site/util/string';
1010
// for a digest and metadata of all existing pages on Node.js Website
1111
// @see https://nextjs.org/docs/app/building-your-application/routing/router-handlers
1212
export const GET = async () => {
13+
// Retrieves all available routes for the default locale
1314
const allAvailbleRoutes = await dynamicRouter.getRoutesByLanguage(
1415
defaultLocale.code
1516
);
1617

17-
const availablePagesMetadata = allAvailbleRoutes
18-
.filter(route => !route.startsWith('blog'))
19-
.map(async pathname => {
20-
const { source, filename } = await dynamicRouter.getMarkdownFile(
21-
defaultLocale.code,
22-
pathname
23-
);
18+
// We exclude the blog routes from the available pages metadata
19+
// as they are generated separately and are not part of the static pages
20+
// and are not part of the static pages metadata
21+
const routesExceptBlog = allAvailbleRoutes.filter(
22+
route => !route.startsWith('blog')
23+
);
24+
25+
const availablePagesMetadata = routesExceptBlog.map(async pathname => {
26+
const { source, filename } = await dynamicRouter.getMarkdownFile(
27+
defaultLocale.code,
28+
pathname
29+
);
30+
31+
// Gets the title and the Description from the Page Metadata
32+
const { title, description } = await dynamicRouter.getPageMetadata(
33+
defaultLocale.code,
34+
pathname
35+
);
2436

25-
// Gets the title and the Description from the Page Metadata
26-
const { title, description } = await dynamicRouter.getPageMetadata(
27-
defaultLocale.code,
28-
pathname
29-
);
37+
// Parser the Markdown source with `gray-matter` and then only
38+
// grabs the markdown content and cleanses it by removing HTML/JSX tags
39+
// removing empty/blank lines or lines just with spaces and trims each line
40+
// from leading and trailing paddings/spaces
41+
const cleanedContent = parseRichTextIntoPlainText(matter(source).content);
3042

31-
// Parser the Markdown source with `gray-matter` and then only
32-
// grabs the markdown content and cleanses it by removing HTML/JSX tags
33-
// removing empty/blank lines or lines just with spaces and trims each line
34-
// from leading and trailing paddings/spaces
35-
const cleanedContent = parseRichTextIntoPlainText(matter(source).content);
43+
// Deflates a String into a base64 string-encoded (zlib compressed)
44+
const content = deflateSync(cleanedContent).toString('base64');
3645

37-
// Deflates a String into a base64 string-encoded (zlib compressed)
38-
const content = deflateSync(cleanedContent).toString('base64');
46+
// Returns metadata of each page available on the Website
47+
return {
48+
filename,
49+
pathname,
50+
title,
51+
description,
52+
content,
53+
};
54+
});
3955

40-
// Returns metadata of each page available on the Website
41-
return {
42-
filename,
43-
pathname,
44-
title,
45-
description,
46-
content,
47-
};
48-
});
56+
const data = await Promise.all(availablePagesMetadata);
4957

50-
return Response.json(await Promise.all(availablePagesMetadata));
58+
return Response.json(data);
5159
};
5260

5361
// This function generates the static paths that come from the dynamic segments

0 commit comments

Comments
 (0)