1
0
Fork 0

fix(org): Avoid confusing about what slug means

This commit is contained in:
joostdecock 2023-07-21 19:22:10 +02:00
parent f985389236
commit efc1821150
2 changed files with 17 additions and 31 deletions

View file

@ -15,9 +15,9 @@ const namespaces = [...layoutNs, ...postNs, ...pageNs]
* when path and locale come from static props (as here) * when path and locale come from static props (as here)
* or set them manually. * or set them manually.
*/ */
const BlogPage = ({ locale, slug, page }) => { const BlogPage = ({ locale, dir, page }) => {
// function to load the correct markdown // function to load the correct markdown
const loader = useCallback(() => import(`orgmarkdown/blog/${slug}/${locale}.md`), [slug, locale]) const loader = useCallback(() => import(`orgmarkdown/blog/${dir}/${locale}.md`), [dir, locale])
// load the markdown // load the markdown
const { frontmatter, MDX } = useDynamicMdx(loader) const { frontmatter, MDX } = useDynamicMdx(loader)
@ -28,14 +28,7 @@ const BlogPage = ({ locale, slug, page }) => {
title={frontmatter.title} title={frontmatter.title}
layout={(props) => <PostLayout {...props} {...{ slug: page.path.join('/'), frontmatter }} />} layout={(props) => <PostLayout {...props} {...{ slug: page.path.join('/'), frontmatter }} />}
> >
<PostArticle <PostArticle {...{ frontmatter, MDX }} />
{...{
slug,
frontmatter,
MDX,
page,
}}
/>
</PageWrapper> </PageWrapper>
) )
} }
@ -51,19 +44,19 @@ const BlogPage = ({ locale, slug, page }) => {
* To learn more, see: https://nextjs.org/docs/basic-features/data-fetching * To learn more, see: https://nextjs.org/docs/basic-features/data-fetching
*/ */
export async function getStaticProps({ params, locale }) { export async function getStaticProps({ params, locale }) {
const { slug } = params const { dir } = params
// if the slug isn't present in the prebuilt posts, return 404 // if the dir isn't present in the prebuilt posts, return 404
if (!Object.keys(posts).includes(`blog/${slug}`)) return { notFound: true } if (!Object.keys(posts[locale]).includes(`blog/${dir}`)) return { notFound: true }
return { return {
props: { props: {
slug, dir,
locale, locale,
...(await serverSideTranslations(locale, namespaces)), ...(await serverSideTranslations(locale, namespaces)),
page: { page: {
locale, locale,
path: ['blog', slug], path: ['blog', dir],
}, },
}, },
} }

View file

@ -15,11 +15,11 @@ const namespaces = [...layoutNs, ...postNs, ...pageNs]
* when path and locale come from static props (as here) * when path and locale come from static props (as here)
* or set them manually. * or set them manually.
*/ */
const ShowcasePage = ({ locale, slug, page }) => { const ShowcasePage = ({ locale, dir, page }) => {
// function to load the correct markdown // function to load the correct markdown
const loader = useCallback( const loader = useCallback(
() => import(`orgmarkdown/showcase/${slug}/${locale}.md`), () => import(`orgmarkdown/showcase/${dir}/${locale}.md`),
[slug, locale] [dir, locale]
) )
const { frontmatter, MDX } = useDynamicMdx(loader) const { frontmatter, MDX } = useDynamicMdx(loader)
@ -31,14 +31,7 @@ const ShowcasePage = ({ locale, slug, page }) => {
title={frontmatter.title} title={frontmatter.title}
layout={(props) => <PostLayout {...props} {...{ slug: page.path.join('/'), frontmatter }} />} layout={(props) => <PostLayout {...props} {...{ slug: page.path.join('/'), frontmatter }} />}
> >
<PostArticle <PostArticle {...{ frontmatter, MDX }} />
{...{
slug,
frontmatter,
MDX,
page,
}}
/>
</PageWrapper> </PageWrapper>
) )
} }
@ -54,19 +47,19 @@ const ShowcasePage = ({ locale, slug, page }) => {
* To learn more, see: https://nextjs.org/docs/basic-features/data-fetching * To learn more, see: https://nextjs.org/docs/basic-features/data-fetching
*/ */
export async function getStaticProps({ params, locale }) { export async function getStaticProps({ params, locale }) {
const { slug } = params const { dir } = params
// if the slug isn't present in the prebuilt posts, return 404 // if the dir isn't present in the prebuilt posts, return 404
if (!Object.keys(posts).includes(`showcase/${slug}`)) return { notFound: true } if (!Object.keys(posts[locale]).includes(`showcase/${dir}`)) return { notFound: true }
return { return {
props: { props: {
slug, dir,
locale, locale,
...(await serverSideTranslations(locale, namespaces)), ...(await serverSideTranslations(locale, namespaces)),
page: { page: {
locale, locale,
path: ['showcase', slug], path: ['showcase', dir],
}, },
}, },
} }