I get this error on deployment…
[ERROR] Top-level await is currently not supported with the "cjs" output format
9:21:06 PM: .netlify/functions-internal/entry.mjs:3869:0:
9:21:06 PM: 3869 │ await fetchProducts();
My code requires this structure when it loads…
const state = {
products: [],
cart: [],
wishlist: [],
};
const fetchProducts = async function () {
if (localStorage.getItem("products")) {
const data = JSON.parse(localStorage.getItem("products"));
state.products = [...data];
return;
}
const response = await fetch(url);
const data = await response.json();
// Update state with all products data
state.products = [...data];
};
await fetchProducts();
Can you please advise what I need to do to get it to work on netlify? On the localhost I’ve tried to change a few things to avoid the ‘await’ at the top level but the code then doesn’t work. I’d just like some advise on how I need to structure this to avoid the top level await if the top level await is really not possible on netlify…
Regards,
Alim