1

我正在创建一个动态站点地图,并试图将所有博客文章包含在站点地图中。WP 中 GraphiQL IDE 中的 WP GraphQL Query 显示了所有帖子,但是在执行代码时,它只显示前 100 个。我可能忽略了一些东西,但不知道为什么会这样。

GraphQL 查询:

export const GET_POSTS = gql`
  query GET_POSTS {
    posts(first: 10000) {
      nodes {
        title
        uri
        modified
      }
    }
  }
`;

站点地图.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;
4

1 回答 1

1

默认情况下,WPGraphQL 返回的每页最大帖子数为 100。您可以通过增加该 graphql_connection_max_query_amount值来覆盖它。

graphql_connection_max_query_amount过滤器文档中:

过滤每页应查询的最大帖子数。默认值为 100 以防止查询过度占用资源,但是各个系统可以根据其特定需要覆盖此值。此过滤器是在 query_args 过滤器之后有意应用的。

于 2021-12-01T07:28:54.217 回答