【问题标题】:Dynamic generated sitemap -there are only declared pages on sitemap动态生成的站点地图 - 站点地图上只有声明的页面
【发布时间】:2021-09-03 05:20:52
【问题描述】:

我想为每个 slug 生成动态 url,但是有一个数组只包含我声明的页面:const pages = ["/", "/about", "/portfolio", "/blog"]; http://localhost:3000/api/my-sitemap。我已经从https://www.npmjs.com/package/sitemap 安装了 npm 站点地图

我在 ../../lib/data.js 中的查询

export const getBlogSlugs = async () => {
  const endpoint =
    "https://api-eu-central-gsagasgasxasasxsaxasxassaster";

  const graphQLClient = new GraphQLClient(endpoint);

  const query = gql`
    {
      posts {
        slug
      }
    }
  `;
  return await graphQLClient.request(query);
};

pages/api/my-sitemap.js

import { getBlogSlugs } from "../../lib/data";
const { SitemapStream, streamToPromise } = require("sitemap");
const { Readable } = require("stream");

export const getStaticProps = async () => {
  const { posts } = await getBlogSlugs();
  return {
    props: {
      posts,
    },
  };
};

export default async (req, res, posts) => {
  try {
    const links = [];
    posts?.map((slug) => {
      links.push({
        url: `/blog/${slug}`,
        changefreq: "daily",
        priority: 0.9,
      });
    });

    // Add other pages
    const pages = ["/", "/about", "/portfolio", "/blog"];
    pages.map((url) => {
      links.push({
        url,
        changefreq: "daily",
        priority: 0.9,
      });
    });

    // Create a stream to write to
    const stream = new SitemapStream({
      hostname: `https://${req.headers.host}`,
    });

    res.writeHead(200, {
      "Content-Type": "application/xml",
    });

    const xmlString = await streamToPromise(
      Readable.from(links).pipe(stream)
    ).then((data) => data.toString());

    res.end(xmlString);
  } catch (e) {
    console.log(e);
    res.send(JSON.stringify(e));
  }
};

我在 pudblic 文件夹中添加了我的 robots.txt:

User-agent: *
Allow: /

Sitemap: http://localhost:3000/api/my-sitemap

我得到的只是声明的页面

localhost:3000/api/my-sitemap

我试过这样也不行:

export const getStaticProps = async () => {
  const data = await getBlogSlugs();
  return {
    props: {
      posts: data.posts,
    },
  };
};

export default async (req, res, posts) => {
  try {
    const links = [];
    posts?.map((post) => {
      links.push({
        url: `/blog/${post.slug}`,
        changefreq: "daily",
        priority: 0.9,
      });
    });

【问题讨论】:

    标签: reactjs next.js seo


    【解决方案1】:

    您不能从 API 路由中使用 getStaticProps。 https://github.com/vercel/next.js/discussions/16068#discussioncomment-703870

    您可以直接在 API 函数中获取数据。

    编辑:在我的应用程序中,我使用下面的 API 路由代码从外部服务器获取数据

    import fetch from "isomorphic-unfetch";
    
    export default async (req, res) => {
      try {
        const result = await fetch("YOUR_URL");
        const posts = await result.json();
    //use posts
        });
      } catch (e) {}
    };
    

    对于 GraphQl,您可以查看 vercel 站点中给出的示例 https://github.com/vercel/next.js/tree/canary/examples/api-routes-graphql

    【讨论】:

    • 我不太明白你的意思;/
    • 我正在寻找如何做到这一点,到目前为止我找不到如何在 api 函数中获取数据的示例。
    • 我已经编辑了答案,我的意思是你不能在内部 nextjs API route 中使用 getStaticProps ,你在 getStaticProps 中使用的逻辑可以直接在 API Routes 函数中实现
    • 有点我需要了解更多关于 API 路由的信息,因为我不知道如何实现它,哈哈。无论如何感谢您的回复。
    猜你喜欢
    • 1970-01-01
    • 2017-11-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-08-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多