Weird Behavior With API Call in Next 13 (App Directory)
Answered
Armant posted this in #help-forum
ArmantOP
I'm having some weird behavior with my project using the App Directory.
When I navigate to the page using my site's navigation bar, the page loads correctly and the API call is made and logged to the browser's console twice (I don't entirely understand why it's happening twice, but that's not the problem here I don't think).
However, when manually typing the page's URL into the browser, the page does not load (it is a blank white page, it doesn't contain my root layout or anything), and the API call is being made and logged to the browser's console infinitely.
The page.tsx:
Could someone please help me understand why this is happening, and/or assist me in fixing it?
When I navigate to the page using my site's navigation bar, the page loads correctly and the API call is made and logged to the browser's console twice (I don't entirely understand why it's happening twice, but that's not the problem here I don't think).
However, when manually typing the page's URL into the browser, the page does not load (it is a blank white page, it doesn't contain my root layout or anything), and the API call is being made and logged to the browser's console infinitely.
The page.tsx:
"use client";
import React from "react";
async function getGames() {
const res = await fetch("/api/games", {
method: "POST",
headers: {
"Content-Type": "application/json"
} as HeadersInit,
body: JSON.stringify({
searchQuery: "",
}),
});
const games = await res.json();
return games;
};
export default async function GamesPage() {
const games = await getGames();
console.log(games);
return (
<div>Games Page</div>
);
};Could someone please help me understand why this is happening, and/or assist me in fixing it?
Answered by Giant panda
Regardless, within server-side code you just do the logic directly
12 Replies
Giant panda
Client components cannot be async
@Giant panda Client components cannot be async
ArmantOP
Are you able to point me in the right direction for the correct way to do this? Would it be to just not have it be async?
I started by having it not "use client", but that came with its own issue as I was getting
TypeError: Failed to parse URL from /api/gamesGiant panda
Don't fetch your own API routes within server components
Asiatic Lion
for server components it is best practice to run the logic directly instead of calling api routes
Giant panda
Just call the underlying logic directly
(Also your endpoint being a POST to get games is really weird, but that's not relevant to the issue)
@Giant panda (Also your endpoint being a POST to **get** games is really weird, but that's not relevant to the issue)
ArmantOP
I'm using IGDB's API, and that's what they require. I thought it was weird too, but it was the only way I could get it to work ¯_(ツ)_/¯
Giant panda
/api/games should be your own API route thoughGiant panda
Regardless, within server-side code you just do the logic directly
Answer
Giant panda
For example querying databases etc
@Giant panda Regardless, within server-side code you just do the logic directly
ArmantOP
Okay, so removing "use client" and changing it to just call their API directly worked, and it doesn't look like it's making the call twice anymore either, so thank you @Giant panda @Asiatic Lion ðŸ‘
async function getGames() {
const res = await fetch("https://api.igdb.com/v4/games", {
method: "POST",
headers: {
"Client-ID": process.env.IGDB_ID,
Authorization: `Bearer ${process.env.IGDB_ACCESS_TOKEN}`,
"Content-Type": "application/json",
} as HeadersInit,
body: `where name ~ *""*; fields name, cover.url; limit 10;`
});
const games = await res.json();
return games;
};