Search code examples
javascriptdeploymentnext.jskubernetes-helm

next.js dont optimise json data file


I want to pass in a replacement JSON file at the deployment of my application container.

Context

I have a next.js application with a JSON file that contains some data, the file is imported to the classes where needed, all works fine.

After building the JSON file does not exist, it seems to be embedded directly in the classes that it's imported by.

The application is dockerized and the container is deployed via a helm chart, it's at this point when deploying I want to provide a new JSON file but as the file is not in the build files I can't replace it.

Is there a nextjs config that will allow me to keep the JSON file external thus allowing me to replace it when deploying without re-building the container?


Solution

  • during the build process, webpack creates ES modules from the imported JSON files. you might want to consider one of the following.

    • try loading the JSON file from an external server. this could be done using a simple fetch. if placed in the getStaticProps or getServerSideProps this request would only be made during the build and on each request respectively.

    • use the dynamic import feature

      import( 'path-to-your-json' );