Next.js Discord

Discord Forum

How to get round api being unavailable at build time?

Answered
adam.birds posted this in #help-forum
Open in Discord
How to get round api being unavailable at build time?

So I have a fetch setup to a an APi which runs in my backend docker container. During the build this is unavailable as all my containers are built with docker compose so they are all up or down at the same time. How can i get round this being unavailable at build time and the build failing. Below is my code:

function getBaseUrl() {
    // Check if running on the server
    if (typeof window === "undefined") {
        // Running on server - use Docker container name
        return "http://backend:8000";
    } else {
        // Running on client - use specific domain
        return "https://example.com";
    }
}

// Function to get the full API URL by appending the API path
function getApiUrl(apiPath: string): string {
    const baseUrl = getBaseUrl(); // Get the base URL dynamically
    return `${baseUrl}${apiPath}`; // Construct the full API URL
}

async function getProducts() {
    const res = await fetch(
        getApiUrl("/api/shop/affiliate_products"),
        {
            cache: "no-cache",
        },
    );
    const products = await res.json();
    return products.results;
}

export default async function Home() {
    const products = await getProducts();

    return (
        <div>
            <Navbar navigation={navigation} />

            <HeroSectionComponent />

            <LatestDealsSection products={products} />

            <FooterComponent
                navigation={footerNavigation}
                social={footerSocial}
            />
        </div>
    );
}
Answered by aardani
put this in your page.js
View full answer

8 Replies

put this in your page.js
Answer
export const dynamic = "force-dynamic"
@aardani `export const dynamic = "force-dynamic"`
if using this can you use getserversideprops again or is that still gone?
@adam.birds if using this can you use getserversideprops again or is that still gone?
are you using the pages folder or app folder?
theres no getServerSideProps in app dir