Skip to content

Commit f02b01c

Browse files
committed
chore: self review changes
1 parent 6c8782f commit f02b01c

File tree

6 files changed

+64
-89
lines changed

6 files changed

+64
-89
lines changed

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

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -38,22 +38,21 @@ export const generateStaticParams = async () => {
3838
return [];
3939
}
4040

41-
// Helper function to fetch and map routes for a specific locale
42-
const getRoutesForLocale = async (l: string) => {
43-
const routes = await dynamicRouter.getRoutesByLanguage(l);
41+
const routes = await dynamicRouter.getAllRoutes();
4442

45-
return routes.map(pathname => dynamicRouter.mapPathToRoute(l, pathname));
46-
};
43+
// Helper function to fetch and map routes for a specific locale
44+
const getRoutesForLocale = async (l: string) =>
45+
routes.map(pathname => dynamicRouter.mapPathToRoute(l, pathname));
4746

4847
// Determine which locales to include in the static export
4948
const locales = ENABLE_STATIC_EXPORT_LOCALE
5049
? availableLocaleCodes
5150
: [defaultLocale.code];
5251

5352
// Generates all possible routes for all available locales
54-
const routes = await Promise.all(locales.map(getRoutesForLocale));
53+
const routesWithLocales = await Promise.all(locales.map(getRoutesForLocale));
5554

56-
return routes.flat().sort();
55+
return routesWithLocales.flat().sort();
5756
};
5857

5958
// This method parses the current pathname and does any sort of modifications needed on the route

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

Lines changed: 7 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -11,40 +11,13 @@ import { notFound } from 'next/navigation';
1111
import type { FC } from 'react';
1212

1313
import * as basePage from '#site/app/[locale]/page';
14-
import { provideBlogPosts } from '#site/next-data/providers/blogData';
1514
import { ENABLE_STATIC_EXPORT } from '#site/next.constants.mjs';
16-
import { blogData } from '#site/next.json.mjs';
15+
import { BLOG_DYNAMIC_ROUTES } from '#site/next.dynamic.constants.mjs';
1716
import { defaultLocale } from '#site/next.locales.mjs';
1817

1918
type DynamicStaticPaths = { path: Array<string>; locale: string };
2019
type DynamicParams = { params: Promise<DynamicStaticPaths> };
2120

22-
/**
23-
* This is a list of all static routes or pages from the Website that we do not
24-
* want to allow to be statically built on our Static Export Build.
25-
*/
26-
const BLOG_DYNAMIC_ROUTES = blogData.categories.flatMap(category => {
27-
// Each category can have multiple pages, so we generate a route for each page
28-
const categoryPages = provideBlogPosts(category).pagination.pages;
29-
30-
const categoryRoute = {
31-
locale: defaultLocale.code,
32-
path: [category],
33-
pathname: `${category}`,
34-
};
35-
36-
const categoryPaginationRoutes = Array.from(
37-
{ length: categoryPages },
38-
(_, page) => ({
39-
locale: defaultLocale.code,
40-
path: [category, 'page', `${page + 1}`],
41-
pathname: `${category}/page/${page + 1}`,
42-
})
43-
);
44-
45-
return [categoryRoute, ...categoryPaginationRoutes];
46-
});
47-
4821
// This is the default Viewport Metadata
4922
// @see https://nextjs.org/docs/app/api-reference/functions/generate-viewport#generateviewport-function
5023
export const generateViewport = basePage.generateViewport;
@@ -64,7 +37,10 @@ export const generateStaticParams = async () => {
6437
return [];
6538
}
6639

67-
return BLOG_DYNAMIC_ROUTES;
40+
return BLOG_DYNAMIC_ROUTES.map(pathname => ({
41+
locale: defaultLocale.code,
42+
path: pathname.split('/'),
43+
}));
6844
};
6945

