diff --git a/packages/nicolium/src/actions/instance.ts b/packages/nicolium/src/actions/instance.ts index 9086d7aba..bf6877f50 100644 --- a/packages/nicolium/src/actions/instance.ts +++ b/packages/nicolium/src/actions/instance.ts @@ -14,16 +14,23 @@ const getHost = () => { } }; -const fetchInstance = async () => { - try { - const client = getClient(); - const instance = await client.instance.getInstance(); +const doFetchInstance = async () => { + const client = getClient(); + const instance = await client.instance.getInstance(); - useInstanceStore.getState().actions.loadInstance(instance); - useComposeStore.getState().actions.importDefaultContentType(instance); - } catch (error) { + useInstanceStore.getState().actions.loadInstance(instance); + useComposeStore.getState().actions.importDefaultContentType(instance); +}; + +const fetchInstance = async () => { + const { fetched, instanceFetchFailed } = useInstanceStore.getState(); + if (fetched || (instanceFetchFailed && !getAuthUserUrl())) return; + + const promise = doFetchInstance().catch((error) => { useInstanceStore.getState().actions.instanceFetchFailed(error); - } + }); + + if (!fetched) await promise; }; const checkIfStandalone = () => @@ -31,9 +38,11 @@ const checkIfStandalone = () => .then(({ ok, headers }) => { const isOk = ok && !!headers.get('content-type')?.includes('application/json'); useInstanceStore.getState().actions.setInstanceFetchFailed(!isOk); + return !isOk; }) .catch((err) => { useInstanceStore.getState().actions.setInstanceFetchFailed(!err.response?.ok); + return true; }); export { getHost, fetchInstance, checkIfStandalone }; diff --git a/packages/nicolium/src/init/nicolium-load.tsx b/packages/nicolium/src/init/nicolium-load.tsx index b32e613eb..e1a6953bb 100644 --- a/packages/nicolium/src/init/nicolium-load.tsx +++ b/packages/nicolium/src/init/nicolium-load.tsx @@ -42,10 +42,12 @@ const NicoliumLoad: React.FC = ({ children }) => { useEffect(() => { /** Load initial data from the backend */ const loadInitial = async () => { - checkIfStandalone(); + const standaloneCheck = checkIfStandalone(); // Await for authenticated fetch await fetchMe(); - // Await for feature detection + // Wait for standalone detection before proceeding + await standaloneCheck; + // Await for feature detection (skipped if already fetched or standalone) await fetchInstance(); // Await for configuration await loadFrontendConfig();