1- /**
2- * This file contains the logic for rendering our dynamic and static routes within the Node.js Website
3- * this page route template is used to render the entry points for each locale and then also reused within
4- * the [...path] route to render the individual pages under each locale of the 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- */
1+ import { notFound } from 'next/navigation' ;
2+ import type { FC } from 'react' ;
93
10- import { notFound , redirect } from 'next/navigation' ;
11- import { setRequestLocale } from 'next-intl/server' ;
12- import type { FC , ReactNode } from 'react' ;
13-
14- import { setClientContext } from '#site/client-context' ;
15- import WithLayout from '#site/components/withLayout' ;
164import { ENABLE_STATIC_EXPORT } from '#site/next.constants.mjs' ;
175import { ENABLE_STATIC_EXPORT_LOCALE } from '#site/next.constants.mjs' ;
18- import { PAGE_VIEWPORT } from '#site/next.dynamic.constants.mjs' ;
19- import { dynamicRouter } from '#site/next.dynamic.mjs' ;
20- import { allLocaleCodes , availableLocaleCodes } from '#site/next.locales.mjs' ;
6+ import * as basePage from '#site/next.dynamic.page.mjs' ;
7+ import { availableLocaleCodes } from '#site/next.locales.mjs' ;
218import { defaultLocale } from '#site/next.locales.mjs' ;
22- import { MatterProvider } from '#site/providers/matterProvider' ;
23- import type { Layouts } from '#site/types/layouts' ;
24- import type { ClientSharedServerContext } from '#site/types/server' ;
259
2610type DynamicStaticPaths = { path : Array < string > ; locale : string } ;
2711type DynamicParams = { params : Promise < DynamicStaticPaths > } ;
2812
29- type DynamicPageRender = {
30- content : ReactNode ;
31- layout : Layouts ;
32- context : Partial < ClientSharedServerContext > ;
33- } ;
34-
3513// This is the default Viewport Metadata
3614// @see https://nextjs.org/docs/app/api-reference/functions/generate-viewport#generateviewport-function
37- export const generateViewport = ( ) => ( { ... PAGE_VIEWPORT } ) ;
15+ export const generateViewport = basePage . generateViewport ;
3816
3917// This generates each page's HTML Metadata
4018// @see https://nextjs.org/docs/app/api-reference/functions/generate-metadata
41- export const generateMetadata = async ( props : DynamicParams ) => {
42- const { path = [ ] , locale = defaultLocale . code } = await props . params ;
43-
44- const pathname = dynamicRouter . getPathname ( path ) ;
45-
46- return dynamicRouter . getPageMetadata ( locale , pathname ) ;
47- } ;
19+ export const generateMetadata = basePage . generateMetadata ;
4820
49- // Generates all possible static paths based on the locales and environment configuration
50- // - Returns an empty array if static export is disabled (`ENABLE_STATIC_EXPORT` is false)
51- // - If `ENABLE_STATIC_EXPORT_LOCALE` is true, generates paths for all available locales
52- // - Otherwise, generates paths only for the default locale
53- // @see https://nextjs.org/docs/app/api-reference/functions/generate-static-params
21+ /**
22+ * Generates all possible static paths based on the locales and environment configuration
23+ * - Returns an empty array if static export is disabled (`ENABLE_STATIC_EXPORT` is false)
24+ * - If `ENABLE_STATIC_EXPORT_LOCALE` is true, generates paths for all available locales
25+ * - Otherwise, generates paths only for the default locale
26+ *
27+ * @see https://nextjs.org/docs/app/api-reference/functions/generate-static-params
28+ */
5429export const generateStaticParams = async ( ) => {
5530 // Return an empty array if static export is disabled
5631 if ( ! ENABLE_STATIC_EXPORT ) {
@@ -64,95 +39,29 @@ export const generateStaticParams = async () => {
6439
6540 const routes = await Promise . all (
6641 // Gets all mapped routes to the Next.js Routing Engine by Locale
67- locales . map ( ( locale : string ) => ( { locale } ) )
42+ locales . map ( locale => ( { locale } ) )
6843 ) ;
6944
7045 return routes . flat ( ) . sort ( ) ;
7146} ;
7247
73- // This method is used for retrieving the current locale and pathname from the request
74- export const getLocaleAndPath = async ( props : DynamicParams ) => {
75- const { path = [ ] , locale = defaultLocale . code } = await props . params ;
76-
77- if ( ! availableLocaleCodes . includes ( locale ) ) {
78- // Forces the current locale to be the Default Locale
79- setRequestLocale ( defaultLocale . code ) ;
80-
81- if ( ! allLocaleCodes . includes ( locale ) ) {
82- // when the locale is not listed in the locales, return NotFound
83- return notFound ( ) ;
84- }
85-
86- // Redirect to the default locale path
87- const pathname = dynamicRouter . getPathname ( path ) ;
88-
89- return redirect ( `/${ defaultLocale . code } /${ pathname } ` ) ;
90- }
91-
92- // Configures the current Locale to be the given Locale of the Request
93- setRequestLocale ( locale ) ;
94-
95- // Gets the current full pathname for a given path
96- return [ locale , dynamicRouter . getPathname ( path ) ] as const ;
97- } ;
98-
99- // This method is used for retrieving the Markdown content and context
100- export const getMarkdownContext = async ( locale : string , pathname : string ) => {
101- // We retrieve the source of the Markdown file by doing an educated guess
102- // of what possible files could be the source of the page, since the extension
103- // context is lost from `getStaticProps` as a limitation of Next.js itself
104- const { source, filename } = await dynamicRouter . getMarkdownFile (
105- locale ,
106- pathname
107- ) ;
108-
109- // This parses the source Markdown content and returns a React Component and
110- // relevant context from the Markdown File
111- const { content, frontmatter, headings, readingTime } =
112- await dynamicRouter . getMDXContent ( source , filename ) ;
113-
114- // Metadata and shared Context to be available through the lifecycle of the page
115- const context = {
116- frontmatter : frontmatter ,
117- headings : headings ,
118- pathname : `/${ pathname } ` ,
119- readingTime : readingTime ,
120- filename : filename ,
121- } ;
122-
123- return [ content , context ] as const ;
124- } ;
125-
126- // This method is used for rendering the actual page
127- export const renderPage : FC < DynamicPageRender > = props => {
128- // Defines a shared Server Context for the Client-Side
129- // That is shared for all pages under the dynamic router
130- setClientContext ( props . context ) ;
131-
132- // The Matter Provider allows Client-Side injection of the data
133- // to a shared React Client Provider even though the page is rendered
134- // within a server-side context
135- return (
136- < MatterProvider { ...props . context } >
137- < WithLayout layout = { props . layout } > { props . content } </ WithLayout >
138- </ MatterProvider >
139- ) ;
140- } ;
141-
14248// This method parses the current pathname and does any sort of modifications needed on the route
14349// then it proceeds to retrieve the Markdown file and parse the MDX Content into a React Component
14450// finally it returns (if the locale and route are valid) the React Component with the relevant context
14551// and attached context providers for rendering the current page
14652const getPage : FC < DynamicParams > = async props => {
14753 // Gets the current full pathname for a given path
148- const [ locale , pathname ] = await getLocaleAndPath ( props ) ;
54+ const [ locale , pathname ] = await basePage . getLocaleAndPath ( props ) ;
14955
15056 // Gets the Markdown content and context
151- const [ content , context ] = await getMarkdownContext ( locale , pathname ) ;
57+ const [ content , context ] = await basePage . getMarkdownContext ( {
58+ locale,
59+ pathname,
60+ } ) ;
15261
15362 // If we have a filename and layout then we have a page
15463 if ( context . filename && context . frontmatter . layout ) {
155- return renderPage ( {
64+ return basePage . renderPage ( {
15665 content : content ,
15766 layout : context . frontmatter . layout ,
15867 context : context ,
0 commit comments