Skip to content

Commit 6fc3d18

Browse files
committed
pass user-agent to api
1 parent f8a1ba7 commit 6fc3d18

5 files changed

Lines changed: 60 additions & 18 deletions

File tree

app/api/graphql/route.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { type NextRequest, NextResponse } from 'next/server';
22

3+
import { ORIGINAL_USER_AGENT_HEADER } from '@/app/app.consts';
34
import { request } from '@/lib/graphql/request';
45
import { rejectNotAllowedGraphqlOperations } from '@/utils/reject-not-allowed-graphql-operations';
56
import { rejectWrongOrigin } from '@/utils/reject-wrong-origin';
@@ -16,7 +17,8 @@ export async function POST(req: NextRequest) {
1617
return NextResponse.json({ error: 'Forbidden' }, { status: 403 });
1718
}
1819

19-
const { data, status } = await request(query, variables);
20+
const originalUserAgent = req.headers.get(ORIGINAL_USER_AGENT_HEADER) ?? req.headers.get('user-agent') ?? undefined;
21+
const { data, status } = await request(query, variables, { originalUserAgent });
2022

2123
return NextResponse.json(data, { status });
2224
} catch {

app/app.consts.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,6 @@ export const RANK_DESCRIPTIONS = {
3838
},
3939
};
4040

41-
export const DEFAULT_LANGUAGE_COLOR = '#64748B';
42-
4341
export const LANGUAGE = {
4442
description:
4543
'See which programming languages are the most popular worldwide. Browse detailed summaries by stars and code size - and dive into rankings for each language, globally or by country.',
@@ -51,3 +49,6 @@ export const LANGUAGE = {
5149
'How many users with at least one public repo with 5+ stars have this language in their public repositories.',
5250
},
5351
};
52+
53+
export const DEFAULT_LANGUAGE_COLOR = '#64748B';
54+
export const ORIGINAL_USER_AGENT_HEADER = 'x-original-user-agent';

lib/graphql/graphql-client.ts

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
import { print } from 'graphql';
22

3+
import { ORIGINAL_USER_AGENT_HEADER } from '@/app/app.consts';
34
import type DocumentNode from '@/types/typed-document-node';
45

56
const resolveGraphqlProxyUrl = () => {
6-
if (typeof window !== 'undefined') {
7+
if (globalThis.window !== undefined) {
78
return '/api/graphql';
89
}
910

@@ -14,19 +15,40 @@ const resolveGraphqlProxyUrl = () => {
1415
throw new Error('Cannot resolve GraphQL proxy URL. Set NEXT_PUBLIC_URI.');
1516
};
1617

18+
const resolveOriginalUserAgent = async () => {
19+
if (globalThis.window !== undefined) {
20+
return null;
21+
}
22+
23+
try {
24+
const { headers } = await import('next/headers');
25+
const incomingHeaders = await headers();
26+
return incomingHeaders.get('user-agent');
27+
} catch {
28+
return null;
29+
}
30+
};
31+
1732
export async function graphqlClient<TData, TVariables>(
1833
document: DocumentNode<TData, TVariables>,
1934
variables?: TVariables,
2035
params?: RequestInit,
2136
): Promise<TData> {
2237
const query = print(document);
2338
const url = resolveGraphqlProxyUrl();
39+
const originalUserAgent = await resolveOriginalUserAgent();
40+
const requestHeaders = new Headers(params?.headers);
41+
42+
requestHeaders.set('Content-Type', 'application/json');
43+
if (originalUserAgent) {
44+
requestHeaders.set(ORIGINAL_USER_AGENT_HEADER, originalUserAgent);
45+
}
2446

2547
const res = await fetch(url, {
48+
...params,
2649
method: 'POST',
27-
headers: { 'Content-Type': 'application/json' },
50+
headers: requestHeaders,
2851
body: JSON.stringify({ query, variables }),
29-
...params,
3052
});
3153

3254
const rawBody = await res.text();

lib/graphql/request.ts

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,33 @@
11
'use server';
22

3+
import { ORIGINAL_USER_AGENT_HEADER } from '@/app/app.consts';
4+
35
import { signedFetch } from '../signed-fetch';
46

7+
type RequestOptions = {
8+
revalidate?: number;
9+
originalUserAgent?: string;
10+
};
11+
512
export async function request(
613
query: string,
714
variables?: Record<string, unknown>,
8-
params?: { revalidate?: number },
15+
options?: RequestOptions,
916
): Promise<{ data: unknown; status: number }> {
17+
const headers: Record<string, string> = {
18+
'Content-Type': 'application/json',
19+
'nextjs-build-phase': String(process.env.NEXT_PHASE === 'phase-production-build'),
20+
};
21+
22+
if (options?.originalUserAgent) {
23+
headers[ORIGINAL_USER_AGENT_HEADER] = options.originalUserAgent;
24+
}
25+
1026
const response = await signedFetch('/graphql', {
1127
method: 'POST',
12-
headers: {
13-
'Content-Type': 'application/json',
14-
// Skip rate limit if custom header is present
15-
'nextjs-build-phase': String(process.env.NEXT_PHASE === 'phase-production-build'),
16-
},
28+
headers,
1729
body: JSON.stringify({ query, variables }),
18-
next: { revalidate: params?.revalidate },
30+
next: { revalidate: options?.revalidate },
1931
});
2032

2133
if (!response.ok) {

public/robots.txt

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,14 @@
1-
User-agent: DataForSeoBot
2-
Crawl-delay: 60
3-
Disallow:
1+
User-agent: Googlebot
2+
Allow: /
43

4+
User-agent: Bingbot
5+
Allow: /
6+
7+
# Generic rule for everyone else
58
User-agent: *
6-
Disallow:
9+
Crawl-delay: 10
10+
Disallow: /api/
11+
Allow: /
712

813
Sitemap: https://gitranks.com/profile/_/sitemap.xml
9-
Sitemap: https://gitranks.com/by/_/sitemap.xml
14+
Sitemap: https://gitranks.com/by/_/sitemap.xml

0 commit comments

Comments
 (0)