Skip to content

Commit 1dcfb66

Browse files
authored
GH-110 Fix strapi to fetch data in realtime. (#110)
* Fix strapi to fetch data in realtime. Signed-off-by: Martin Sulikowski <[email protected]> * Fix data Signed-off-by: Martin Sulikowski <[email protected]> * fix Signed-off-by: Martin Sulikowski <[email protected]> * fix Signed-off-by: Martin Sulikowski <[email protected]> * Final fix Signed-off-by: Martin Sulikowski <[email protected]> --------- Signed-off-by: Martin Sulikowski <[email protected]>
1 parent cb86fa9 commit 1dcfb66

File tree

5 files changed

+1110
-43
lines changed

5 files changed

+1110
-43
lines changed

app/api/project/route.ts

Lines changed: 30 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,36 @@
11
import { NextResponse } from "next/server";
22

3+
export const revalidate = 3600;
4+
35
export async function GET(request: Request) {
4-
const res = await fetch(
5-
`https://cms.eternalcode.pl/api/projects?populate=*`,
6-
{
7-
headers: {
8-
Authorization: `Bearer ${process.env.ETERNALCODE_STRAPI_KEY}`,
9-
},
10-
}
11-
);
6+
try {
7+
const res = await fetch(
8+
`https://cms.eternalcode.pl/api/projects?populate=*`,
9+
{
10+
headers: {
11+
Authorization: `Bearer ${process.env.ETERNALCODE_STRAPI_KEY}`,
12+
},
13+
next: { revalidate }
14+
}
15+
);
1216

13-
const body = await res.json();
17+
if (!res.ok) {
18+
const errorBody = await res.json();
19+
return NextResponse.json(errorBody, { status: res.status });
20+
}
1421

15-
if (!res.ok) {
16-
return NextResponse.json(body, { status: 404 });
22+
const body = await res.json();
23+
24+
return NextResponse.json(body, {
25+
headers: {
26+
'Cache-Control': 'public, max-age=60, stale-while-revalidate=3600',
27+
}
28+
});
29+
} catch (error) {
30+
console.error("Error fetching projects:", error);
31+
return NextResponse.json(
32+
{ error: "Failed to fetch projects" },
33+
{ status: 500 }
34+
);
1735
}
18-
19-
return NextResponse.json(body);
20-
}
36+
}

app/api/team/route.ts

Lines changed: 30 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,36 @@
11
import { NextResponse } from "next/server";
22

3+
export const revalidate = 3600; // 1 hour
4+
35
export async function GET(request: Request) {
4-
const res = await fetch(
5-
`https://cms.eternalcode.pl/api/team-members?populate=*`,
6-
{
7-
headers: {
8-
Authorization: `Bearer ${process.env.ETERNALCODE_STRAPI_KEY}`,
9-
},
10-
}
11-
);
6+
try {
7+
const res = await fetch(
8+
`https://cms.eternalcode.pl/api/team-members?populate=*`,
9+
{
10+
headers: {
11+
Authorization: `Bearer ${process.env.ETERNALCODE_STRAPI_KEY}`,
12+
},
13+
next: { revalidate }
14+
}
15+
);
1216

13-
const body = await res.json();
17+
if (!res.ok) {
18+
const errorBody = await res.json();
19+
return NextResponse.json(errorBody, { status: res.status });
20+
}
1421

15-
if (!res.ok) {
16-
return NextResponse.json(body, { status: 404 });
22+
const body = await res.json();
23+
24+
return NextResponse.json(body, {
25+
headers: {
26+
'Cache-Control': 'public, max-age=60, stale-while-revalidate=3600',
27+
}
28+
});
29+
} catch (error) {
30+
console.error("Error fetching team data:", error);
31+
return NextResponse.json(
32+
{ error: "Failed to fetch team data" },
33+
{ status: 500 }
34+
);
1735
}
18-
19-
return NextResponse.json(body);
20-
}
36+
}

0 commit comments

Comments
 (0)