Skip to content

Commit 439f978

Browse files
authored
meta: remove weird fetch/generator thingies, rely on next's internal revalidate api (#8001)
1 parent 42d64f0 commit 439f978

File tree

22 files changed

+130
-287
lines changed

22 files changed

+130
-287
lines changed

apps/site/app/[locale]/layout.tsx

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,11 @@ import '#site/styles/index.css';
1414

1515
const fontClasses = classNames(IBM_PLEX_MONO.variable, OPEN_SANS.variable);
1616

17-
type RotLayoutProps = PropsWithChildren<{ params: { locale: string } }>;
17+
type RootLayoutProps = PropsWithChildren<{
18+
params: Promise<{ locale: string }>;
19+
}>;
1820

19-
const RootLayout: FC<RotLayoutProps> = async ({ children, params }) => {
21+
const RootLayout: FC<RootLayoutProps> = async ({ children, params }) => {
2022
const { locale } = await params;
2123

2224
const { langDir, hrefLang } = availableLocalesMap[locale] || defaultLocale;

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/download-snippets/route.tsx

Lines changed: 0 additions & 37 deletions
This file was deleted.

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

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

Lines changed: 0 additions & 31 deletions
This file was deleted.

apps/site/app/[locale]/not-found.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
'use client';
2+
13
import { ArrowRightIcon } from '@heroicons/react/24/solid';
24
import Image from 'next/image';
35
import { useTranslations } from 'next-intl';

apps/site/app/[locale]/page.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ type DynamicParams = { params: Promise<DynamicStaticPaths> };
3131

3232
// This is the default Viewport Metadata
3333
// @see https://nextjs.org/docs/app/api-reference/functions/generate-viewport#generateviewport-function
34-
export const generateViewport = async () => ({ ...PAGE_VIEWPORT });
34+
export const generateViewport = () => ({ ...PAGE_VIEWPORT });
3535

3636
// This generates each page's HTML Metadata
3737
// @see https://nextjs.org/docs/app/api-reference/functions/generate-metadata

apps/site/components/Downloads/DownloadReleasesTable/index.tsx

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import Badge from '@node-core/ui-components/Common/Badge';
2-
import { getTranslations } from 'next-intl/server';
2+
import { useTranslations } from 'next-intl';
33
import type { FC } from 'react';
44

55
import FormattedTime from '#site/components/Common/FormattedTime';
66
import DetailsButton from '#site/components/Downloads/DownloadReleasesTable/DetailsButton';
7-
import getReleaseData from '#site/next-data/releaseData';
7+
import provideReleaseData from '#site/next-data/providers/releaseData';
88

99
const BADGE_KIND_MAP = {
1010
'End-of-life': 'warning',
@@ -14,10 +14,9 @@ const BADGE_KIND_MAP = {
1414
Pending: 'default',
1515
} as const;
1616

17-
const DownloadReleasesTable: FC = async () => {
18-
const releaseData = await getReleaseData();
19-
20-
const t = await getTranslations();
17+
const DownloadReleasesTable: FC = () => {
18+
const releaseData = provideReleaseData();
19+
const t = useTranslations();
2120

2221
return (
2322
<table id="tbVersions" className="download-table full-width">

apps/site/components/withBlogCrossLinks.tsx

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,18 @@ import CrossLink from '#site/components/Common/CrossLink';
55
import getBlogData from '#site/next-data/blogData';
66
import type { BlogCategory } from '#site/types';
77

8-
const WithBlogCrossLinks: FC = async () => {
8+
const WithBlogCrossLinks: FC = () => {
99
const { pathname } = getClientContext();
1010

1111
// Extracts from the static URL the components used for the Blog Post slug
12-
const [, , category, postname] = pathname.split('/');
12+
const [, , category, postname] = pathname.split('/') as [
13+
unknown,
14+
unknown,
15+
BlogCategory,
16+
string,
17+
];
1318

14-
const { posts } = await getBlogData(category as BlogCategory);
19+
const { posts } = getBlogData(category);
1520

1621
const currentItem = posts.findIndex(
1722
({ slug }) => slug === `/blog/${category}/${postname}`

apps/site/components/withDownloadSection.tsx

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
import { getLocale } from 'next-intl/server';
1+
import { useLocale } from 'next-intl';
22
import type { FC, PropsWithChildren } from 'react';
33

44
import { getClientContext } from '#site/client-context';
55
import WithNodeRelease from '#site/components/withNodeRelease';
6-
import getDownloadSnippets from '#site/next-data/downloadSnippets';
7-
import getReleaseData from '#site/next-data/releaseData';
6+
import provideDownloadSnippets from '#site/next-data/providers/downloadSnippets';
7+
import provideReleaseData from '#site/next-data/providers/releaseData';
88
import { defaultLocale } from '#site/next.locales.mjs';
99
import {
1010
ReleaseProvider,
@@ -13,12 +13,13 @@ import {
1313

1414
// By default the translated languages do not contain all the download snippets
1515
// Hence we always merge any translated snippet with the fallbacks for missing snippets
16-
const fallbackSnippets = await getDownloadSnippets(defaultLocale.code);
16+
const fallbackSnippets = provideDownloadSnippets(defaultLocale.code);
1717

18-
const WithDownloadSection: FC<PropsWithChildren> = async ({ children }) => {
19-
const locale = await getLocale();
20-
const releases = await getReleaseData();
21-
const snippets = await getDownloadSnippets(locale);
18+
const WithDownloadSection: FC<PropsWithChildren> = ({ children }) => {
19+
const locale = useLocale();
20+
const releases = provideReleaseData();
21+
22+
const snippets = provideDownloadSnippets(locale);
2223
const { pathname } = getClientContext();
2324

2425
// Some available translations do not have download snippets translated or have them partially translated

0 commit comments

Comments
 (0)