4

I'm currently using Webpack to manage configuration for my React JS app.

I have a config.development.json file that is loaded by my development build script. It contains

{
  "primary1Color": "pink"
}

It's loaded in the Webpack script as follows

externals: {
    configuration: JSON.stringify(require("./config.development.json"))
}

There's a similar set up for production builds.

I reference the config parameters in my app as follows

import configuration from "configuration";

const mainColor = configuration.primary1Color;

This is all working.

However, I'd like to allow the settings to be configured post-deployment---i.e. have the app read the config file when it runs. Then, if customers wish to change the color scheme, they can do so without me having to rebuild the app.

How can I get the app to dynamically load my JSON config file?

1
  • A core question is how often will they update these values. Having to do a new deployment for changing a color is silly. However, if you're only doing this every year, then loading data from a database or API is also silly. Allowing people to edit JSON files is also, not a good idea. Commented Nov 6, 2016 at 23:24

2 Answers 2

2

You don't have to bundle it with webpack. You can use normal ajax call to load the json or use script.js.

https://github.com/ded/script.js

However if you really want to use webpack loader, you can try external-loader.

https://github.com/sheerun/external-loader

More discussion here: "Require external (unmanaged) file"

Sign up to request clarification or add additional context in comments.

Comments

2

I think the best approach would be to create an API endpoint that react interacts with to load them.

1 Comment

but how will you make sure this config is loaded before react app loads

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.