-
-
Notifications
You must be signed in to change notification settings - Fork 6.5k
chore: get rid of hacky dynamic pages, use proper app router #8086
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
dd33d07
chore: get rid of hacky dynamic pages, use proper app router
ovflowd 6c8782f
fix: fixed route generation
ovflowd f02b01c
chore: self review changes
ovflowd 5b3014a
fix: fixed static deployment
ovflowd 254de27
chore: more cleanup and improvements
ovflowd File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,78 @@ | ||
| import { notFound } from 'next/navigation'; | ||
| import type { FC } from 'react'; | ||
|
|
||
| import { ENABLE_STATIC_EXPORT } from '#site/next.constants.mjs'; | ||
| import { BLOG_DYNAMIC_ROUTES } from '#site/next.dynamic.constants.mjs'; | ||
| import * as basePage from '#site/next.dynamic.page.mjs'; | ||
| import { defaultLocale } from '#site/next.locales.mjs'; | ||
|
|
||
| type DynamicStaticPaths = { path: Array<string>; locale: string }; | ||
| type DynamicParams = { params: Promise<DynamicStaticPaths> }; | ||
|
|
||
| // This is the default Viewport Metadata | ||
| // @see https://nextjs.org/docs/app/api-reference/functions/generate-viewport#generateviewport-function | ||
| export const generateViewport = basePage.generateViewport; | ||
|
|
||
| // This generates each page's HTML Metadata | ||
| // @see https://nextjs.org/docs/app/api-reference/functions/generate-metadata | ||
| export const generateMetadata = basePage.generateMetadata; | ||
|
|
||
| // Generates all possible static paths based on the locales and environment configuration | ||
| // - Returns an empty array if static export is disabled (`ENABLE_STATIC_EXPORT` is false) | ||
| // - If `ENABLE_STATIC_EXPORT_LOCALE` is true, generates paths for all available locales | ||
| // - Otherwise, generates paths only for the default locale | ||
| // @see https://nextjs.org/docs/app/api-reference/functions/generate-static-params | ||
| export const generateStaticParams = async () => { | ||
| // Return an empty array if static export is disabled | ||
| if (!ENABLE_STATIC_EXPORT) { | ||
| return []; | ||
| } | ||
|
|
||
| return BLOG_DYNAMIC_ROUTES.map(pathname => ({ | ||
| locale: defaultLocale.code, | ||
| path: pathname.split('/'), | ||
| })); | ||
| }; | ||
|
|
||
| // This method parses the current pathname and does any sort of modifications needed on the route | ||
| // then it proceeds to retrieve the Markdown file and parse the MDX Content into a React Component | ||
| // finally it returns (if the locale and route are valid) the React Component with the relevant context | ||
| // and attached context providers for rendering the current page | ||
| const getPage: FC<DynamicParams> = async props => { | ||
| // Gets the current full pathname for a given path | ||
| const [locale, pathname] = await basePage.getLocaleAndPath(props); | ||
|
|
||
| // Verifies if the current route is a dynamic route | ||
| const isDynamicRoute = BLOG_DYNAMIC_ROUTES.some(r => r.includes(pathname)); | ||
|
|
||
| // Gets the Markdown content and context for Blog pages | ||
| // otherwise this is likely a blog-category or a blog post | ||
| const [content, context] = await basePage.getMarkdownContext({ | ||
| locale: locale, | ||
| pathname: `blog/${pathname}`, | ||
| }); | ||
|
|
||
| // If this isn't a valid dynamic route for blog post or there's no mardown file | ||
| // for this, then we fail as not found as there's nothing we can do. | ||
| if (isDynamicRoute || context.filename) { | ||
| return basePage.renderPage({ | ||
| content: content, | ||
| layout: context.frontmatter.layout ?? 'blog-category', | ||
| context: { ...context, pathname: `/blog/${pathname}` }, | ||
| }); | ||
| } | ||
|
|
||
| return notFound(); | ||
| }; | ||
|
|
||
| // Enforces that this route is used as static rendering | ||
| // Except whenever on the Development mode as we want instant-refresh when making changes | ||
| // @see https://nextjs.org/docs/app/api-reference/file-conventions/route-segment-config#dynamic | ||
| export const dynamic = 'force-static'; | ||
|
|
||
| // Ensures that this endpoint is invalidated and re-executed every X minutes | ||
| // so that when new deployments happen, the data is refreshed | ||
| // @see https://nextjs.org/docs/app/api-reference/file-conventions/route-segment-config#revalidate | ||
| export const revalidate = 300; | ||
|
|
||
| export default getPage; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.