#Deploying on Vercel - 504 error when using SSR
23 messages · Page 1 of 1 (latest)
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);
};
Look at this code from a bundlers perspective: how would you know what files to I include in the bundle from seeing this code?
I'm not really sure about this, I vaguely know about bundlers, but couldn't assume too much stuff from there
fs.readFile(getDataFilePath(filename))
which file would a bundler include when coming across this?
it cant know so it wont bundle any
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 ?
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
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
yes
Here "config" would be the JSON object and that's it right ? So I could just replace my
const json = await getJsonData(...);
by something like
const json = config
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
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
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
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?
They have similarities, but not all are the same; I haven't made them myself so I have to deal with them