0

I'm working on an app that requires an API key to be inserted into the config.

Since I'm using npm run build to generate a static page (dist folder with index.html that loads app.js and appropriate chunks and styles) - the config file gets read only once and then embedded into generated app.js.

Due to how it works, whenever I change values in the non-embedded config (available in the dist/public/ folder - the values don't refresh in app.js as they should.

Is there any way to import config dynamically whenever index.html along with attached app.js reload?

Here's what I tried:

import * as config from '../public/finnhub.config.json';

const stockProvider = reactive(new FinnhubStockProvider(config));

It does load the config - but only during the build, meaning it's constant.

1 Answer 1

3

Importing the config file would cause it to be bundled. If you want the config file to be read on demand instead, you could fetch it:

import { onMounted, ref } from 'vue'

export default {
  setup() {
    const config = ref({})

    const fetchData = async () => {
      const resp = await fetch('/finnhub.config.json')
      config.value = await resp.json()
    }

    onMounted(() => {
      fetchData()
    })
  }
}
Sign up to request clarification or add additional context in comments.

2 Comments

Yes, just a point of note: this only works when you host the static files and not run index.html in your browser as a file. (double click index.html).
For that wouldn't you just need to run the same func on document load, no?

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.