In my VueJS app I need to load customization components which add functionalities to a main component. These customization components can be inexistant if there is no customization for a specific client.
I was thinking of using async components with import() function like so:
<template>
<component :is="Component"></component>
</template>
<script>
import { defineAsyncComponent } from 'vue';
import ViewCommon from './ViewCommon.vue';
export default {
components: {
ViewCommon,
},
data() {
return {
Component: null,
}
},
async created() {
let compPath = "";
const compPath = "./mods/" + specificApp.name + "/ViewMod.vue";
this.Component = defineAsyncComponent({
loader: () => import(compPath),
errorComponent: ViewCommon,
timeout: 3000
})
},
}
</script>
It works well, except that it throws an error in the console if the customization component does not exist. In dev mode, with Vite :
Failed to load module script: Expected a JavaScript module script but the server responded with a MIME type of "text/html"...
In production, I get a 404 in console.
But I want to avoid errors since I know it can happen and have a fallback component.
I also tried replacing the defineAsyncComponent function by only using import() and handling errors, but is still throwing the same error:
import(compPath).then(
(mod) => {
console.log("success")
this.Component = mod.default()
},
(error) => {
console.log("error", error)
this.Component = "ViewCommon"
}
)
Is it possible to catch this error? Or do I have to create empty components for clients without customization?