WP GraphQL 仅查询 returns 生成站点地图时的前 100 个帖子
WP GraphQL query only returns first 100 posts when generating sitemap
我正在创建动态站点地图,并试图拉入所有博客文章以包含在站点地图中。 WP 中 GraphiQL IDE 中的 WP GraphQL 查询显示了所有帖子,但是在执行代码时,它只显示前 100 个。我可能忽略了一些东西,但不确定为什么会这样。
GraphQL 查询:
export const GET_POSTS = gql`
query GET_POSTS {
posts(first: 10000) {
nodes {
title
uri
modified
}
}
}
`;
Sitemap.xml
const Sitemap = () => {};
export const getServerSideProps = async ({ res }) => {
const baseUrl = {
development: "http://localhost:3000",
production: "https://PRODURL.com",
}[process.env.NODE_ENV];
const staticPages = fs
.readdirSync(
{
development: "pages",
production: "./",
}[process.env.NODE_ENV],
)
.filter((staticPage) => {
return ![
"_app.tsx",
"[[...slug]].tsx",
"_error.tsx",
"sitemap.xml.tsx",
].includes(staticPage);
})
.map((staticPagePath) => {
return `${baseUrl}/${staticPagePath}`;
});
const { data } = await client.query({
query: GET_PAGES_SITEMAP,
});
const blogPosts = await client.query({
query: GET_POSTS,
});
const pages = data?.pages.nodes || [];
const posts = blogPosts.data.posts.nodes || [];
const sitemap = `<?xml version="1.0" encoding="UTF-8"?>
<urlset
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"
xmlns:image="http://www.google.com/schemas/sitemap-image/1.1"
xmlns:video="http://www.google.com/schemas/sitemap-video/1.1"
xmlns:news="http://www.google.com/schemas/sitemap-news/0.9"
xmlns:mobile="http://www.google.com/schemas/sitemap-mobile/1.0"
xmlns:pagemap="http://www.google.com/schemas/sitemap-pagemap/1.0"
xmlns:xhtml="http://www.w3.org/1999/xhtml"
xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd">
${staticPages
.map((url) => {
const date = new Date();
const dateFormatter = Intl.DateTimeFormat("sv-SE");
return `
<url>
<loc>${url}</loc>
<lastmod>${dateFormatter.format(date)}</lastmod>
<changefreq>monthly</changefreq>
<priority>1.0</priority>
</url>
`;
})
.join("")}
${pages
.map(({ uri, modified }) => {
const date = new Date(modified);
const dateFormatter = Intl.DateTimeFormat("sv-SE");
return `
<url>
<loc>${baseUrl}${uri}</loc>
<lastmod>${dateFormatter.format(date)}</lastmod>
<changefreq>weekly</changefreq>
<priority>1.0</priority>
</url>
`;
})
.join("")}
${posts
.map(({ uri, modified }) => {
const date = new Date(modified);
const dateFormatter = Intl.DateTimeFormat("sv-SE");
return `
<url>
<loc>${baseUrl}/blog${uri}</loc>
<lastmod>${dateFormatter.format(date)}</lastmod>
<changefreq>weekly</changefreq>
<priority>1.0</priority>
</url>
`;
})
.join("")}
</urlset>
`;
res.setHeader("Content-Type", "text/xml");
res.write(sitemap);
res.end();
return {
props: {},
};
};
export default Sitemap;
默认情况下,WPGraphQL 返回的每页最大帖子数为 100。您可以通过增加 graphql_connection_max_query_amount
值来覆盖此设置。
来自 graphql_connection_max_query_amount
过滤器文档:
Filter the maximum number of posts per page that should be queried.
The default is 100 to prevent queries from being exceedingly resource
intensive, however individual systems can override this for their
specific needs. This filter is intentionally applied AFTER the
query_args filter.
我正在创建动态站点地图,并试图拉入所有博客文章以包含在站点地图中。 WP 中 GraphiQL IDE 中的 WP GraphQL 查询显示了所有帖子,但是在执行代码时,它只显示前 100 个。我可能忽略了一些东西,但不确定为什么会这样。
GraphQL 查询:
export const GET_POSTS = gql`
query GET_POSTS {
posts(first: 10000) {
nodes {
title
uri
modified
}
}
}
`;
Sitemap.xml
const Sitemap = () => {};
export const getServerSideProps = async ({ res }) => {
const baseUrl = {
development: "http://localhost:3000",
production: "https://PRODURL.com",
}[process.env.NODE_ENV];
const staticPages = fs
.readdirSync(
{
development: "pages",
production: "./",
}[process.env.NODE_ENV],
)
.filter((staticPage) => {
return ![
"_app.tsx",
"[[...slug]].tsx",
"_error.tsx",
"sitemap.xml.tsx",
].includes(staticPage);
})
.map((staticPagePath) => {
return `${baseUrl}/${staticPagePath}`;
});
const { data } = await client.query({
query: GET_PAGES_SITEMAP,
});
const blogPosts = await client.query({
query: GET_POSTS,
});
const pages = data?.pages.nodes || [];
const posts = blogPosts.data.posts.nodes || [];
const sitemap = `<?xml version="1.0" encoding="UTF-8"?>
<urlset
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"
xmlns:image="http://www.google.com/schemas/sitemap-image/1.1"
xmlns:video="http://www.google.com/schemas/sitemap-video/1.1"
xmlns:news="http://www.google.com/schemas/sitemap-news/0.9"
xmlns:mobile="http://www.google.com/schemas/sitemap-mobile/1.0"
xmlns:pagemap="http://www.google.com/schemas/sitemap-pagemap/1.0"
xmlns:xhtml="http://www.w3.org/1999/xhtml"
xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd">
${staticPages
.map((url) => {
const date = new Date();
const dateFormatter = Intl.DateTimeFormat("sv-SE");
return `
<url>
<loc>${url}</loc>
<lastmod>${dateFormatter.format(date)}</lastmod>
<changefreq>monthly</changefreq>
<priority>1.0</priority>
</url>
`;
})
.join("")}
${pages
.map(({ uri, modified }) => {
const date = new Date(modified);
const dateFormatter = Intl.DateTimeFormat("sv-SE");
return `
<url>
<loc>${baseUrl}${uri}</loc>
<lastmod>${dateFormatter.format(date)}</lastmod>
<changefreq>weekly</changefreq>
<priority>1.0</priority>
</url>
`;
})
.join("")}
${posts
.map(({ uri, modified }) => {
const date = new Date(modified);
const dateFormatter = Intl.DateTimeFormat("sv-SE");
return `
<url>
<loc>${baseUrl}/blog${uri}</loc>
<lastmod>${dateFormatter.format(date)}</lastmod>
<changefreq>weekly</changefreq>
<priority>1.0</priority>
</url>
`;
})
.join("")}
</urlset>
`;
res.setHeader("Content-Type", "text/xml");
res.write(sitemap);
res.end();
return {
props: {},
};
};
export default Sitemap;
默认情况下,WPGraphQL 返回的每页最大帖子数为 100。您可以通过增加 graphql_connection_max_query_amount
值来覆盖此设置。
来自 graphql_connection_max_query_amount
过滤器文档:
Filter the maximum number of posts per page that should be queried. The default is 100 to prevent queries from being exceedingly resource intensive, however individual systems can override this for their specific needs. This filter is intentionally applied AFTER the query_args filter.