-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #23 from CalebBarnes/feat/generate-static-params-a…
…rchive-taxonomy-pagination Feat/generate static params archive taxonomy pagination
- Loading branch information
Showing
7 changed files
with
437 additions
and
53 deletions.
There are no files selected for viewing
This file contains 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 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 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 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
151 changes: 151 additions & 0 deletions
151
packages/core/src/next-app-functions/generate-static-params/get-archive-static-params.ts
This file contains 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,151 @@ | ||
import { getPostTypes } from "../../api/get-post-types"; | ||
import { getSiteSettings } from "../../api/get-site-settings"; | ||
|
||
export async function getArchiveStaticParams({ postTypes }) { | ||
const wpPostTypes = await getPostTypes(); | ||
const settings = await getSiteSettings(); | ||
const staticParams: { paths: string[] }[] = []; | ||
|
||
if (settings.page_for_posts) { | ||
try { | ||
const endpoint = `${process.env.NEXT_PUBLIC_WP_URL}/wp-json/wp/v2/pages/${settings.page_for_posts}`; | ||
const req = await fetch(endpoint); | ||
const postsPage = await req.json(); | ||
|
||
staticParams.push({ | ||
paths: [postsPage.slug], | ||
}); | ||
|
||
const paginationInfo = await getArchivePaginationInfo({ | ||
rest_base: "posts", | ||
per_page: settings.posts_per_page, | ||
}); | ||
|
||
const postArchiveStaticParams = getStaticParamsFromPaginationInfo({ | ||
archiveSlug: postsPage.slug, | ||
paginationInfo, | ||
}); | ||
|
||
staticParams.push(...postArchiveStaticParams); | ||
} catch (error) { | ||
console.error(error); | ||
} | ||
} | ||
|
||
for (const postType of postTypes) { | ||
const itemKey = Object.keys(wpPostTypes).find( | ||
(key) => wpPostTypes[key].rest_base === postType | ||
); | ||
const matchingPostType = wpPostTypes[itemKey]; | ||
|
||
if ( | ||
matchingPostType.has_archive && | ||
typeof matchingPostType.has_archive === "string" | ||
) { | ||
const paginationInfo = await getArchivePaginationInfo({ | ||
rest_base: matchingPostType.rest_base, | ||
per_page: settings.posts_per_page, | ||
}); | ||
|
||
const postTypeArchiveStaticParams = getStaticParamsFromPaginationInfo({ | ||
archiveSlug: matchingPostType.has_archive, | ||
paginationInfo, | ||
}); | ||
|
||
staticParams.push({ | ||
paths: [matchingPostType.has_archive], | ||
}); | ||
|
||
staticParams.push(...postTypeArchiveStaticParams); | ||
|
||
// if (matchingPostType.taxonomies) { | ||
// for (const taxonomy of matchingPostType.taxonomies) { | ||
// const taxonomyStaticParams = getTaxonomyStaticParams({ taxonomy }); | ||
|
||
// // console.log(matchingPostType.slug); | ||
// // console.log({ taxonomy }); | ||
|
||
// // const params = {} | ||
|
||
// // if (taxonomy.slug === "category") { | ||
// // params.categories = String(term.id); | ||
// // } else if (taxonomy.slug === "post_tag") { | ||
// // params.tags = String(term.id); | ||
// // } else { | ||
// // params[taxonomy.slug] = String(term.id); | ||
// // } | ||
|
||
// // const taxonomyPaginationInfo = await getArchivePaginationInfo({ | ||
// // rest_base: matchingPostType.rest_base, | ||
// // per_page: settings.posts_per_page, | ||
// // params: { | ||
|
||
// // }, | ||
// // }); | ||
// } | ||
// } | ||
} | ||
|
||
// todo: check which taxonomies are registered for this post type | ||
// todo: then fetch all terms for each taxonomy | ||
// todo: then generate static paths for each term | ||
} | ||
|
||
return staticParams; | ||
} | ||
|
||
async function getArchivePaginationInfo({ | ||
rest_base, | ||
per_page, | ||
params = {}, | ||
}: { | ||
rest_base: string; | ||
per_page: number; | ||
params?: Record<string, string>; | ||
}) { | ||
const queryString = new URLSearchParams({ | ||
per_page: String(per_page), | ||
...params, | ||
}).toString(); | ||
|
||
try { | ||
const endpoint = `${process.env.NEXT_PUBLIC_WP_URL}/wp-json/wp/v2/${rest_base}?${queryString}`; | ||
const req = await fetch(endpoint); | ||
|
||
// Check if the fetch request was successful | ||
if (!req.ok) { | ||
throw new Error(`HTTP error! status: ${req.status}`); | ||
} | ||
|
||
const totalPages = req.headers.get("X-WP-TotalPages") | ||
? Number(req.headers.get("X-WP-TotalPages") || 1) | ||
: undefined; | ||
|
||
const totalItems = req.headers.get("X-WP-Total") | ||
? Number(req.headers.get("X-WP-Total") || 0) | ||
: undefined; | ||
|
||
return { | ||
totalPages, | ||
totalItems, | ||
}; | ||
} catch (error) { | ||
console.error("Error fetching data:", error); | ||
return null; | ||
} | ||
} | ||
|
||
function getStaticParamsFromPaginationInfo({ archiveSlug, paginationInfo }) { | ||
const staticParams = []; | ||
|
||
for (let i = 1; i <= paginationInfo.totalPages; i++) { | ||
if (i === 1) { | ||
continue; | ||
} | ||
staticParams.push({ | ||
paths: [archiveSlug, String(i)], | ||
}); | ||
} | ||
|
||
return staticParams; | ||
} |
22 changes: 22 additions & 0 deletions
22
...ages/core/src/next-app-functions/generate-static-params/get-single-items-static-params.ts
This file contains 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,22 @@ | ||
import { getAllItems } from "../../api/get-all-items"; | ||
|
||
export async function getSingleItemsStaticParams({ | ||
postTypes, | ||
}: { | ||
postTypes: string[]; | ||
}) { | ||
const staticParams: { paths: string[] }[] = []; | ||
const allItems = await getAllItems(postTypes); | ||
|
||
for (const item of allItems) { | ||
if (!item.path) { | ||
continue; | ||
} | ||
const pathBreadcrumbs = item.path.split("/").filter((x) => x); | ||
staticParams.push({ | ||
paths: [...pathBreadcrumbs], | ||
}); | ||
} | ||
|
||
return staticParams; | ||
} |
Oops, something went wrong.
f2e1425
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
next-wordpress-starter – ./apps/next-wordpress-starter
next-wordpress-starter-alpha.vercel.app
next-wordpress-starter-calebbarnes.vercel.app
next-wordpress-starter-git-main-calebbarnes.vercel.app