Skip to content

Commit

Permalink
- add next-sitemap as dependency
Browse files Browse the repository at this point in the history
- add postbuild command to build sitemap
- add next-sitemap.config.js
- add wordpress-sitemap.xml.js
  • Loading branch information
jasonbahl committed Jun 12, 2024
1 parent 19921ac commit 78f17cd
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 1 deletion.
24 changes: 24 additions & 0 deletions next-sitemap.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/** @type {import('next-sitemap').IConfig} */

const SITE_URL = process.env.NEXT_PUBLIC_SITE_URL

module.exports = {
siteUrl: SITE_URL,
generateRobotsTxt: true,
exclude: ["/docs-sitemap.xml", "/docs/*"],
robotsTxtOptions: {
additionalSitemaps: [
`${SITE_URL}/wordpress-sitemap.xml`, // <==== Add here
],
},
transform: (config, path) => {
if (path.match(/\/\d{4}\/\d{2}\/\d{2}\/.*/gim)) {
return null
}

return {
loc: path,
lastmod: config.autoLastmod ? new Date().toISOString() : undefined,
}
},
}
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
"lint": "eslint . --ext js,jsx,ts,tsx",
"lint:fix": "eslint . --ext js,jsx,ts,tsx --fix",
"predev": "npm run generate",
"prebuild": "npm run generate"
"prebuild": "npm run generate",
"postbuild": "next-sitemap"
},
"dependencies": {
"@algolia/autocomplete-core": "^1.9.2",
Expand Down Expand Up @@ -56,6 +57,7 @@
"mdast-util-to-string": "^3.1.0",
"mdx-annotations": "^0.1.1",
"next": "^13.1.6",
"next-sitemap": "^3.1.3",
"next-themes": "^0.2.1",
"postcss-focus-visible": "^6.0.4",
"postcss-import": "^14.1.0",
Expand Down
71 changes: 71 additions & 0 deletions pages/wordpress-sitemap.xml.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
import { gql } from "@apollo/client"
import { getApolloClient } from "@faustwp/core/dist/mjs/client"
import { getServerSideSitemapLegacy } from "next-sitemap"

const client = getApolloClient()

const SITEMAP_QUERY = gql`
query SitemapQuery($after: String) {
contentNodes(
where: {
contentTypes: [
FIELD_TYPE
POST
PAGE
]
}
first: 50
after: $after
) {
pageInfo {
hasNextPage
endCursor
}
nodes {
uri
modifiedGmt
}
}
}
`

async function getAllWPContent(after = null, acc = []) {
const { data } = await client.query({
query: SITEMAP_QUERY,
variables: {
after,
},
})

console.log(data.contentNodes.nodes)
acc = [...acc, ...data.contentNodes.nodes]

if (data.contentNodes.pageInfo.hasNextPage) {
acc = await getAllWPContent(data.contentNodes.pageInfo.endCursor, acc)
}

return acc
}

// Sitemap component
export default function WPSitemap() {}

// collect all the post
export const getServerSideProps = async (ctx) => {
const nodes = await getAllWPContent()

const allRoutes = nodes.reduce((acc, node) => {
if (!node.uri) {
return acc
}

acc.push({
loc: node.uri,
lastmod: new Date(node.modifiedGmt).toISOString(),
})

return acc
}, [])

return await getServerSideSitemapLegacy(ctx, allRoutes)
}

0 comments on commit 78f17cd

Please sign in to comment.