From 9f1fe751e8f5edb045c0a1ba765ed61d9816678e Mon Sep 17 00:00:00 2001 From: joostdecock Date: Sun, 22 Oct 2023 13:19:30 +0200 Subject: [PATCH] wip(org): More changes for #5230 Also clears some of the old stuff for showcase/blog posts --- sites/org/components/mdx/posts/utils.mjs | 62 ------------------- .../pages/account/{sets/[id].mjs => set.mjs} | 23 ++++--- .../account/{sets/index.mjs => sets.mjs} | 0 sites/org/pages/blog/[dir].mjs | 7 ++- sites/org/pages/designs/[design].mjs | 2 +- sites/org/pages/docs/[...slug].mjs | 15 ++--- sites/org/pages/showcase/[dir].mjs | 9 +-- sites/org/site.config.mjs | 4 -- sites/shared/components/account/sets.mjs | 4 +- 9 files changed, 31 insertions(+), 95 deletions(-) delete mode 100644 sites/org/components/mdx/posts/utils.mjs rename sites/org/pages/account/{sets/[id].mjs => set.mjs} (75%) rename sites/org/pages/account/{sets/index.mjs => sets.mjs} (100%) diff --git a/sites/org/components/mdx/posts/utils.mjs b/sites/org/components/mdx/posts/utils.mjs deleted file mode 100644 index f3cae659884..00000000000 --- a/sites/org/components/mdx/posts/utils.mjs +++ /dev/null @@ -1,62 +0,0 @@ -import { localePath } from 'shared/utils.mjs' -import { siteConfig as config } from 'site/site.config.mjs' - -/** - * get pre-generated paths for each language for post slug pages - * @param {Object} posts an object holding all the posts - * @return {String[]} paths for the most recent posts in all locales - */ -export const getPostSlugPaths = (posts) => { - const paths = [] - - for (const lang in posts) { - paths.push( - ...Object.keys(posts[lang]) - .slice(0, config.posts.preGenerate) - .map((slug) => localePath(lang, slug)) - ) - } - - return paths -} - -/** - * get pre-generated paths for each language for post index pages - * @param {Object} posts an object keyed by locale of posts sorted by date published - * @param {String} type post type: blog, showcase, or newsletter - * @return {String[]} paths for the first two pages of posts in all locales - */ -export const getPostIndexPaths = (posts, type) => { - const paths = [] - for (const language in posts) { - paths.push(localePath(language, `${type}/page/1`)) - paths.push(localePath(language, `${type}/page/2`)) - } - - return paths -} - -/** - * get static props for a post index page - * @param {Object} pagenr the current page number in the pagination - * @param {Object} posts on object keyed by slug holding the posts title - * @return {Object} meta on object keyed by slug holding the posts metadata - * @return {Object[]} props.posts the posts to link to on the page - * @return {Number} props.current the current page number - * @return {Number} props.total the total number of pages - */ -export const getPostIndexProps = (pagenr, posts, meta) => { - const pageNum = parseInt(pagenr) - const numLocPages = Math.ceil(Object.keys(posts).length / config.posts.perPage) - if (pageNum > numLocPages) return false - - const pagePosts = Object.entries(posts) - .slice(config.posts.perPage * (pageNum - 1), config.posts.perPage * pageNum) - .map(([slug, post]) => ({ - s: slug, - ...post, - ...meta[slug], - })) - - return { posts: pagePosts, current: pageNum, total: numLocPages } -} diff --git a/sites/org/pages/account/sets/[id].mjs b/sites/org/pages/account/set.mjs similarity index 75% rename from sites/org/pages/account/sets/[id].mjs rename to sites/org/pages/account/set.mjs index bf9302391af..7fbfcabde02 100644 --- a/sites/org/pages/account/sets/[id].mjs +++ b/sites/org/pages/account/set.mjs @@ -1,9 +1,10 @@ // Dependencies import dynamic from 'next/dynamic' import { serverSideTranslations } from 'next-i18next/serverSideTranslations' -import { nsMerge } from 'shared/utils.mjs' +import { nsMerge, getSearchParam } from 'shared/utils.mjs' // Hooks import { useTranslation } from 'next-i18next' +import { useState, useEffect } from 'react' // Components import { PageWrapper, ns as pageNs } from 'shared/components/wrappers/page.mjs' import { ns as authNs } from 'shared/components/wrappers/auth/index.mjs' @@ -32,8 +33,14 @@ const DynamicSet = dynamic( * when path and locale come from static props (as here) * or set them manually. */ -const SetPage = ({ page, id }) => { +const SetPage = ({ page }) => { const { t } = useTranslation(ns) + const [id, setId] = useState() + + useEffect(() => { + const newId = getSearchParam('id') + if (newId !== id) setId(newId) + }, [id]) return ( @@ -46,22 +53,14 @@ const SetPage = ({ page, id }) => { export default SetPage -export async function getStaticProps({ locale, params }) { +export async function getStaticProps({ locale }) { return { props: { ...(await serverSideTranslations(locale, ns)), - id: params.id, page: { locale, - path: ['account', 'sets', params.id], + path: ['account', 'set'], }, }, } } - -/* - * getStaticPaths() is used to specify for which routes (think URLs) - * this page should be used to generate the result. - * To learn more, see: https://nextjs.org/docs/basic-features/data-fetching - */ -export const getStaticPaths = async () => ({ paths: [], fallback: true }) diff --git a/sites/org/pages/account/sets/index.mjs b/sites/org/pages/account/sets.mjs similarity index 100% rename from sites/org/pages/account/sets/index.mjs rename to sites/org/pages/account/sets.mjs diff --git a/sites/org/pages/blog/[dir].mjs b/sites/org/pages/blog/[dir].mjs index 2cc10ac81e6..d7b327e2b86 100644 --- a/sites/org/pages/blog/[dir].mjs +++ b/sites/org/pages/blog/[dir].mjs @@ -47,10 +47,11 @@ export async function getStaticProps({ params, locale }) { } export const getStaticPaths = async () => { - return { - paths: getPostSlugPaths(posts), - fallback: 'blocking', + const paths = [] + for (const lang in posts) { + paths.push(...Object.keys(posts[lang]).map((slug) => localePath(lang, slug))) } + return { paths, fallback: false } } export default BlogPage diff --git a/sites/org/pages/designs/[design].mjs b/sites/org/pages/designs/[design].mjs index c681c23cdbe..a10ba87ebc5 100644 --- a/sites/org/pages/designs/[design].mjs +++ b/sites/org/pages/designs/[design].mjs @@ -54,6 +54,6 @@ export async function getStaticProps({ locale, params }) { export async function getStaticPaths() { return { paths: [...collection].map((design) => `/designs/${design}`), - fallback: 'blocking', + fallback: false, } } diff --git a/sites/org/pages/docs/[...slug].mjs b/sites/org/pages/docs/[...slug].mjs index 825d3ef7e4f..589edf833de 100644 --- a/sites/org/pages/docs/[...slug].mjs +++ b/sites/org/pages/docs/[...slug].mjs @@ -1,4 +1,5 @@ -import { nsMerge } from 'shared/utils.mjs' +import { nsMerge, localePath } from 'shared/utils.mjs' +import { siteConfig } from 'site/site.config.mjs' // Used in static paths import { pages } from 'site/prebuild/docs.en.mjs' // Dependencies @@ -69,11 +70,11 @@ export async function getStaticProps({ locale, params }) { * To learn more, see: https://nextjs.org/docs/basic-features/data-fetching */ export async function getStaticPaths() { - const somePaths = Object.keys(pages).filter((path) => path !== 'docs') - //.filter((path) => path.split('/').length < 5) - - return { - paths: somePaths.map((key) => `/${key}`), - fallback: false, + const allSlugs = Object.keys(pages).filter((path) => path !== 'docs') + const paths = [] + for (const lang of siteConfig.languages) { + paths.push(...allSlugs.map((slug) => localePath(lang, slug))) } + + return { paths, fallback: false } } diff --git a/sites/org/pages/showcase/[dir].mjs b/sites/org/pages/showcase/[dir].mjs index 03e2593c65e..6b0eecfaf49 100644 --- a/sites/org/pages/showcase/[dir].mjs +++ b/sites/org/pages/showcase/[dir].mjs @@ -1,4 +1,4 @@ -import { nsMerge } from 'shared/utils.mjs' +import { nsMerge, localePath } from 'shared/utils.mjs' import { pages as posts } from 'site/prebuild/showcase.mjs' import { getPostSlugPaths } from 'site/components/mdx/posts/utils.mjs' import { serverSideTranslations } from 'next-i18next/serverSideTranslations' @@ -52,10 +52,11 @@ export async function getStaticProps({ params, locale }) { } export const getStaticPaths = async () => { - return { - paths: getPostSlugPaths(posts), - fallback: 'blocking', + const paths = [] + for (const lang in posts) { + paths.push(...Object.keys(posts[lang]).map((slug) => localePath(lang, slug))) } + return { paths, fallback: false } } export default ShowcasePage diff --git a/sites/org/site.config.mjs b/sites/org/site.config.mjs index 9a5fdeec34c..453896d2f4f 100644 --- a/sites/org/site.config.mjs +++ b/sites/org/site.config.mjs @@ -20,8 +20,4 @@ export const siteConfig = { languagesWip: [], site: 'FreeSewing.org', tld: 'org', - posts: { - preGenerate: 6, - perPage: 50, - }, } diff --git a/sites/shared/components/account/sets.mjs b/sites/shared/components/account/sets.mjs index f1e161c9672..3050668c124 100644 --- a/sites/shared/components/account/sets.mjs +++ b/sites/shared/components/account/sets.mjs @@ -427,7 +427,7 @@ export const Mset = ({ id, publicOnly = false }) => { {mset.public && ( - + )} @@ -746,7 +746,7 @@ export const Sets = () => { />
- +
))}