|
| 1 | +import { notFound, redirect } from 'next/navigation'; |
| 2 | +import type { FC } from 'react'; |
| 3 | + |
| 4 | +import provideReleaseData from '#site/next-data/providers/releaseData'; |
| 5 | +import { ENABLE_STATIC_EXPORT } from '#site/next.constants.mjs'; |
| 6 | +import { ARCHIVE_DYNAMIC_ROUTES } from '#site/next.dynamic.constants.mjs'; |
| 7 | +import * as basePage from '#site/next.dynamic.page.mjs'; |
| 8 | +import { defaultLocale } from '#site/next.locales.mjs'; |
| 9 | +import type { DynamicParams } from '#site/types'; |
| 10 | + |
| 11 | +type PageParams = DynamicParams<{ version: string }>; |
| 12 | + |
| 13 | +// This is the default Viewport Metadata |
| 14 | +// @see https://nextjs.org/docs/app/api-reference/functions/generate-viewport#generateviewport-function |
| 15 | +export const generateViewport = basePage.generateViewport; |
| 16 | + |
| 17 | +// This generates each page's HTML Metadata |
| 18 | +// @see https://nextjs.org/docs/app/api-reference/functions/generate-metadata |
| 19 | +export const generateMetadata = basePage.generateMetadata; |
| 20 | + |
| 21 | +// Generates all possible static paths based on the locales and environment configuration |
| 22 | +// - Returns an empty array if static export is disabled (`ENABLE_STATIC_EXPORT` is false) |
| 23 | +// - If `ENABLE_STATIC_EXPORT_LOCALE` is true, generates paths for all available locales |
| 24 | +// - Otherwise, generates paths only for the default locale |
| 25 | +// @see https://nextjs.org/docs/app/api-reference/functions/generate-static-params |
| 26 | +export const generateStaticParams = async () => { |
| 27 | + // Return an empty array if static export is disabled |
| 28 | + if (!ENABLE_STATIC_EXPORT) { |
| 29 | + return []; |
| 30 | + } |
| 31 | + |
| 32 | + return ARCHIVE_DYNAMIC_ROUTES.map(version => ({ |
| 33 | + locale: defaultLocale.code, |
| 34 | + version: version, |
| 35 | + })); |
| 36 | +}; |
| 37 | + |
| 38 | +// This method parses the current pathname and does any sort of modifications needed on the route |
| 39 | +// then it proceeds to retrieve the Markdown file and parse the MDX Content into a React Component |
| 40 | +// finally it returns (if the locale and route are valid) the React Component with the relevant context |
| 41 | +// and attached context providers for rendering the current page |
| 42 | +const getPage: FC<PageParams> = async props => { |
| 43 | + const { version, locale: routeLocale } = await props.params; |
| 44 | + |
| 45 | + // Gets the current full pathname for a given path |
| 46 | + const [locale, pathname] = basePage.getLocaleAndPath(version, routeLocale); |
| 47 | + |
| 48 | + if (version === 'current') { |
| 49 | + const releaseData = provideReleaseData(); |
| 50 | + |
| 51 | + const release = releaseData.find(release => release.status === 'Current'); |
| 52 | + |
| 53 | + redirect(`/${locale}/download/archive/${release?.versionWithPrefix}`); |
| 54 | + } |
| 55 | + |
| 56 | + // Verifies if the current route is a dynamic route |
| 57 | + const isDynamicRoute = ARCHIVE_DYNAMIC_ROUTES.some(r => r.includes(pathname)); |
| 58 | + |
| 59 | + // Gets the Markdown content and context for Download Archive pages |
| 60 | + const [content, context] = await basePage.getMarkdownContext({ |
| 61 | + locale: locale, |
| 62 | + pathname: 'download/archive', |
| 63 | + }); |
| 64 | + |
| 65 | + // If this isn't a valid dynamic route for archive version or there's no markdown |
| 66 | + // file for this, then we fail as not found as there's nothing we can do. |
| 67 | + if (isDynamicRoute && context.filename) { |
| 68 | + return basePage.renderPage({ |
| 69 | + content: content, |
| 70 | + layout: context.frontmatter.layout!, |
| 71 | + context: { ...context, pathname: `/download/archive/${pathname}` }, |
| 72 | + }); |
| 73 | + } |
| 74 | + |
| 75 | + return notFound(); |
| 76 | +}; |
| 77 | + |
| 78 | +// Enforces that this route is used as static rendering |
| 79 | +// Except whenever on the Development mode as we want instant-refresh when making changes |
| 80 | +// @see https://nextjs.org/docs/app/api-reference/file-conventions/route-segment-config#dynamic |
| 81 | +export const dynamic = 'force-static'; |
| 82 | + |
| 83 | +// Ensures that this endpoint is invalidated and re-executed every X minutes |
| 84 | +// so that when new deployments happen, the data is refreshed |
| 85 | +// @see https://nextjs.org/docs/app/api-reference/file-conventions/route-segment-config#revalidate |
| 86 | +export const revalidate = 300; |
| 87 | + |
| 88 | +export default getPage; |
0 commit comments