#Deploying on Vercel - 504 error when using SSR

23 messages · Page 1 of 1 (latest)

reef oracle
#

How are you reading the json files?

valid plinth
# reef oracle How are you reading the json files?

I'm using a helper file which contains several functions, especially

import fs from 'fs/promises';
import path from 'path';

const getDataFilePath = (filename) => path.join(process.cwd(), 'data', filename);

const getJsonData = async (filename) => {
  const fileContent = await fs.readFile(getDataFilePath(filename));
  return JSON.parse(fileContent);
};

export const getFilteredData = async (type, filters = {}, size = 24, offset = 0) => {
  const json = await getJsonData(`${type}.json`);
  let data = transformData(json); // omit functions that extract, filter, sort data
  return data.slice(offset, +offset + size);
};
reef oracle
#

Look at this code from a bundlers perspective: how would you know what files to I include in the bundle from seeing this code?

valid plinth
reef oracle
#

fs.readFile(getDataFilePath(filename))

#

which file would a bundler include when coming across this?

#

it cant know so it wont bundle any

valid plinth
#

But then how can you parse JSON files when the app is deployed ? I believe that locally it's not bundled (or idk but it works), but then when deployed, is a db mandatory ?

reef oracle
#

either a db or you import the json in a way that compatible with bundlers

#

import config from "./config.json"

#

that only works with static files and filenames tho

valid plinth
#

Well so, if I have data files, would the only way to make it work be to import all of them ?

#

Since it can't know dynamically

reef oracle
#

yes

valid plinth
#

Well except that I'd have to know the name in advance...
Or well maybe I could import all my files, and then put them as properties of an object, then get it from there...
Not sure about how bad of a practice it is

reef oracle
#

it depends on the project. i personally wouldnt add a pattern like this to our production codebase

#

if its just about prototyping or testing something its fine i guess

valid plinth
#

If it was a bigger project I believe I'd have gone with a db, but there I have less than 10 JSON files, not sure what I'd do with them

#

I still want to deploy it, so on one hand it'd be used by less than 10 people so really something basic, but on the other hand I'd like to get an insight about how it could be done better

reef oracle
#

are the json files the same in structure or do they differ?

#

when to you import them? does it make sense to split up the logic based on where the actual data is needed?

valid plinth
#

They have similarities, but not all are the same; I haven't made them myself so I have to deal with them