DEV Community

Discussion on: Create a Next.js RSS feed for your static website

Collapse
 
_ndeyefatoudiop profile image
Ndeye Fatou Diop • Edited

Thanks for the post. For those looking, you can validate the RSS feed generated here: validator.w3.org/feed/

For those using Next.js with MDX files (located under src/pages), you can get the posts this way :

const globby = require("globby");
const fs = require("fs");
const path = require("path");
const frontmatter = require("@github-docs/frontmatter");

const getAllPostsXmlData = async () => {
  const pages = await globby([
    "src/pages/**/*.mdx",
    "!src/pages/_*.js",
    "!src/pages/*.js",
    "!src/pages/api",
  ]);
  const posts = pages.map((page) => {
    const fullPath = path.join(process.cwd(), page);
    const fileContents = fs.readFileSync(fullPath, "utf8");
    const matter = frontmatter(fileContents);
    return { ...matter.data, url: getPostUrl(page) };
  });
  return posts;
};
Enter fullscreen mode Exit fullscreen mode