Skip to content

Commit 25da472

Browse files
committed
feat: move sitemaps generation in backend with python scripts
1 parent 7032d30 commit 25da472

File tree

12 files changed

+156
-562
lines changed

12 files changed

+156
-562
lines changed
Lines changed: 15 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,20 @@
1-
import { siteConfig } from "@/config/site";
2-
import { getSitemapMediaMovieCount } from "@/features/server/sitemap";
3-
import { buildSitemapIndex } from "@/lib/sitemap";
1+
import { supabaseAdmin } from "@/lib/supabase/supabase-admin";
42
import { NextResponse } from "next/server";
5-
import { gzipSync } from "zlib";
63

74
export async function GET() {
8-
try {
9-
const count = await getSitemapMediaMovieCount();
5+
const { data, error } = await supabaseAdmin.storage
6+
.from("sitemaps")
7+
.download("movies/index.xml.gz");
108

11-
const sitemapIndexes = Array.from({ length: count }, (_, index) => {
12-
return `${siteConfig.url}/sitemaps/films/${index}`;
13-
});
14-
15-
const sitemapIndexXML = buildSitemapIndex(sitemapIndexes);
16-
const gzipped = gzipSync(sitemapIndexXML);
17-
18-
return new NextResponse(Uint8Array.from(gzipped), {
19-
headers: {
20-
"Content-Type": "application/xml",
21-
"Content-Encoding": "gzip",
22-
"Content-Length": gzipped.length.toString(),
23-
"Cache-Control": "public, max-age=86400",
24-
},
25-
});
26-
} catch (error) {
27-
console.error("Error generating sitemap index:", error);
28-
return NextResponse.error();
9+
if (error || !data) {
10+
return new NextResponse("[sitemap] films index not found", { status: 404 });
2911
}
30-
}
12+
13+
return new NextResponse(data, {
14+
headers: {
15+
"Content-Type": "application/xml",
16+
"Content-Encoding": "gzip",
17+
"Cache-Control": "public, max-age=86400",
18+
},
19+
});
20+
}
Lines changed: 14 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,56 +1,22 @@
1-
import { siteConfig } from "@/config/site";
2-
import { getSitemapMediaMovies } from "@/features/server/sitemap";
3-
import { routing } from "@/lib/i18n/routing";
4-
import { buildSitemap } from "@/lib/sitemap";
5-
import { kebabCase } from "lodash";
1+
import { supabaseAdmin } from "@/lib/supabase/supabase-admin";
62
import { NextResponse } from "next/server";
7-
import { gzipSync } from 'zlib';
83

