Skip to content

Commit aa2ebec

Browse files
committed
feat(mdx): move to next-mdx-remote
1 parent fcfc7e2 commit aa2ebec

File tree

32 files changed

+587
-1417
lines changed

32 files changed

+587
-1417
lines changed
Lines changed: 2 additions & 97 deletions
Original file line numberDiff line numberDiff line change
@@ -1,97 +1,2 @@
1-
/**
2-
* This file extends on the `page.tsx` file, which is the default file that is used to render
3-
* the entry points for each locale and then also reused within the [...path] route to render the
4-
* and contains all logic for rendering our dynamic and static routes within the Node.js Website.
5-
*
6-
* Note: that each `page.tsx` should have its own `generateStaticParams` to prevent clash of
7-
* dynamic params, which will lead on static export errors and other sort of issues.
8-
*/
9-
10-
import { availableLocaleCodes, defaultLocale } from '@node-core/website-i18n';
11-
import { notFound } from 'next/navigation';
12-
13-
import { ENABLE_STATIC_EXPORT } from '#site/next.constants.mjs';
14-
import { ENABLE_STATIC_EXPORT_LOCALE } from '#site/next.constants.mjs';
15-
import { dynamicRouter } from '#site/next.dynamic.mjs';
16-
import * as basePage from '#site/next.dynamic.page.mjs';
17-
18-
import type { DynamicParams } from '#site/types';
19-
import type { FC } from 'react';
20-
21-
type PageParams = DynamicParams<{ path: Array<string> }>;
22-
23-
// This is the default Viewport Metadata
24-
// @see https://nextjs.org/docs/app/api-reference/functions/generate-viewport#generateviewport-function
25-
export const generateViewport = basePage.generateViewport;
26-
27-
// This generates each page's HTML Metadata
28-
// @see https://nextjs.org/docs/app/api-reference/functions/generate-metadata
29-
export const generateMetadata = basePage.generateMetadata;
30-
31-
// Generates all possible static paths based on the locales and environment configuration
32-
// - Returns an empty array if static export is disabled (`ENABLE_STATIC_EXPORT` is false)
33-
// - If `ENABLE_STATIC_EXPORT_LOCALE` is true, generates paths for all available locales
34-
// - Otherwise, generates paths only for the default locale
35-
// @see https://nextjs.org/docs/app/api-reference/functions/generate-static-params
36-
export const generateStaticParams = async () => {
37-
// Return an empty array if static export is disabled
38-
if (!ENABLE_STATIC_EXPORT) {
39-
return [];
40-
}
41-
42-
const routes = await dynamicRouter.getAllRoutes();
43-
44-
// Helper function to fetch and map routes for a specific locale
45-
const getRoutesForLocale = async (l: string) =>
46-
routes.map(pathname => dynamicRouter.mapPathToRoute(l, pathname));
47-
48-
// Determine which locales to include in the static export
49-
const locales = ENABLE_STATIC_EXPORT_LOCALE
50-
? availableLocaleCodes
51-
: [defaultLocale.code];
52-
53-
// Generates all possible routes for all available locales
54-
const routesWithLocales = await Promise.all(locales.map(getRoutesForLocale));
55-
56-
return routesWithLocales.flat().sort();
57-
};
58-
59-
// This method parses the current pathname and does any sort of modifications needed on the route
60-
// then it proceeds to retrieve the Markdown file and parse the MDX Content into a React Component
61-
// finally it returns (if the locale and route are valid) the React Component with the relevant context
62-
// and attached context providers for rendering the current page
63-
const getPage: FC<PageParams> = async props => {
64-
const { path, locale: routeLocale } = await props.params;
65-
66-
// Gets the current full pathname for a given path
67-
const [locale, pathname] = basePage.getLocaleAndPath(path, routeLocale);
68-
69-
// Gets the Markdown content and context
70-
const [content, context] = await basePage.getMarkdownContext({
71-
locale,
72-
pathname,
73-
});
74-
75-
// If we have a filename and layout then we have a page
76-
if (context.filename && context.frontmatter.layout) {
77-
return basePage.renderPage({
78-
content,
79-
layout: context.frontmatter.layout,
80-
context,
81-
});
82-
}
83-
84-
return notFound();
85-
};
86-
87-
// Enforces that this route is used as static rendering
88-
// Except whenever on the Development mode as we want instant-refresh when making changes
89-
// @see https://nextjs.org/docs/app/api-reference/file-conventions/route-segment-config#dynamic
90-
export const dynamic = 'force-static';
91-
92-
// Ensures that this endpoint is invalidated and re-executed every X minutes
93-
// so that when new deployments happen, the data is refreshed
94-
// @see https://nextjs.org/docs/app/api-reference/file-conventions/route-segment-config#revalidate
95-
export const revalidate = 300;
96-
97-
export default getPage;
1+
export * from '../page';
2+
export { default } from '../page';

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

