0

I was able to generate an rss.xml file and write it fs.writefilesync. When viewed locally, it works, but on vercel, the build breaks and says "no such file or director ./public/rss.xml."

If I commnet out the fs.writefilesync line, the deployment and build completes but I get a 404 when visiting mysite.com/rss.xml. Any Idea how I can view the rss.xml file after deployment?

This is the generaterssfile in a utils folder

import fs from 'fs'
import { Feed } from "feed";
 

 async function PostFeed(articles) {

 const site_url='mysite.com';
 const pubDate= new Date()
 const author = 'Post Author'
 const feed = new Feed({
            title: 'mysite | RSS Feed',
            description: '',
            id: site_url,
            link: site_url,
            image: `${site_url}/favicon.ico`,
            favicon: `${site_url }/favicon.ico`,
            copyright: `All rights reserved ${pubDate.getFullYear()}, mysite`,
            updated: pubDate,
            date:pubDate,
            generator: "Feed for mysite",
            feedLinks: {
              rss2: `${site_url}/rss.xml`, 
              json: `${site_url }/rss/feed.json`, 
            },
            author, 
 });
 
 await articles.forEach((post) => {

  const url = `${site_url}/post/${post.node.slug}`;
 
   feed.addItem({
              title: post.node.title,
              id: url,
              link: url,
              description: post.node.excerpt,
              content: post.node.excerpt,
              author: author ,
              contributor: [author],
              date: new Date(post.node.createdAt),
            });
 });
 
 fs.writeFileSync("./public/rss.xml", feed.rss2(), { encoding: "utf-8" });
 
}
export default PostFeed

Then I call PostFeed in getServerSideProps in my index.js

There aren't any specific solutions.

Tongene
  • 1
  • 2

1 Answers1

0

You have to configure versel.json If you deploy your project on Vercel.

Firstly, create a vercel.json file in your root directory, and then add the following code to the vercel.json file in order to grant permissions to all destinations and sources:

{
    "rewrites":  [
        {"source": "/(.*)", "destination": "/"}
    ]
}
  • source: A pattern that matches each incoming pathname (excluding querystring).
  • destination: A location destination defined as an absolute pathname or external URL.

Further readings:

Project Configuration on Vercel

Emre
  • 723
  • 6
  • 14
  • This sounds about right, but I am not sure why it still fails. Anyway, thanks. I will just comment out the fs.writeSyncFile line for now until I find a better way. – Tongene May 16 '23 at 11:45
  • @Tongene If you are encountering an error with `fs`, it appears that the import statement for fs is incorrect. You can import the `fs` library as follows: `import * as fs from "fs"` – Emre May 16 '23 at 13:08
  • So I need to replace this "import fs from 'fs' " with this "import * as fs from "fs"? – Tongene May 17 '23 at 14:56
  • Yes @Tongene. It should work then. – Emre May 17 '23 at 16:32