Repository navigation
How to Import config json from database to Docusaurus.Config.js #7847
|
I am trying to fetch data like title, url, favicon and navbar items from backend to Docusaurus.Config.js. But, import statements are giving some error as "Cannot use import statement outside a module". Is there any way I can use import statements in docusaurus.config.js ? Or Any ways to write fetch Api in this file? |
Replies: 1 comment 1 reply
|
We don't support ES import statements in the config file, as we don't support ES modules yet there (but we plan to). This also means you can't use top-level await. I'm not sure how imports are related to fetching data from a backend. 🤷♂️ Technically we support async config creation, see https://docusaurus.io/docs/api/docusaurus-config module.exports = async function configCreatorAsync() {
const {title, favicon, navbarItems} = await fetchConfigFromBackend();
return {
// site config...
};
};Now I'm not sure it's a great idea: you'd have to run this request everytime you start Docusaurus (this means you can't start Docusuaurs if you have no internet connexion, or if your backend is offline etc...). What you could do is just to fetch once in a while a json file with all those data from your backend, and then use it in the Docusaurus config. const {title, favicon, navbarItems} = require("./dataFromBackend.json");
module.exports = {
// site config...
}; |
We don't support ES import statements in the config file, as we don't support ES modules yet there (but we plan to). This also means you can't use top-level await.
I'm not sure how imports are related to fetching data from a backend. 🤷♂️
Technically we support async config creation, see https://docusaurus.io/docs/api/docusaurus-config
Now I'm not sure it's a great idea: you'd have to run this request everytime you start Docusaurus (this means you can't start Docusuaurs if you have no internet connexion, or if your backend …