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- */
9-
10- import { notFound , redirect } from 'next/navigation' ;
11- import { setRequestLocale } from 'next-intl/server' ;
1+ import { notFound } from 'next/navigation' ;
122import type { FC } from 'react' ;
133
14- import { setClientContext } from '#site/client-context' ;
15- import WithLayout from '#site/components/withLayout' ;
16- import {
17- ENABLE_STATIC_EXPORT_LOCALE ,
18- ENABLE_STATIC_EXPORT ,
19- } from '#site/next.constants.mjs' ;
20- import {
21- PAGE_VIEWPORT ,
22- DYNAMIC_ROUTES ,
23- } from '#site/next.dynamic.constants.mjs' ;
24- import { dynamicRouter } from '#site/next.dynamic.mjs' ;
25- import { allLocaleCodes , availableLocaleCodes } from '#site/next.locales.mjs' ;
4+ import { ENABLE_STATIC_EXPORT } from '#site/next.constants.mjs' ;
5+ import { ENABLE_STATIC_EXPORT_LOCALE } from '#site/next.constants.mjs' ;
6+ import * as basePage from '#site/next.dynamic.page.mjs' ;
7+ import { availableLocaleCodes } from '#site/next.locales.mjs' ;
268import { defaultLocale } from '#site/next.locales.mjs' ;
27- import { MatterProvider } from '#site/providers/matterProvider' ;
289
2910type DynamicStaticPaths = { path : Array < string > ; locale : string } ;
3011type DynamicParams = { params : Promise < DynamicStaticPaths > } ;
3112
3213// This is the default Viewport Metadata
3314// @see https://nextjs.org/docs/app/api-reference/functions/generate-viewport#generateviewport-function
34- export const generateViewport = ( ) => ( { ... PAGE_VIEWPORT } ) ;
15+ export const generateViewport = basePage . generateViewport ;
3516
3617// This generates each page's HTML Metadata
3718// @see https://nextjs.org/docs/app/api-reference/functions/generate-metadata
38- export const generateMetadata = async ( props : DynamicParams ) => {
39- const { path = [ ] , locale = defaultLocale . code } = await props . params ;
40-
41- const pathname = dynamicRouter . getPathname ( path ) ;
42-
43- return dynamicRouter . getPageMetadata ( locale , pathname ) ;
44- } ;
19+ export const generateMetadata = basePage . generateMetadata ;
4520
46- // Generates all possible static paths based on the locales and environment configuration
47- // - Returns an empty array if static export is disabled (`ENABLE_STATIC_EXPORT` is false)
48- // - If `ENABLE_STATIC_EXPORT_LOCALE` is true, generates paths for all available locales
49- // - Otherwise, generates paths only for the default locale
50- // @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+ */
5129export const generateStaticParams = async ( ) => {
5230 // Return an empty array if static export is disabled
5331 if ( ! ENABLE_STATIC_EXPORT ) {
@@ -61,7 +39,7 @@ export const generateStaticParams = async () => {
6139
6240 const routes = await Promise . all (
6341 // Gets all mapped routes to the Next.js Routing Engine by Locale
64- locales . map ( ( locale : string ) => ( { locale } ) )
42+ locales . map ( locale => ( { locale } ) )
6543 ) ;
6644
6745 return routes . flat ( ) . sort ( ) ;
@@ -72,87 +50,22 @@ export const generateStaticParams = async () => {
7250// finally it returns (if the locale and route are valid) the React Component with the relevant context
7351// and attached context providers for rendering the current page
7452const getPage : FC < DynamicParams > = async props => {
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-
9553 // Gets the current full pathname for a given path
96- const pathname = dynamicRouter . getPathname ( path ) ;
97-
98- const staticGeneratedLayout = DYNAMIC_ROUTES . get ( pathname ) ;
54+ const [ locale , pathname ] = await basePage . getLocaleAndPath ( props ) ;
9955
100- // If the current pathname is a statically generated route
101- // it means it does not have a Markdown file nor exists under the filesystem
102- // but it is a valid route with an assigned layout that should be rendered
103- if ( staticGeneratedLayout !== undefined ) {
104- // Metadata and shared Context to be available through the lifecycle of the page
105- const sharedContext = { pathname : `/${ pathname } ` } ;
106-
107- // Defines a shared Server Context for the Client-Side
108- // That is shared for all pages under the dynamic router
109- setClientContext ( sharedContext ) ;
110-
111- // The Matter Provider allows Client-Side injection of the data
112- // to a shared React Client Provider even though the page is rendered
113- // within a server-side context
114- return (
115- < MatterProvider { ...sharedContext } >
116- < WithLayout layout = { staticGeneratedLayout } />
117- </ MatterProvider >
118- ) ;
119- }
120-
121- // We retrieve the source of the Markdown file by doing an educated guess
122- // of what possible files could be the source of the page, since the extension
123- // context is lost from `getStaticProps` as a limitation of Next.js itself
124- const { source, filename } = await dynamicRouter . getMarkdownFile (
56+ // Gets the Markdown content and context
57+ const [ content , context ] = await basePage . getMarkdownContext ( {
12558 locale,
126- pathname
127- ) ;
128-
129- if ( source . length && filename . length ) {
130- // This parses the source Markdown content and returns a React Component and
131- // relevant context from the Markdown File
132- const { content, frontmatter, headings, readingTime } =
133- await dynamicRouter . getMDXContent ( source , filename ) ;
134-
135- // Metadata and shared Context to be available through the lifecycle of the page
136- const sharedContext = {
137- frontmatter : frontmatter ,
138- headings : headings ,
139- pathname : `/${ pathname } ` ,
140- readingTime : readingTime ,
141- filename : filename ,
142- } ;
143-
144- // Defines a shared Server Context for the Client-Side
145- // That is shared for all pages under the dynamic router
146- setClientContext ( sharedContext ) ;
147-
148- // The Matter Provider allows Client-Side injection of the data
149- // to a shared React Client Provider even though the page is rendered
150- // within a server-side context
151- return (
152- < MatterProvider { ...sharedContext } >
153- < WithLayout layout = { frontmatter . layout } > { content } </ WithLayout >
154- </ MatterProvider >
155- ) ;
59+ pathname,
60+ } ) ;
61+
62+ // If we have a filename and layout then we have a page
63+ if ( context . filename && context . frontmatter . layout ) {
64+ return basePage . renderPage ( {
65+ content : content ,
66+ layout : context . frontmatter . layout ,
67+ context : context ,
68+ } ) ;
15669 }
15770
15871 return notFound ( ) ;
0 commit comments