Skip to content

Commit d25f94a

Browse files
committed
feat: pass along UA
1 parent 0f171da commit d25f94a

6 files changed

Lines changed: 38 additions & 1 deletion

File tree

‎apps/frontend/src/composables/fetch.js‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@
33
* This composable is kept for legacy code that hasn't been migrated yet.
44
*/
55

6+
import { useVisitorUserAgent } from '~/composables/visitor-user-agent.ts'
67
import { withLabrinthCanaryHeader } from '~/helpers/canary.ts'
8+
import { getFrontendUserAgent, VISITOR_USER_AGENT_HEADER } from '~/helpers/user-agent.ts'
79

810
let cachedRateLimitKey = undefined
911
let rateLimitKeyPromise = undefined
@@ -40,6 +42,12 @@ export const useBaseFetch = async (url, options = {}, skipAuth = false) => {
4042

4143
if (import.meta.server) {
4244
options.headers['x-ratelimit-key'] = await getRateLimitKey(config)
45+
options.headers['User-Agent'] = getFrontendUserAgent(config.public.hash)
46+
47+
const visitorUserAgent = useVisitorUserAgent()
48+
if (visitorUserAgent) {
49+
options.headers[VISITOR_USER_AGENT_HEADER] = visitorUserAgent
50+
}
4351
}
4452

4553
if (!skipAuth) {
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import { tryUseNuxtApp } from '#imports'
2+
3+
// Nuxt's `useRequestHeaders` throws outside of a setup context, and `useBaseFetch` is
4+
// called from plenty of places that no longer have one.
5+
export function useVisitorUserAgent(): string | undefined {
6+
if (!import.meta.server) return undefined
7+
8+
return tryUseNuxtApp()?.ssrContext?.event?.node?.req?.headers['user-agent']
9+
}

‎apps/frontend/src/helpers/api.ts‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@ import {
1414
import type { Ref } from 'vue'
1515

1616
import { useFeatureFlags } from '~/composables/featureFlags.ts'
17+
import { useVisitorUserAgent } from '~/composables/visitor-user-agent.ts'
1718
import { withStagingArchonBaseUrl } from '~/helpers/archon.ts'
19+
import { getFrontendUserAgent, VISITOR_USER_AGENT_HEADER } from '~/helpers/user-agent.ts'
1820

1921
async function getRateLimitKeyFromSecretsStore(): Promise<string | undefined> {
2022
try {
@@ -33,10 +35,12 @@ export function createModrinthClient(
3335
apiBaseUrl: string
3436
archonBaseUrl: string
3537
sharedInstancesBaseUrl: string
38+
commitHash: string
3639
rateLimitKey?: string
3740
},
3841
): NuxtModrinthClient {
3942
const flags = useFeatureFlags()
43+
const visitorUserAgent = useVisitorUserAgent()
4044
const optionalFeatures = [
4145
import.meta.dev ? (new VerboseLoggingFeature() as AbstractFeature) : undefined,
4246
].filter(Boolean) as AbstractFeature[]
@@ -46,6 +50,8 @@ export function createModrinthClient(
4650
archonBaseUrl: () =>
4751
withStagingArchonBaseUrl(config.archonBaseUrl, flags.value.archonApiStaging),
4852
sharedInstancesBaseUrl: config.sharedInstancesBaseUrl,
53+
userAgent: () => (import.meta.server ? getFrontendUserAgent(config.commitHash) : undefined),
54+
headers: visitorUserAgent ? { [VISITOR_USER_AGENT_HEADER]: visitorUserAgent } : undefined,
4955
archonSentryCapture: () => flags.value.archonSentryCapture,
5056
rateLimitKey: config.rateLimitKey || getRateLimitKeyFromSecretsStore,
5157
features: [
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
export const VISITOR_USER_AGENT_HEADER = 'X-Forwarded-User-Agent'
2+
3+
export function getFrontendUserAgent(commitHash: string): string {
4+
return `modrinth/frontend/${commitHash || 'unknown'} (support@modrinth.com)`
5+
}

‎apps/frontend/src/providers/setup/modrinth-client.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ export function setupModrinthClientProvider(auth: Awaited<ReturnType<typeof useA
88
apiBaseUrl: config.public.apiBaseUrl.replace('/v2/', '/'),
99
archonBaseUrl: config.public.pyroBaseUrl.replace('/v2/', '/'),
1010
sharedInstancesBaseUrl: config.public.sharedInstancesBaseUrl,
11+
commitHash: config.public.hash,
1112
rateLimitKey: config.rateLimitKey,
1213
})
1314
provideModrinthClient(client)

‎apps/frontend/src/server/utils/api-client.ts‎

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@ import {
55
type NuxtClientConfig,
66
NuxtModrinthClient,
77
} from '@modrinth/api-client'
8-
import type { H3Event } from 'h3'
8+
import { getRequestHeader, type H3Event } from 'h3'
9+
10+
import { getFrontendUserAgent, VISITOR_USER_AGENT_HEADER } from '~/helpers/user-agent'
911

1012
async function getRateLimitKeyFromSecretsStore(): Promise<string | undefined> {
1113
try {
@@ -28,6 +30,10 @@ export function useServerModrinthClient(options?: ServerModrinthClientOptions):
2830
const sharedInstancesBaseUrl =
2931
config.sharedInstancesBaseUrl || config.public.sharedInstancesBaseUrl
3032

33+
const visitorUserAgent = options?.event
34+
? getRequestHeader(options.event, 'user-agent')
35+
: undefined
36+
3137
const features = []
3238

3339
if (options?.authToken) {
@@ -42,6 +48,8 @@ export function useServerModrinthClient(options?: ServerModrinthClientOptions):
4248
const clientConfig: NuxtClientConfig = {
4349
labrinthBaseUrl: apiBaseUrl,
4450
sharedInstancesBaseUrl,
51+
userAgent: getFrontendUserAgent(config.public.hash),
52+
headers: visitorUserAgent ? { [VISITOR_USER_AGENT_HEADER]: visitorUserAgent } : undefined,
4553
rateLimitKey: config.rateLimitKey || getRateLimitKeyFromSecretsStore,
4654
features,
4755
}

0 commit comments

Comments
 (0)