Skip to content

Full Toolkit for Next-Level Domain Analysis

Centralize sponsor data and restrict image optimization allowlist 🛡️🖼️ (#93)

Felix Wotschofsky6f41d7ab3463
diff --git a/app/(landing)/_components/sponsors-section.tsx b/app/(landing)/_components/sponsors-section.tsx
index d3b6840..1f993bc 100644
--- a/app/(landing)/_components/sponsors-section.tsx
+++ b/app/(landing)/_components/sponsors-section.tsx
@@ -9,8 +9,7 @@ import {
DialogTrigger,
} from '@/components/ui/dialog';
-import { env } from '@/env';
-import { getGitHubSponsors } from '@/lib/github';
+import { getAllSponsors } from '@/lib/sponsors';
import { cn } from '@/lib/utils';
type SponsorsSectionProps = HTMLAttributes<HTMLElement>;
@@ -19,27 +18,7 @@ export const SponsorsSection: FC<SponsorsSectionProps> = async ({
className,
...props
}) => {
- const buildSponsorUrl = (baseUrl: string) => {
- try {
- const url = new URL(baseUrl);
- url.searchParams.set('ref', 'domain-digger');
- return url.toString();
- } catch (_) {
- return `https://${baseUrl}?ref=domain-digger`;
- }
- };
-
- const githubSponsors = await getGitHubSponsors('wotschofsky');
-
- const allSponsors = [
- ...(env.SPONSORS || []),
- ...githubSponsors.map((s) => ({
- id: s.login,
- name: s.name,
- logoUrl: s.avatarUrl,
- url: s.websiteUrl || s.url,
- })),
- ];
+ const allSponsors = await getAllSponsors();
if (!allSponsors.length) {
return null;
@@ -54,11 +33,7 @@ export const SponsorsSection: FC<SponsorsSectionProps> = async ({
<div className="mb-2 flex items-center justify-center gap-4">
{allSponsors.map((sponsor) => (
- <a
- key={sponsor.id}
- href={buildSponsorUrl(sponsor.url)}
- target="_blank"
- >
+ <a key={sponsor.id} href={sponsor.url} target="_blank">
<Image
width={48}
height={48}
diff --git a/lib/github.ts b/lib/github.ts
index 9d7aa1a..42c0903 100644
--- a/lib/github.ts
+++ b/lib/github.ts
@@ -2,110 +2,6 @@ import { log } from 'evlog';
import { env } from '@/env';
-type SponsorsQueryResponse = {
- data: {
- user: {
- sponsorshipsAsMaintainer: {
- nodes: {
- sponsorEntity: {
- login: string;
- name: string;
- url: string;
- avatarUrl: string;
- websiteUrl: string | null;
- };
- tier: {
- id: string;
- } | null;
- }[];
- };
- };
- };
-};
-
-export const getGitHubSponsors = async (username: string) => {
- if (!env.GITHUB_TOKEN) {
- log.warn({ message: 'github_token_missing', context: 'sponsors' });
- return [];
- }
-
- const response = await fetch('https://api.github.com/graphql', {
- method: 'POST',
- headers: {
- 'Content-Type': 'application/json',
- Authorization: `Bearer ${env.GITHUB_TOKEN}`,
- },
- body: JSON.stringify({
- query: `
- query SponsorsQuery($username: String!) {
- user(login: $username) {
- sponsorshipsAsMaintainer(first: 100) {
- nodes {
- sponsorEntity {
- ... on User {
- login
- name
- url
- avatarUrl
- websiteUrl
- }
- ... on Organization {
- login
- name
- url
- avatarUrl
- websiteUrl
- }
- }
- tier {
- id
- }
- }
- }
- }
- }
- `,
- variables: {
- username,
- },
- }),
- });
-
- if (!response.ok) {
- const body = await response.text();
- throw new Error(
- `Failed to fetch GitHub sponsors: ${response.status} ${response.statusText}\n${body}`,
- );
- }
-
- const body = (await response.json()) as SponsorsQueryResponse;
-
- const sponsors = body.data.user.sponsorshipsAsMaintainer.nodes.map((node) => {
- if (!node.tier) {
- throw new Error(
- 'Failed to fetch GitHub sponsors; access token might lack sufficient permissions',
- );
- }
-
- return {
- login: node.sponsorEntity.login,
- name: node.sponsorEntity.name,
- url: node.sponsorEntity.url,
- avatarUrl: node.sponsorEntity.avatarUrl,
- websiteUrl: node.sponsorEntity.websiteUrl,
- tierId: node.tier.id,
- };
- });
-
- const filteredSponsors = env.GITHUB_SPONSORS_FEATURED_TIERS
- ? sponsors.filter((sponsor) =>
- env.GITHUB_SPONSORS_FEATURED_TIERS!.includes(sponsor.tierId),
- )
- : sponsors;
-
- return filteredSponsors;
-};
-
type StargazerSummary = {
recentStargazers: Array<{
name: string;
diff --git a/lib/sponsors.ts b/lib/sponsors.ts
new file mode 100644
index 0000000..92c5075
--- /dev/null
+++ b/lib/sponsors.ts
@@ -0,0 +1,151 @@
+import { env } from '../env';
+
+export const GITHUB_SPONSOR_USERNAME = 'wotschofsky';
+
+export type Sponsor = {
+ id: string;
+ name: string;
+ logoUrl: string;
+ url: string;
+};
+
+type SponsorsQueryResponse = {
+ data: {
+ user: {
+ sponsorshipsAsMaintainer: {
+ nodes: {
+ sponsorEntity: {
+ login: string;
+ name: string;
+ url: string;
+ avatarUrl: string;
+ websiteUrl: string | null;
+ };
+ tier: {
+ id: string;
+ } | null;
+ }[];
+ };
+ };
+ };
+};
+
+const buildSponsorUrl = (baseUrl: string) => {
+ try {
+ const url = new URL(baseUrl);
+ url.searchParams.set('ref', 'domain-digger');
+ return url.toString();
+ } catch {
+ return `https://${baseUrl}?ref=domain-digger`;
+ }
+};
+
+const fetchGitHubSponsors = async () => {
+ if (!env.GITHUB_TOKEN) {
+ return [];
+ }
+
+ const response = await fetch('https://api.github.com/graphql', {
+ method: 'POST',
+ headers: {
+ 'Content-Type': 'application/json',
+ Authorization: `Bearer ${env.GITHUB_TOKEN}`,
+ },
+ body: JSON.stringify({
+ query: `
+ query SponsorsQuery($username: String!) {
+ user(login: $username) {
+ sponsorshipsAsMaintainer(first: 100) {
+ nodes {
+ sponsorEntity {
+ ... on User {
+ login
+ name
+ url
+ avatarUrl
+ websiteUrl
+ }
+ ... on Organization {
+ login
+ name
+ url
+ avatarUrl
+ websiteUrl
+ }
+ }
+ tier {
+ id
+ }
+ }
+ }
+ }
+ }
+ `,
+ variables: {
+ username: GITHUB_SPONSOR_USERNAME,
+ },
+ }),
+ });
+
+ if (!response.ok) {
+ const body = await response.text();
+ throw new Error(
+ `Failed to fetch GitHub sponsors: ${response.status} ${response.statusText}\n${body}`,
+ );
+ }
+
+ const body = (await response.json()) as SponsorsQueryResponse;
+
+ const sponsors = body.data.user.sponsorshipsAsMaintainer.nodes.map((node) => {
+ if (!node.tier) {
+ throw new Error(
+ 'Failed to fetch GitHub sponsors; access token might lack sufficient permissions',
+ );
+ }
+
+ return {
+ login: node.sponsorEntity.login,
+ name: node.sponsorEntity.name,
+ url: node.sponsorEntity.url,
+ avatarUrl: node.sponsorEntity.avatarUrl,
+ websiteUrl: node.sponsorEntity.websiteUrl,
+ tierId: node.tier.id,
+ };
+ });
+
+ return env.GITHUB_SPONSORS_FEATURED_TIERS
+ ? sponsors.filter((sponsor) =>
+ env.GITHUB_SPONSORS_FEATURED_TIERS!.includes(sponsor.tierId),
+ )
+ : sponsors;
+};
+
+export const getAllSponsors = async (): Promise<Sponsor[]> => {
+ const githubSponsors = await fetchGitHubSponsors();
+
+ const allSponsors = [
+ ...(env.SPONSORS ?? []),
+ ...githubSponsors.map((sponsor) => ({
+ id: sponsor.login,
+ name: sponsor.name,
+ logoUrl: sponsor.avatarUrl,
+ url: sponsor.websiteUrl || sponsor.url,
+ })),
+ ];
+
+ const sponsorsWithRef = allSponsors.map((sponsor) => ({
+ ...sponsor,
+ url: buildSponsorUrl(sponsor.url),
+ }));
+
+ return sponsorsWithRef;
+};
+
+export const getSponsorImageRemotePatterns = async () => {
+ const sponsors = await getAllSponsors();
+
+ return sponsors.map((sponsor) => {
+ const url = new URL(sponsor.logoUrl);
+ return { hostname: url.hostname, pathname: url.pathname };
+ });
+};
diff --git a/next.config.ts b/next.config.ts
index 4b33d3e..86d7a40 100644
--- a/next.config.ts
+++ b/next.config.ts
@@ -1,6 +1,8 @@
import type { NextConfig } from 'next';
-const nextConfig: NextConfig = {
+import { getSponsorImageRemotePatterns } from './lib/sponsors';
+
+const nextConfig = async (): Promise<NextConfig> => ({
reactStrictMode: false,
turbopack: {
rules: {
@@ -16,9 +18,7 @@ const nextConfig: NextConfig = {
hostname: 'static.wsky.dev',
pathname: '/branding/**',
},
- {
- hostname: 'avatars.githubusercontent.com',
- },
+ ...(await getSponsorImageRemotePatterns()),
],
formats: ['image/avif', 'image/webp'],
},
@@ -55,6 +55,6 @@ const nextConfig: NextConfig = {
// so they're matched literally instead of as a character class.
'/lookup/\\[domain\\]/subdomains': ['./bin/subfinder'],
},
-};
+});
export default nextConfig;