mirror of
https://github.com/modrinth/code.git
synced 2026-08-27 18:14:49 +00:00
frontend deployment changes & stop prerendering (#7227)
* feat: frontend docker image * build: tweak gc, disable prerendering * feat: pass along UA * fix: fallback to process.env * build: remove this pr branch test * fix: don't pin docker image ver
This commit is contained in:
@@ -3,7 +3,10 @@
|
||||
* This composable is kept for legacy code that hasn't been migrated yet.
|
||||
*/
|
||||
|
||||
import { useVisitorUserAgent } from '~/composables/visitor-user-agent.ts'
|
||||
import { withLabrinthCanaryHeader } from '~/helpers/canary.ts'
|
||||
import { readEnv } from '~/helpers/env.ts'
|
||||
import { getFrontendUserAgent, VISITOR_USER_AGENT_HEADER } from '~/helpers/user-agent.ts'
|
||||
|
||||
let cachedRateLimitKey = undefined
|
||||
let rateLimitKeyPromise = undefined
|
||||
@@ -13,15 +16,7 @@ async function getRateLimitKey(config) {
|
||||
if (cachedRateLimitKey !== undefined) return cachedRateLimitKey
|
||||
|
||||
if (!rateLimitKeyPromise) {
|
||||
rateLimitKeyPromise = (async () => {
|
||||
try {
|
||||
const mod = 'cloudflare:workers'
|
||||
const { env } = await import(/* @vite-ignore */ mod)
|
||||
return await env.RATE_LIMIT_IGNORE_KEY?.get()
|
||||
} catch {
|
||||
return undefined
|
||||
}
|
||||
})()
|
||||
rateLimitKeyPromise = readEnv('RATE_LIMIT_IGNORE_KEY')
|
||||
}
|
||||
|
||||
cachedRateLimitKey = await rateLimitKeyPromise
|
||||
@@ -40,6 +35,12 @@ export const useBaseFetch = async (url, options = {}, skipAuth = false) => {
|
||||
|
||||
if (import.meta.server) {
|
||||
options.headers['x-ratelimit-key'] = await getRateLimitKey(config)
|
||||
options.headers['User-Agent'] = getFrontendUserAgent(config.public.hash)
|
||||
|
||||
const visitorUserAgent = useVisitorUserAgent()
|
||||
if (visitorUserAgent) {
|
||||
options.headers[VISITOR_USER_AGENT_HEADER] = visitorUserAgent
|
||||
}
|
||||
}
|
||||
|
||||
if (!skipAuth) {
|
||||
|
||||
@@ -0,0 +1,9 @@
|
||||
import { tryUseNuxtApp } from '#imports'
|
||||
|
||||
// Nuxt's `useRequestHeaders` throws outside of a setup context, and `useBaseFetch` is
|
||||
// called from plenty of places that no longer have one.
|
||||
export function useVisitorUserAgent(): string | undefined {
|
||||
if (!import.meta.server) return undefined
|
||||
|
||||
return tryUseNuxtApp()?.ssrContext?.event?.node?.req?.headers['user-agent']
|
||||
}
|
||||
@@ -14,18 +14,10 @@ import {
|
||||
import type { Ref } from 'vue'
|
||||
|
||||
import { useFeatureFlags } from '~/composables/featureFlags.ts'
|
||||
import { useVisitorUserAgent } from '~/composables/visitor-user-agent.ts'
|
||||
import { withStagingArchonBaseUrl } from '~/helpers/archon.ts'
|
||||
|
||||
async function getRateLimitKeyFromSecretsStore(): Promise<string | undefined> {
|
||||
try {
|
||||
const mod = 'cloudflare:workers'
|
||||
const { env } = await import(/* @vite-ignore */ mod)
|
||||
return await env.RATE_LIMIT_IGNORE_KEY?.get()
|
||||
} catch {
|
||||
// Not running in Cloudflare Workers environment
|
||||
return undefined
|
||||
}
|
||||
}
|
||||
import { readEnv } from '~/helpers/env.ts'
|
||||
import { getFrontendUserAgent, VISITOR_USER_AGENT_HEADER } from '~/helpers/user-agent.ts'
|
||||
|
||||
export function createModrinthClient(
|
||||
auth: Ref<{ token: string | undefined }>,
|
||||
@@ -33,10 +25,12 @@ export function createModrinthClient(
|
||||
apiBaseUrl: string
|
||||
archonBaseUrl: string
|
||||
sharedInstancesBaseUrl: string
|
||||
commitHash: string
|
||||
rateLimitKey?: string
|
||||
},
|
||||
): NuxtModrinthClient {
|
||||
const flags = useFeatureFlags()
|
||||
const visitorUserAgent = useVisitorUserAgent()
|
||||
const optionalFeatures = [
|
||||
import.meta.dev ? (new VerboseLoggingFeature() as AbstractFeature) : undefined,
|
||||
].filter(Boolean) as AbstractFeature[]
|
||||
@@ -46,8 +40,10 @@ export function createModrinthClient(
|
||||
archonBaseUrl: () =>
|
||||
withStagingArchonBaseUrl(config.archonBaseUrl, flags.value.archonApiStaging),
|
||||
sharedInstancesBaseUrl: config.sharedInstancesBaseUrl,
|
||||
userAgent: () => (import.meta.server ? getFrontendUserAgent(config.commitHash) : undefined),
|
||||
headers: visitorUserAgent ? { [VISITOR_USER_AGENT_HEADER]: visitorUserAgent } : undefined,
|
||||
archonSentryCapture: () => flags.value.archonSentryCapture,
|
||||
rateLimitKey: config.rateLimitKey || getRateLimitKeyFromSecretsStore,
|
||||
rateLimitKey: config.rateLimitKey || (() => readEnv('RATE_LIMIT_IGNORE_KEY')),
|
||||
features: [
|
||||
// for modrinth hosting
|
||||
// is skipped for normal reqs
|
||||
|
||||
@@ -0,0 +1,22 @@
|
||||
type SecretsStoreBinding = { get: () => Promise<string> }
|
||||
|
||||
// Cloudflare exposes plain vars and Secrets Store bindings on the Workers env. Every
|
||||
// other runtime (the Docker image, local dev) only has process.env.
|
||||
async function getWorkersEnv(): Promise<Record<string, unknown> | undefined> {
|
||||
try {
|
||||
const mod = 'cloudflare:workers'
|
||||
const { env } = await import(/* @vite-ignore */ mod)
|
||||
return env as Record<string, unknown>
|
||||
} catch {
|
||||
return undefined
|
||||
}
|
||||
}
|
||||
|
||||
export async function readEnv(name: string): Promise<string | undefined> {
|
||||
const binding = (await getWorkersEnv())?.[name]
|
||||
|
||||
if (typeof binding === 'string') return binding
|
||||
if (binding) return await (binding as SecretsStoreBinding).get()
|
||||
|
||||
return globalThis.process?.env?.[name]
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
export const VISITOR_USER_AGENT_HEADER = 'X-Forwarded-User-Agent'
|
||||
|
||||
export function getFrontendUserAgent(commitHash: string): string {
|
||||
return `modrinth/frontend/${commitHash || 'unknown'} (support@modrinth.com)`
|
||||
}
|
||||
@@ -8,6 +8,7 @@ export function setupModrinthClientProvider(auth: Awaited<ReturnType<typeof useA
|
||||
apiBaseUrl: config.public.apiBaseUrl.replace('/v2/', '/'),
|
||||
archonBaseUrl: config.public.pyroBaseUrl.replace('/v2/', '/'),
|
||||
sharedInstancesBaseUrl: config.public.sharedInstancesBaseUrl,
|
||||
commitHash: config.public.hash,
|
||||
rateLimitKey: config.rateLimitKey,
|
||||
})
|
||||
provideModrinthClient(client)
|
||||
|
||||
@@ -1,20 +1,20 @@
|
||||
export default defineEventHandler(async (event) => {
|
||||
try {
|
||||
const mod = 'cloudflare:workers'
|
||||
const { env } = await import(/* @vite-ignore */ mod)
|
||||
const cfEnv = env as any
|
||||
const config = useRuntimeConfig(event)
|
||||
import { readEnv } from '~/helpers/env'
|
||||
|
||||
if (cfEnv.CF_PAGES_URL) config.public.siteUrl = cfEnv.CF_PAGES_URL
|
||||
if (cfEnv.BROWSER_BASE_URL) config.public.apiBaseUrl = cfEnv.BROWSER_BASE_URL
|
||||
if (cfEnv.BASE_URL) config.apiBaseUrl = cfEnv.BASE_URL
|
||||
if (cfEnv.PYRO_BASE_URL) {
|
||||
config.public.pyroBaseUrl = cfEnv.PYRO_BASE_URL
|
||||
config.pyroBaseUrl = cfEnv.PYRO_BASE_URL
|
||||
}
|
||||
if (cfEnv.STRIPE_PUBLISHABLE_KEY)
|
||||
config.public.stripePublishableKey = cfEnv.STRIPE_PUBLISHABLE_KEY
|
||||
} catch {
|
||||
/* empty */
|
||||
export default defineEventHandler(async (event) => {
|
||||
const config = useRuntimeConfig(event)
|
||||
|
||||
const siteUrl = await readEnv('CF_PAGES_URL')
|
||||
const browserBaseUrl = await readEnv('BROWSER_BASE_URL')
|
||||
const baseUrl = await readEnv('BASE_URL')
|
||||
const pyroBaseUrl = await readEnv('PYRO_BASE_URL')
|
||||
const stripePublishableKey = await readEnv('STRIPE_PUBLISHABLE_KEY')
|
||||
|
||||
if (siteUrl) config.public.siteUrl = siteUrl
|
||||
if (browserBaseUrl) config.public.apiBaseUrl = browserBaseUrl
|
||||
if (baseUrl) config.apiBaseUrl = baseUrl
|
||||
if (pyroBaseUrl) {
|
||||
config.public.pyroBaseUrl = pyroBaseUrl
|
||||
config.pyroBaseUrl = pyroBaseUrl
|
||||
}
|
||||
if (stripePublishableKey) config.public.stripePublishableKey = stripePublishableKey
|
||||
})
|
||||
|
||||
@@ -1,23 +1,13 @@
|
||||
import { type Labrinth, ModrinthApiError } from '@modrinth/api-client'
|
||||
import { SignJWT } from 'jose'
|
||||
|
||||
import { readEnv } from '~/helpers/env'
|
||||
import { useServerModrinthClient } from '~/server/utils/api-client'
|
||||
|
||||
type IntercomTokenResponse = {
|
||||
token: string
|
||||
}
|
||||
|
||||
async function getIntercomKeyFromSecretsStore(): Promise<string | undefined> {
|
||||
try {
|
||||
const mod = 'cloudflare:workers'
|
||||
const { env } = await import(/* @vite-ignore */ mod)
|
||||
return await env.INTERCOM_IDENTITY_SECRET?.get()
|
||||
} catch {
|
||||
// Not running in Cloudflare Workers environment
|
||||
return undefined
|
||||
}
|
||||
}
|
||||
|
||||
async function signIntercomUserJwt(
|
||||
user: { id: string; username: string; email?: string; created: string },
|
||||
secret: string,
|
||||
@@ -72,7 +62,7 @@ export default defineEventHandler(async (event): Promise<IntercomTokenResponse>
|
||||
setHeader(event, 'cache-control', 'private, no-store, max-age=0')
|
||||
|
||||
const intercomSecret =
|
||||
(await getIntercomKeyFromSecretsStore()) ?? useRuntimeConfig(event).intercomIdentitySecret
|
||||
(await readEnv('INTERCOM_IDENTITY_SECRET')) ?? useRuntimeConfig(event).intercomIdentitySecret
|
||||
|
||||
if (!intercomSecret) {
|
||||
throw createError({
|
||||
|
||||
@@ -5,17 +5,10 @@ import {
|
||||
type NuxtClientConfig,
|
||||
NuxtModrinthClient,
|
||||
} from '@modrinth/api-client'
|
||||
import type { H3Event } from 'h3'
|
||||
import { getRequestHeader, type H3Event } from 'h3'
|
||||
|
||||
async function getRateLimitKeyFromSecretsStore(): Promise<string | undefined> {
|
||||
try {
|
||||
const mod = 'cloudflare:workers'
|
||||
const { env } = await import(/* @vite-ignore */ mod)
|
||||
return await env.RATE_LIMIT_IGNORE_KEY?.get()
|
||||
} catch {
|
||||
return undefined
|
||||
}
|
||||
}
|
||||
import { readEnv } from '~/helpers/env'
|
||||
import { getFrontendUserAgent, VISITOR_USER_AGENT_HEADER } from '~/helpers/user-agent'
|
||||
|
||||
export interface ServerModrinthClientOptions {
|
||||
event?: H3Event
|
||||
@@ -28,6 +21,10 @@ export function useServerModrinthClient(options?: ServerModrinthClientOptions):
|
||||
const sharedInstancesBaseUrl =
|
||||
config.sharedInstancesBaseUrl || config.public.sharedInstancesBaseUrl
|
||||
|
||||
const visitorUserAgent = options?.event
|
||||
? getRequestHeader(options.event, 'user-agent')
|
||||
: undefined
|
||||
|
||||
const features = []
|
||||
|
||||
if (options?.authToken) {
|
||||
@@ -42,7 +39,9 @@ export function useServerModrinthClient(options?: ServerModrinthClientOptions):
|
||||
const clientConfig: NuxtClientConfig = {
|
||||
labrinthBaseUrl: apiBaseUrl,
|
||||
sharedInstancesBaseUrl,
|
||||
rateLimitKey: config.rateLimitKey || getRateLimitKeyFromSecretsStore,
|
||||
userAgent: getFrontendUserAgent(config.public.hash),
|
||||
headers: visitorUserAgent ? { [VISITOR_USER_AGENT_HEADER]: visitorUserAgent } : undefined,
|
||||
rateLimitKey: config.rateLimitKey || (() => readEnv('RATE_LIMIT_IGNORE_KEY')),
|
||||
features,
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user