Saturday 15 April 2017

Unintended JSON caching in Heroku app (node.js)

I have a Node.js app running in a Heroku container and I'm experiencing some problems with writing and reading JSON files.

Here's how I'm reading and parsing JSON files:

const feedData = fs.readFileSync('./local/feed.json');
const parsedFeedData = JSON.parse(feedData);

For example, in one use case the parsed object gets passed to a function, to be updated and rewritten to JSON:

const updateFeed = (feed, index) => {
    const feedCopy = Object.assign([], feed);
    feedCopy.splice(index, 1);

    const json = JSON.stringify(feedCopy);
    fs.writeFileSync('./local/feed.json', json);
};

Running these operations locally, I don't encounter any problems but when being ran on Heroku, it acts as if I'm reading the JSON with var parseJSON = require('./file-name'); and reads the data from the cache. I can tell this because the first data update (the function above) is always successful with reading and writing however the second time that function is ran, it reads a JSON file as if the first update never happened to that same JSON file.

I might be absolutely wrong but I suspect this might be due to caching that Heroku is doing. Has anyone ever encountered an issue like this with their node.js instances on Heroku?



via cinnaroll45

No comments:

Post a Comment