Lines changed: 0 additions & 82 deletions
This file was deleted.
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
import { defaultLocale } from '@node-core/website-i18n';
2+
import { notFound } from 'next/navigation';
3+
4+
import { ENABLE_STATIC_EXPORT } from '#site/next.constants.mjs';
5+
import { blogData } from '#site/next.json.mjs';
6+
import { getMarkdownFile } from '#site/router';
7+
import { BLOG_DYNAMIC_ROUTES } from '#site/router/constants';
8+
import { renderPage } from '#site/router/render';
9+
10+
import type { DynamicParams } from '#site/types';
11+
import type { FC } from 'react';
12+
13+
type PageParams = DynamicParams<{ cat: string }>;
14+
15+
// Generates all possible static paths based on the locales and environment configuration
16+
// - Returns an empty array if static export is disabled (`ENABLE_STATIC_EXPORT` is false)
17+
// - If `ENABLE_STATIC_EXPORT_LOCALE` is true, generates paths for all available locales
18+
// - Otherwise, generates paths only for the default locale
19+
// @see https://nextjs.org/docs/app/api-reference/functions/generate-static-params
20+
export const generateStaticParams = async () => {
21+
// Return an empty array if static export is disabled
22+
if (!ENABLE_STATIC_EXPORT) {
23+
return [];
24+
}
25+
26+
return blogData.categories.map(cat => ({
27+
locale: defaultLocale.code,
28+
cat,
29+
}));
30+
};
31+
32+
// This method parses the current pathname and does any sort of modifications needed on the route
33+
// then it proceeds to retrieve the Markdown file and parse the MDX Content into a React Component
34+
// finally it returns (if the locale and route are valid) the React Component with the relevant context
35+
// and attached context providers for rendering the current page
36+
const getPage: FC<PageParams> = async props => {
37+
const { cat, locale } = await props.params;
38+
39+
// Verifies if the current route is a dynamic route
40+
const isDynamicRoute = BLOG_DYNAMIC_ROUTES.some(r => r.includes(cat));
41+
42+
if (isDynamicRoute) {
43+
const file = (await getMarkdownFile(locale, 'blog'))!;
44+
file.pathname = `/blog/${cat}`;
45+
46+
return renderPage(file);
47+
}
48+
49+
return notFound();
50+
};
51+
52+
export * from '../../page';
53+
export default getPage;

apps/site/app/[locale]/download/archive/[version]/page.tsx

Lines changed: 10 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,14 @@ import { notFound, redirect } from 'next/navigation';
44
import provideReleaseData from '#site/next-data/providers/releaseData';
55
import provideReleaseVersions from '#site/next-data/providers/releaseVersions';
66
import { ENABLE_STATIC_EXPORT } from '#site/next.constants.mjs';
7-
import * as basePage from '#site/next.dynamic.page.mjs';
7+
import { getMarkdownFile } from '#site/router';
8+
import { renderPage } from '#site/router/render';
89

910
import type { DynamicParams } from '#site/types';
1011
import type { FC } from 'react';
1112

1213
type PageParams = DynamicParams<{ version: string }>;
1314

14-
// This is the default Viewport Metadata
15-
// @see https://nextjs.org/docs/app/api-reference/functions/generate-viewport#generateviewport-function
16-
export const generateViewport = basePage.generateViewport;
17-
18-
// This generates each page's HTML Metadata
19-
// @see https://nextjs.org/docs/app/api-reference/functions/generate-metadata
20-
export const generateMetadata = basePage.generateMetadata;
21-
2215
// Generates all possible static paths based on the locales and environment configuration
2316
// - Returns an empty array if static export is disabled (`ENABLE_STATIC_EXPORT` is false)
2417
// - If `ENABLE_STATIC_EXPORT_LOCALE` is true, generates paths for all available locales
@@ -43,10 +36,7 @@ export const generateStaticParams = async () => {
4336
// finally it returns (if the locale and route are valid) the React Component with the relevant context
4437
// and attached context providers for rendering the current page
4538
const getPage: FC<PageParams> = async props => {
46-
const { version, locale: routeLocale } = await props.params;
47-
48-
// Gets the current full pathname for a given path
49-
const [locale, pathname] = basePage.getLocaleAndPath(version, routeLocale);
39+
const { version, locale } = await props.params;
5040

5141
if (version === 'current') {
5242
const releaseData = await provideReleaseData();
@@ -59,35 +49,19 @@ const getPage: FC<PageParams> = async props => {
5949
const versions = await provideReleaseVersions();
6050

6151
// Verifies if the current route is a dynamic route
62-
const isDynamicRoute = versions.some(r => r.includes(pathname));
63-
64-
// Gets the Markdown content and context for Download Archive pages
65-
const [content, context] = await basePage.getMarkdownContext({
66-
locale,
67-
pathname: 'download/archive',
68-
});
52+
const isDynamicRoute = versions.some(r => r.includes(version));
6953

7054
// If this isn't a valid dynamic route for archive version or there's no markdown
7155
// file for this, then we fail as not found as there's nothing we can do.
72-
if (isDynamicRoute && context.filename) {
73-
return basePage.renderPage({
74-
content,
75-
layout: context.frontmatter.layout!,
76-
context: { ...context, pathname: `/download/archive/${pathname}` },
77-
});
56+
if (isDynamicRoute) {
57+
const markdown = (await getMarkdownFile(locale, 'download/archive'))!;
58+
markdown.pathname = `/download/archive/${version}`;
59+
60+
return renderPage(markdown);
7861
}
7962

8063
return notFound();
8164
};
8265

83-
// Enforces that this route is used as static rendering
84-
// Except whenever on the Development mode as we want instant-refresh when making changes
85-
// @see https://nextjs.org/docs/app/api-reference/file-conventions/route-segment-config#dynamic
86-
export const dynamic = 'force-static';
87-
88-
// Ensures that this endpoint is invalidated and re-executed every X minutes
89-
// so that when new deployments happen, the data is refreshed
90-
// @see https://nextjs.org/docs/app/api-reference/file-conventions/route-segment-config#revalidate
91-
export const revalidate = 300;
92-
9366
export default getPage;
67+
export * from '#site/router/page';

0 commit comments

Comments
 (0)