-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- add postbuild command to build sitemap - add next-sitemap.config.js - add wordpress-sitemap.xml.js
- Loading branch information
Showing
3 changed files
with
98 additions
and
1 deletion.
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
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, | ||
} | ||
}, | ||
} |
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
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) | ||
} |