Next.js Discord

Discord Forum

Caching fs based data at build time that gets used in a middleware

joostschuur posted this in #help-forum
Open in Discord
I have middleware that needs to use some data at runtime that can pre-calculated at build time. This data is based on the contents of a repository subdirectory, so I use the fs module to generate this my cached data, assuming that this would only run at build time and have access to the file system.

However, I still get Error: The edge runtime does not support Node.js 'fs' module.. I understand why on the edge it can't access the file system or use the fs module, however, I thought what I was doing was sufficient to only use that module at build time.

So this is in an i18n.ts file and then I import { locales} from './config/i18n:

function getLocales() {
  const locales = ... // computed using fs.readDirSync()

  return locales;

export const locales = getLocales();

So several questions: Is this even sufficient to generate data once at build time and then never need to run getLocales again at runtime? Do I need to do anything to help Next.js to realise that I'm never running that function on the edde? Especially in development mode?

6 Replies

It did occur to me, that simply by having the middleware import from i18n.ts means when accessed as via middleware it still encounters a import of fs. I tried to work around that, by only using fs in a separate file.

So locales.ts:

import fs from 'fs';
import path from 'path';

const SURVEYS_BASE_PATH = 'src/data/surveys';

export function getSurveyLocales() {
  // uses fs.readDirSync

export const surveyLocales = getSurveyLocales();

And then i18n.ts (partially):

export { surveyLocales } from './locales';

const defaultLocale = 'en';

And finally my middleware.ts:

import { surveyLocales } from './config/i18n';

export function middleware(request: NextRequest) {
  // uses surveyLocales

export const config = {
  matcher: ['/((?!api|_next|_vercel|.*\\..*).*)'],

I still get the same error about using fs on the edge though.
we can't use node api on middleware.
is it possible to generate the content to a json file and import it in middleware instead?
I understand why, but I'm not trying to use it on the edge at runtime. I'm trying to generate the data at build time and have it cached when accessed during runtime.
Makes sense though that whatever state might normally be cached during build time (i.e. my exported data from a file) doesn't persist when it's deployed, now that I think abouut it.
I can create a utility script to generate the data as part of the build process. Seems a bit kludgy.
yes sadly the middleware will act like it is running on edge even you are self hosting the server with nodejs