7046
// This method parses the current pathname and does any sort of modifications needed on the route
@@ -75,9 +51,8 @@ const getPage: FC<DynamicParams> = async props => {
7551
// Gets the current full pathname for a given path
7652
const [locale, pathname] = await basePage.getLocaleAndPath(props);
7753

78-
const isDynamicRoute = BLOG_DYNAMIC_ROUTES.some(route =>
79-
route.pathname.includes(pathname)
80-
);
54+
// Verifies if the current route is a dynamic route
55+
const isDynamicRoute = BLOG_DYNAMIC_ROUTES.some(r => r.includes(pathname));
8156

8257
// Gets the Markdown content and context for Blog pages
8358
// otherwise this is likely a blog-category or a blog post

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

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,7 @@ import { parseRichTextIntoPlainText } from '#site/util/string';
1111
// @see https://nextjs.org/docs/app/building-your-application/routing/router-handlers
1212
export const GET = async () => {
1313
// Retrieves all available routes for the default locale
14-
const allAvailbleRoutes = await dynamicRouter.getRoutesByLanguage(
15-
defaultLocale.code
16-
);
14+
const allAvailbleRoutes = await dynamicRouter.getAllRoutes();
1715

1816
// We exclude the blog routes from the available pages metadata
1917
// as they are generated separately and are not part of the static pages

apps/site/app/sitemap.ts

Lines changed: 21 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -5,60 +5,40 @@ import {
55
BASE_URL,
66
EXTERNAL_LINKS_SITEMAP,
77
} from '#site/next.constants.mjs';
8+
import { BLOG_DYNAMIC_ROUTES } from '#site/next.dynamic.constants.mjs';
89
import { dynamicRouter } from '#site/next.dynamic.mjs';
910
import { availableLocaleCodes, defaultLocale } from '#site/next.locales.mjs';
1011

1112
// This is the combination of the Application Base URL and Base PATH
1213
const baseUrlAndPath = `${BASE_URL}${BASE_PATH}`;
1314

15+
// All available alternate locales
16+
const nonDefaultLocales = availableLocaleCodes.filter(
17+
l => l !== defaultLocale.code
18+
);
19+
20+
const getAlternatePath = (r: string, locales: Array<string>) =>
21+
Object.fromEntries(locales.map(l => [l, `${baseUrlAndPath}/${l}/${r}`]));
22+
1423
// This allows us to generate a `sitemap.xml` file dynamically based on the needs of the Node.js Website
1524
const sitemap = async (): Promise<MetadataRoute.Sitemap> => {
16-
const routes = await dynamicRouter.getRoutesByLanguage(defaultLocale.code);
17-
const paths = [];
25+
// Gets a list of all statically available routes
26+
const routes = await dynamicRouter.getAllRoutes();
1827

1928
const currentDate = new Date().toISOString();
2029

21-
for (const route of routes) {
22-
const availableLocalesForRoute = [];
23-
24-
for (const locale of availableLocaleCodes.filter(
25-
locale => locale !== defaultLocale.code
26-
)) {
27-
const markdownFile = await dynamicRouter.getMarkdownFile(locale, route);
28-
const isAvailable = markdownFile.filename !== '';
29-
if (isAvailable) {
30-
availableLocalesForRoute.push(locale);
31-
}
32-
}
33-
34-
const alternatesPaths = availableLocalesForRoute.reduce(
35-
(acc, locale) => ({
36-
...acc,
37-
[locale]: `${baseUrlAndPath}/${locale}/${route}`,
38-
}),
39-
{}
40-
);
30+
const getSitemapEntry = (r: string, locales: Array<string> = []) => ({
31+
url: `${baseUrlAndPath}/${defaultLocale.code}/${r}`,
32+
lastModified: currentDate,
33+
changeFrequency: 'always' as const,
34+
alternates: { languages: getAlternatePath(r, locales) },
35+
});
4136

42-
paths.push({
43-
url: `${baseUrlAndPath}/${defaultLocale.code}/${route}`,
44-
lastModified: currentDate,
45-
changeFrequency: 'always' as const,
46-
alternates: {
47-
languages: {
48-
...alternatesPaths,
49-
},
50-
},
51-
});
52-
}
37+
const staticPaths = routes.map(r => getSitemapEntry(r, nonDefaultLocales));
38+
const blogPaths = BLOG_DYNAMIC_ROUTES.map(r => getSitemapEntry(r));
39+
const externalPaths = EXTERNAL_LINKS_SITEMAP.map(r => getSitemapEntry(r));
5340

54-
return [
55-
...paths,
56-
...EXTERNAL_LINKS_SITEMAP.map(route => ({
57-
url: route,
58-
lastModified: currentDate,
59-
changeFrequency: 'always' as const,
60-
})),
61-
];
41+
return [...staticPaths, ...blogPaths, ...externalPaths];
6242
};
6343

6444
export default sitemap;

apps/site/next.dynamic.constants.mjs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,32 @@
11
'use strict';
22

3+
import { provideBlogPosts } from '#site/next-data/providers/blogData';
4+
import { blogData } from '#site/next.json.mjs';
5+
36
import { BASE_PATH, BASE_URL } from './next.constants.mjs';
47
import { siteConfig } from './next.json.mjs';
58

9+
/**
10+
* This constant is used to create static routes on-the-fly that do not have a file-system
11+
* counterpart route. This is useful for providing routes with matching Layout Names
12+
* but that do not have Markdown content and a matching file for the route
13+
*
14+
* @type {Array<string>} A Map of pathname and Layout Name
15+
*/
16+
export const BLOG_DYNAMIC_ROUTES = [
17+
// Provides Routes for all Blog Categories
18+
...blogData.categories.map(c => `blog/${c}`),
19+
// Provides Routes for all Blog Categories w/ Pagination
20+
...blogData.categories
21+
// retrieves the amount of pages for each blog category
22+
.map(c => [c, provideBlogPosts(c).pagination.pages])
23+
// creates a numeric array for each page and define a pathname for
24+
// each page for a category (i.e. blog/all/page/1)
25+
.map(([c, t]) => [...Array(t).keys()].map(p => `blog/${c}/page/${p + 1}`))
26+
// flattens the array since we have a .map inside another .map
27+
.flat(),
28+
];
29+
630
/**
731
* This is the default Next.js Page Metadata for all pages
832
*

apps/site/next.dynamic.mjs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -78,14 +78,13 @@ const getDynamicRouter = async () => {
7878
});
7979

8080
/**
81-
* This method returns a list of all routes that exist for a given locale
81+
* This method returns a list of all routes that exist
8282
* Note: It will only match routes that have at least one pathname.
83-
*
84-
* @param {string} locale
83+
8584
* @returns {Promise<Array<string>>}
8685
*/
87-
const getRoutesByLanguage = async (locale = defaultLocale.code) =>
88-
[...pathnameToFilename.keys()].filter(p => locale.length && p.length);
86+
const getAllRoutes = async () =>
87+
[...pathnameToFilename.keys()].filter(pathname => pathname.length);
8988

9089
/**
9190
* This method attempts to retrieve either a localized Markdown file
@@ -262,7 +261,7 @@ const getDynamicRouter = async () => {
262261
return {
263262
mapPathToRoute,
264263
getPathname,
265-
getRoutesByLanguage,
264+
getAllRoutes,
266265
getMDXContent,
267266
getMarkdownFile,
268267
getPageMetadata,

0 commit comments

Comments
 (0)