Writing to /public with fs (sitemap.xml)

Hi everyone,

Even though I look in some old posts I did not find a solution for my issue so I am creating a new post.

I am generating a Sitemap.xml dynamically. So far I am serving it live with a route using Webapp. While it works I don’t like that it takes quite a lot of ressources to go through the Collections all the time to generate the new sitemap and could be an easy way for my site to be slow if for some reason someone would permanently generate a sitemap.

So I am looking to save the file to the public folder using fs but so far couldn’t make it work. I never manage to find a way to access properly the path that would work both for development and production. I am following this tutorial : https://thedutchlab.com/blog/generating-a-sitemapxmlgz-every-night but I can’t manage to have this line work:

    fs.writeFileSync(require('path').resolve(__dirname,'../public/sitemap.xml'), sitemap.toString());

The path is never good. I could have it work once in development bu then it failed in production. the code was the following:

	const path = process.env["METEOR_SHELL_DIR"] + "/../../../public/sitemap.xml";

	fs.writeFileSync(path, sitemap.toString());

but in prod the path was the following:

path: 'undefined/../../../public/sitemap.xml'

Thanks in advance for your support.

This is what I was able to find out so far, which is why I never bothered trying to write to the filesystem. With process.cwd() you get the absolute path to the server directory.


and you can construct the former private directory, the one you can access via Assets, by appending assets/app to that path


Unfortunately in a deployed (–production) Meteor node app there is no public directory. What used to be in the public directory will be copied into the two web.browser directories


Hope that helps

I would write the file to /tmp when you generate it. Then you can check for the existence of that file each time someone hits the relevant url. If it is present, serve that. If notz generate and save it. You could even have a time limit for when you regenerate the file, e.g., if the file is older than x hours, regenerate it.

1 Like