I have a challenge with reading a config file and using that file in next.js middleware, but middleware is using edge runtime only and therefore node fs can't be used.
However, the project will be deployed and handled by the company's infrastructure and therefore I'm pretty sure the middleware is running with nodejs runtime(?)
What I'm trying to accomplish is something like this (example with very simplified pseudo code below) and wonder if someone have any idea how to make a workaround to make this work? Or have solved something similar in another way?
// config.ts
import {readFileSync, watchFile} from 'node:fs';
import {parse as parseYAML} from 'yaml';
const getConfig = () => {
let config = {};
const configYAML = parseYAML(readFileSync(file, 'utf8'));
watchFile(file, {interval: 5007}, () => {
// return updated config data
return;
});
return configYAML;
};
export default getConfig;
// middleware.ts
import getConfig from './config';
const middleware = async (request: NextRequest) => {
try {
const config = getConfig();
// use config
}
catch (error) {
// throw error
}
};
export default middleware;