94
export async function GET(
105
_: Request,
116
{ params }: { params: Promise<{ id: string }> }
127
) {
13-
try {
14-
const { id } = await params;
15-
const films = await getSitemapMediaMovies(Number(id));
16-
const sitemapXML = buildSitemap(films.map((film) => {
17-
const translations = Object.fromEntries(
18-
film.tmdb_movie_translations.map((t) => [
19-
`${t.iso_639_1}-${t.iso_3166_1}`,
20-
t.title,
21-
])
22-
);
23-
24-
return {
25-
url: `${siteConfig.url}/film/${film.id}${film.original_title ? `-${kebabCase(film.original_title)}` : ''}`,
26-
priority: 0.8,
27-
alternates: {
28-
languages: Object.fromEntries(
29-
routing.locales.map((locale) => {
30-
const title = translations[locale] || film.original_title;
31-
const slug = `${film.id}${title ? `-${kebabCase(title)}` : ''}`;
32-
return [
33-
locale,
34-
`${siteConfig.url}/${locale}/film/${slug}`,
35-
];
36-
})
37-
),
38-
},
39-
};
40-
}));
41-
42-
const gzippedXML = gzipSync(sitemapXML);
43-
44-
return new NextResponse(Uint8Array.from(gzippedXML), {
45-
headers: {
46-
"Content-Type": "application/xml",
47-
"Content-Encoding": "gzip",
48-
"Content-Length": gzippedXML.length.toString(),
49-
"Cache-Control": "public, max-age=86400",
50-
},
51-
});
52-
} catch (error) {
53-
console.error("Error generating sitemap:", error);
54-
return NextResponse.error();
8+
const { id } = await params;
9+
const { data, error } = await supabaseAdmin.storage
10+
.from("sitemaps")
11+
.download(`movies/${id}.xml.gz`);
12+
if (error || !data) {
13+
return new NextResponse("[sitemap] films page not found", { status: 404 });
5514
}
15+
return new NextResponse(data, {
16+
headers: {
17+
"Content-Type": "application/xml",
18+
"Content-Encoding": "gzip",
19+
"Cache-Control": "public, max-age=86400",
20+
},
21+
});
5622
}
Lines changed: 14 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,20 @@
1-
import { siteConfig } from "@/config/site";
2-
import { getSitemapPlaylistCount } from "@/features/server/sitemap";
3-
import { buildSitemapIndex } from "@/lib/sitemap";
1+
import { supabaseAdmin } from "@/lib/supabase/supabase-admin";
42
import { NextResponse } from "next/server";
5-
import { gzipSync } from "zlib";
63

74
export async function GET() {
8-
try {
9-
const count = await getSitemapPlaylistCount();
5+
const { data, error } = await supabaseAdmin.storage
6+
.from("sitemaps")
7+
.download("playlists/index.xml.gz");
108

11-
const sitemapIndexes = Array.from({ length: count }, (_, index) => {
12-
return `${siteConfig.url}/sitemaps/playlists/${index}`;
13-
});
14-
15-
const sitemapIndexXML = buildSitemapIndex(sitemapIndexes);
16-
const gzipped = gzipSync(sitemapIndexXML);
17-
18-
return new NextResponse(Uint8Array.from(gzipped), {
19-
headers: {
20-
"Content-Type": "application/xml",
21-
"Content-Encoding": "gzip",
22-
"Content-Length": gzipped.length.toString(),
23-
"Cache-Control": "public, max-age=86400",
24-
},
25-
});
26-
} catch (error) {
27-
console.error("Error generating playlist sitemap index:", error);
28-
return NextResponse.error();
9+
if (error || !data) {
10+
return new NextResponse("[sitemap] playlists index not found", { status: 404 });
2911
}
12+
13+
return new NextResponse(data, {
14+
headers: {
15+
"Content-Type": "application/xml",
16+
"Content-Encoding": "gzip",
17+
"Cache-Control": "public, max-age=86400",
18+
},
19+
});
3020
}
Lines changed: 14 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,22 @@
1-
import { siteConfig } from "@/config/site";
2-
import { getSitemapPlaylists } from "@/features/server/sitemap";
3-
import { buildSitemap } from "@/lib/sitemap";
1+
import { supabaseAdmin } from "@/lib/supabase/supabase-admin";
42
import { NextResponse } from "next/server";
5-
import { gzipSync } from "zlib";
63

74
export async function GET(
85
_: Request,
96
{ params }: { params: Promise<{ id: string }> }
107
) {
11-
try {
12-
const { id } = await params;
13-
const playlists = await getSitemapPlaylists(Number(id));
14-
const sitemapXML = buildSitemap(playlists.map((playlist) => ({
15-
url: `${siteConfig.url}/playlist/${playlist.id}`,
16-
lastModified: playlist.updated_at
17-
? new Date(playlist.updated_at)
18-
: new Date(),
19-
changeFrequency: "daily",
20-
priority: 0.8,
21-
})));
22-
23-
const gzipped = gzipSync(sitemapXML);
24-
25-
return new NextResponse(Uint8Array.from(gzipped), {
26-
headers: {
27-
"Content-Type": "application/xml",
28-
"Content-Encoding": "gzip",
29-
"Content-Length": gzipped.length.toString(),
30-
"Cache-Control": "public, max-age=86400", // 24h cache
31-
},
32-
});
33-
} catch (error) {
34-
console.error("Error generating playlist sitemap:", error);
35-
return NextResponse.error();
8+
const { id } = await params;
9+
const { data, error } = await supabaseAdmin.storage
10+
.from("sitemaps")
11+
.download(`playlists/${id}.xml.gz`);
12+
if (error || !data) {
13+
return new NextResponse("[sitemap] playlists page not found", { status: 404 });
3614
}
15+
return new NextResponse(data, {
16+
headers: {
17+
"Content-Type": "application/xml",
18+
"Content-Encoding": "gzip",
19+
"Cache-Control": "public, max-age=86400",
20+
},
21+
});
3722
}
Lines changed: 14 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,20 @@
1-
import { siteConfig } from "@/config/site";
2-
import { getSitemapReviewMovieCount, getSitemapReviewTvSeriesCount } from "@/features/server/sitemap";
3-
import { buildSitemapIndex } from "@/lib/sitemap";
1+
import { supabaseAdmin } from "@/lib/supabase/supabase-admin";
42
import { NextResponse } from "next/server";
5-
import { gzipSync } from "zlib";
63

74
export async function GET() {
8-
try {
9-
const reviewMovieCount = await getSitemapReviewMovieCount();
10-
const reviewTvSeriesCount = await getSitemapReviewTvSeriesCount();
5+
const { data, error } = await supabaseAdmin.storage
6+
.from("sitemaps")
7+
.download("reviews/index.xml.gz");
118

12-
const sitemapMovieIndexes = Array.from({ length: reviewMovieCount }, (_, index) => {
13-
return `${siteConfig.url}/sitemaps/reviews/movie/${index}`;
14-
});
15-
16-
const sitemapTvSeriesIndexes = Array.from({ length: reviewTvSeriesCount }, (_, index) => {
17-
return `${siteConfig.url}/sitemaps/reviews/tv-series/${index}`;
18-
});
19-
20-
const sitemapIndexXML = buildSitemapIndex([...sitemapMovieIndexes, ...sitemapTvSeriesIndexes]);
21-
const gzipped = gzipSync(sitemapIndexXML);
22-
23-
return new NextResponse(Uint8Array.from(gzipped), {
24-
headers: {
25-
"Content-Type": "application/xml",
26-
"Content-Encoding": "gzip",
27-
"Content-Length": gzipped.length.toString(),
28-
"Cache-Control": "public, max-age=86400",
29-
},
30-
});
31-
} catch (error) {
32-
console.error("Error generating review sitemap index:", error);
33-
return NextResponse.error();
9+
if (error || !data) {
10+
return new NextResponse("[sitemap] reviews index not found", { status: 404 });
3411
}
12+
13+
return new NextResponse(data, {
14+
headers: {
15+
"Content-Type": "application/xml",
16+
"Content-Encoding": "gzip",
17+
"Cache-Control": "public, max-age=86400",
18+
},
19+
});
3520
}
Lines changed: 14 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,22 @@
1-
import { siteConfig } from "@/config/site";
2-
import { getSitemapReviewsMovie } from "@/features/server/sitemap";
3-
import { buildSitemap } from "@/lib/sitemap";
1+
import { supabaseAdmin } from "@/lib/supabase/supabase-admin";
42
import { NextResponse } from "next/server";
5-
import { gzipSync } from "zlib";
63

74
export async function GET(
85
_: Request,
96
{ params }: { params: Promise<{ id: string }> }
107
) {
11-
try {
12-
const { id } = await params;
13-
const reviews = await getSitemapReviewsMovie(Number(id));
14-
15-
const sitemapXML = buildSitemap(reviews.map((review) => ({
16-
url: `${siteConfig.url}/film/${review.activity.movie_id}/review/${review.id}`,
17-
lastModified: review.updated_at
18-
? new Date(review.updated_at)
19-
: new Date(),
20-
changeFrequency: "daily",
21-
priority: 0.8,
22-
})));
23-
24-
const gzipped = gzipSync(sitemapXML);
25-
26-
return new NextResponse(Uint8Array.from(gzipped), {
27-
headers: {
28-
"Content-Type": "application/xml",
29-
"Content-Encoding": "gzip",
30-
"Content-Length": gzipped.length.toString(),
31-
"Cache-Control": "public, max-age=86400", // 24h
32-
},
33-
});
34-
} catch (error) {
35-
console.error("Error generating review sitemap:", error);
36-
return NextResponse.error();
8+
const { id } = await params;
9+
const { data, error } = await supabaseAdmin.storage
10+
.from("sitemaps")
11+
.download(`reviews/movie/${id}.xml.gz`);
12+
if (error || !data) {
13+
return new NextResponse("[sitemap] reviews movie page not found", { status: 404 });
3714
}
15+
return new NextResponse(data, {
16+
headers: {
17+
"Content-Type": "application/xml",
18+
"Content-Encoding": "gzip",
19+
"Cache-Control": "public, max-age=86400",
20+
},
21+
});
3822
}
Lines changed: 14 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,22 @@
1-
import { siteConfig } from "@/config/site";
2-
import { getSitemapReviewsTvSeries } from "@/features/server/sitemap";
3-
import { buildSitemap } from "@/lib/sitemap";
1+
import { supabaseAdmin } from "@/lib/supabase/supabase-admin";
42
import { NextResponse } from "next/server";
5-
import { gzipSync } from "zlib";
63

74
export async function GET(
85
_: Request,
96
{ params }: { params: Promise<{ id: string }> }
107
) {
11-
try {
12-
const { id } = await params;
13-
const reviews = await getSitemapReviewsTvSeries(Number(id));
14-
const sitemapXML = buildSitemap(reviews.map((review) => ({
15-
url: `${siteConfig.url}/tv-series/${review.activity.tv_series_id}/review/${review.id}`,
16-
lastModified: review.updated_at
17-
? new Date(review.updated_at)
18-
: new Date(),
19-
changeFrequency: "daily",
20-
priority: 0.8,
21-
})));
22-
23-
const gzipped = gzipSync(sitemapXML);
24-
25-
return new NextResponse(Uint8Array.from(gzipped), {
26-
headers: {
27-
"Content-Type": "application/xml",
28-
"Content-Encoding": "gzip",
29-
"Content-Length": gzipped.length.toString(),
30-
"Cache-Control": "public, max-age=86400", // 24h
31-
},
32-
});
33-
} catch (error) {
34-
console.error("Error generating review sitemap:", error);
35-
return NextResponse.error();
8+
const { id } = await params;
9+
const { data, error } = await supabaseAdmin.storage
10+
.from("sitemaps")
11+
.download(`reviews/tv-series/${id}.xml.gz`);
12+
if (error || !data) {
13+
return new NextResponse("[sitemap] reviews tv-series page not found", { status: 404 });
3614
}
15+
return new NextResponse(data, {
16+
headers: {
17+
"Content-Type": "application/xml",
18+
"Content-Encoding": "gzip",
19+
"Cache-Control": "public, max-age=86400",
20+
},
21+
});
3722
}

0 commit comments

Comments
 (0)