最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

javascript - getServerSideProps() in Next.js (using the App Router) - Stack Overflow

matteradmin4PV0评论

I have tried to touch Next.js 13 (using the App Router) and faced some issues. The structure of my test app is:

---/school
------/app
   ------/layout.tsx
   ------/page.tsx
---/src

The ./app/page.tsx is

async function Page() {

    const hotels = await hotelService.getAllHotels()

    return (
        <RootLayout>
            <HomePage hotels={hotels}/>
        </RootLayout>
    )
}

export default Page

When next build makes collecting page data step the hotelService.getAllHotels() requests data from API by URL address. The URL address is defined by $API_BACKEND_ENV variable and depends on the environment (URL_FRO_DEV= and URL_FRO_PROD=).

The error es from const res = await fetch(this.url + "/hotel/all", { cache: 'no-store' }) line. If this.url not defined the error is thrown.

How can I avoid the collecting page data and throwing an exception?

NEXT JS docs says the getServerSideProps() is not launched the next build mand. I tried to define hotelService.getAllHotels() inside the getServerSideProps() and expected not request the API. But it does not work.

async function Page({ data }) {

    return (
        <RootLayout>
            <HomePage hotels={data}/>
        </RootLayout>
    )
}

export async function getServerSideProps() {
    const data = await hotelService.getAllHotels()  <<< I expect that this line is not launched while the <next build> mand
    return {
        props: data
    }
}

export default Page

I have tried to touch Next.js 13 (using the App Router) and faced some issues. The structure of my test app is:

---/school
------/app
   ------/layout.tsx
   ------/page.tsx
---/src

The ./app/page.tsx is

async function Page() {

    const hotels = await hotelService.getAllHotels()

    return (
        <RootLayout>
            <HomePage hotels={hotels}/>
        </RootLayout>
    )
}

export default Page

When next build makes collecting page data step the hotelService.getAllHotels() requests data from API by URL address. The URL address is defined by $API_BACKEND_ENV variable and depends on the environment (URL_FRO_DEV=https://1.1.1.1/hotel/all and URL_FRO_PROD=https://2.2.2.2/hotel/all).

The error es from const res = await fetch(this.url + "/hotel/all", { cache: 'no-store' }) line. If this.url not defined the error is thrown.

How can I avoid the collecting page data and throwing an exception?

NEXT JS docs says the getServerSideProps() is not launched the next build mand. I tried to define hotelService.getAllHotels() inside the getServerSideProps() and expected not request the API. But it does not work.

async function Page({ data }) {

    return (
        <RootLayout>
            <HomePage hotels={data}/>
        </RootLayout>
    )
}

export async function getServerSideProps() {
    const data = await hotelService.getAllHotels()  <<< I expect that this line is not launched while the <next build> mand
    return {
        props: data
    }
}

export default Page
Share Improve this question edited Apr 27, 2024 at 7:32 Youssouf Oumar 46.6k16 gold badges103 silver badges105 bronze badges asked Aug 23, 2023 at 15:23 chudnikauchudnikau 392 silver badges7 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 8

If you want a behavior similar to getServerSideProps that does not run at build time, use the dynamic route config, like so:

// layout.js / page.js / route.js

export const dynamic = "force-dynamic"; // 
Post a comment

comment list (0)

  1. No comments so far