Next.js Discord

Discord Forum

SiteMap shows in development but get a 500 error in prod

Unanswered
Derrick posted this in #help-forum
Open in Discord
i have a sitemap file that shows when i head over to my
.sitemap.xml
when in development but i get a 500 error while in production. First of all the sitemap worked well in both production and development but recently i added blogs to my web app and added it to the sitemap and that's when the issue started.

import { MetadataRoute } from 'next'
import { cookies } from 'next/headers'
import { createClient } from '@/utils/supabase/server'
import { sortedBlogsData } from "@/lib/fetchBlogs";


export default async function sitemap():Promise<MetadataRoute.Sitemap>

{

   const cookieStore = cookies()
   const supabase = createClient(cookieStore)
   const data = await supabase.from('houses').select('*')
   const dataEntries: MetadataRoute.Sitemap = data.data!.map(({id})=>({
      url: `https://together.realestate/properties/${id}`
   }))
   const blogs = sortedBlogsData()

   const blogsEntries: MetadataRoute.Sitemap = blogs.map(({slug})=>({
      url:`https://together.realestate/blogs/${slug}`
   }))

 
 return[
   {
      url:'https://together.realestate',
      lastModified: new Date(),

   },
   {
      url:'https://together.realestate/about',
      lastModified: new Date(),

   },
   {
      url: 'https://together.realestate/account',
      lastModified: new Date(),

      
   },
   {
      url: 'https://together.realestate/login',
      lastModified: new Date(),
   
   },
   {
      url: 'https://together.realestate/marketplace',
      lastModified: new Date(),
  
   },
   {
      url: 'https://together.realestate/new-listing',
      lastModified: new Date(),
   }, 
   {
      url:'https://together.realestate/blogs',
      lastModified: new Date(),
      changeFrequency: 'daily',
   },
   ...dataEntries,
   ...blogsEntries

 ]
}

would really appreciate the help.

12 Replies

500's are accompanied with a server-side error. go to the server log and check what the error is
@joulev sorry for the late reply, the web app i was building was for a client of mine and he was away. The logs show me this error
 ⨯ Error: ENOENT: no such file or directory, scandir '/var/task/blogdata'
    at Object.readdirSync (node:fs:1525:3)
    at an (/var/task/.next/server/app/sitemap.xml/route.js:5:1637)
    at ad (/var/task/.next/server/app/sitemap.xml/route.js:5:2037)
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
    at async af (/var/task/.next/server/app/sitemap.xml/route.js:5:3037)
    at async /var/task/node_modules/next/dist/compiled/next-server/app-route.runtime.prod.js:6:36258
    at async eR.execute (/var/task/node_modules/next/dist/compiled/next-server/app-route.runtime.prod.js:6:26874)
    at async eR.handle (/var/task/node_modules/next/dist/compiled/next-server/app-route.runtime.prod.js:6:37512)
    at async es (/var/task/node_modules/next/dist/compiled/next-server/server.runtime.prod.js:16:25465)
    at async en.responseCache.get.routeKind (/var/task/node_modules/next/dist/compiled/next-server/server.runtime.prod.js:17:1026) {
  errno: -2,
  syscall: 'scandir',
  code: 'ENOENT',
  path: '/var/task/blogdata'
}
@Derrick <@484037068239142956> yes
What kind of file are you trying to read with fs? JSON?
@joulev an MDX file
Sorry for the late replies also ..I don't know why discord never sends me notifications when I get messages @joulev
@joulev I suggest trying this https://vercel.com/guides/how-can-i-use-files-in-serverless-functions#examples-of-reading-files
I'll give it a go and let you know how things go.thank you my friend
hey @joulev after review with the guide you sent i already had implemented it
import path from "path";
import matter  from 'gray-matter'
import fs from 'fs'
import { BlogPost } from "./blog-types";

const blogsDirectory = path.join(process.cwd(), 'blogdata')

export async function sortedBlogsData(){
   //get file names

   const filenames = fs.readdirSync(blogsDirectory)

   const allBlogData = filenames.map((filename)=>{
      //remove .md from file name to get the slug 

      const slug = filename.replace(/\.mdx$/,'')

      //read markdown file as string
      const fullPath = path.join(blogsDirectory, filename) 
      //blogdata/the file name .. will be the output

      const fileContents = fs.readFileSync(fullPath, 'utf-8')

      //use gray-matter to parse the blog's metadata section

      const matterResult = matter(fileContents)

      const blogPost = {
         slug,
         title: matterResult.data.title,
         date: matterResult.data.date,
         summary: matterResult.data.summary,
         image: matterResult.data.image
         
      }

      return blogPost
   })

   return allBlogData.sort((a,b)=> a.date < b.date ? 1: -1)
}


export function getBlogData({slug}: {slug:string}){

   const markdownFile = fs.readFileSync(
      path.join('blogdata', slug + '.mdx'),
      'utf-8'
   );
   const {data:matterResult, content}= matter(markdownFile)

     const blogPostData : BlogPost = {
         slug,
         title: matterResult.title,
         date: matterResult.date,
         summary: matterResult.summary,
         image: matterResult.image,
         content
      }

      return blogPostData

  

}
@Derrick hey <@484037068239142956> after review with the guide you sent i already had implemented it
hmm try this
// next.config.js
module.exports = {
  experimental: {
    outputFileTracingIncludes: {
      '/sitemap.xml': ['./blogdata/**/*'],
    },
  },
}