From 9ccaa396dcf78c24aca1f976d2562ea0b1a387c2 Mon Sep 17 00:00:00 2001 From: Jordan Eldredge Date: Wed, 4 Jun 2025 22:24:29 -0700 Subject: [PATCH] Sitemap and cors for nextjs --- packages/skin-database/app/graphql/route.ts | 6 ++++ packages/skin-database/app/sitemap.ts | 34 +++++++++++++++++++++ packages/skin-database/server.ts | 3 ++ yarn.lock | 7 ++++- 4 files changed, 49 insertions(+), 1 deletion(-) create mode 100644 packages/skin-database/app/sitemap.ts diff --git a/packages/skin-database/app/graphql/route.ts b/packages/skin-database/app/graphql/route.ts index d1304836..1d6499fb 100644 --- a/packages/skin-database/app/graphql/route.ts +++ b/packages/skin-database/app/graphql/route.ts @@ -15,6 +15,12 @@ const { handleRequest } = createYoga({ fetchAPI: { Response }, graphiql: true, + + cors: { + origin: "*", // Allow all origins for simplicity, adjust as needed + methods: ["GET", "POST", "OPTIONS"], + credentials: true, + }, }); export { diff --git a/packages/skin-database/app/sitemap.ts b/packages/skin-database/app/sitemap.ts new file mode 100644 index 00000000..1ad56565 --- /dev/null +++ b/packages/skin-database/app/sitemap.ts @@ -0,0 +1,34 @@ +import type { MetadataRoute } from "next"; +import * as Skins from "../data/skins"; + +// Google's limit is 50,000 URLs per sitemap +const PAGE_SIZE = 50000; + +const BASE_URL = "https://skins.webamp.org"; + +export async function generateSitemaps(): Promise<{ id: number }[]> { + const count = await Skins.getClassicSkinCount(); + const maps: { id: number }[] = []; + for (let i = 0; i < Math.ceil(count / PAGE_SIZE); i++) { + maps.push({ id: i }); + } + return maps; +} + +export default async function sitemap({ + id, +}: { + id: string; +}): Promise { + const md5s = await Skins.getAllClassicSkins(); + const skinUrls = md5s.map(({ md5, fileName }) => `skin/${md5}/${fileName}`); + const urls = ["/about", "/", "/upload", ...skinUrls]; + const slice = urls.slice( + parseInt(id, 10) * PAGE_SIZE, + (parseInt(id, 10) + 1) * PAGE_SIZE + ); + return slice.map((url) => ({ + url: `${BASE_URL}${url}`, + changeFrequency: "monthly", + })); +} diff --git a/packages/skin-database/server.ts b/packages/skin-database/server.ts index 5d34faea..e75b5ff9 100644 --- a/packages/skin-database/server.ts +++ b/packages/skin-database/server.ts @@ -1,3 +1,6 @@ +import dotenv from "dotenv"; + +dotenv.config(); import { parse } from "url"; import next from "next"; import { createApp } from "./api/app"; diff --git a/yarn.lock b/yarn.lock index 2a264447..3d12d94f 100644 --- a/yarn.lock +++ b/yarn.lock @@ -15486,11 +15486,16 @@ graphql-yoga@^5.10.10: lru-cache "^10.0.0" tslib "^2.8.1" -graphql@16.8.1, graphql@^16.8.1, graphql@^16.9.0: +graphql@^16.8.1: version "16.8.1" resolved "https://registry.yarnpkg.com/graphql/-/graphql-16.8.1.tgz#1930a965bef1170603702acdb68aedd3f3cf6f07" integrity sha512-59LZHPdGZVh695Ud9lRzPBVTtlX9ZCV150Er2W43ro37wVof0ctenSaskPPjN7lVTIN8mSZt8PHUNKZuNQUuxw== +graphql@^16.9.0: + version "16.11.0" + resolved "https://registry.yarnpkg.com/graphql/-/graphql-16.11.0.tgz#96d17f66370678027fdf59b2d4c20b4efaa8a633" + integrity sha512-mS1lbMsxgQj6hge1XZ6p7GPhbrtFwUFYi3wRzXAC/FmYnyXMTvvI3td3rjmQ2u8ewXueaSvRPWaEcgVVOT9Jnw== + grats@^0.0.31: version "0.0.31" resolved "https://registry.yarnpkg.com/grats/-/grats-0.0.31.tgz#f094786584a0f2fb345b38e6c37330d3f4f391cd"