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:
Michael H.
2026-08-20 17:07:07 +00:00
committed by GitHub
parent f39a602be3
commit bee0c14b31
13 changed files with 221 additions and 80 deletions
+30
View File
@@ -0,0 +1,30 @@
# syntax=docker/dockerfile:1
FROM node:24-slim
LABEL org.opencontainers.image.source=https://github.com/modrinth/code
LABEL org.opencontainers.image.title=frontend
LABEL org.opencontainers.image.description="Modrinth website"
LABEL org.opencontainers.image.licenses=AGPL-3.0-only
RUN apt-get update \
&& apt-get install -y --no-install-recommends dumb-init \
&& rm -rf /var/lib/apt/lists/*
ENV NODE_ENV=production
ENV HOST=0.0.0.0
ENV PORT=3000
WORKDIR /app
# Nitro bundles every runtime dependency into .output, so no node_modules is needed.
COPY --chown=node:node .output ./.output
USER node
EXPOSE 3000
HEALTHCHECK --interval=30s --timeout=5s --start-period=20s --retries=3 \
CMD node -e "fetch('http://127.0.0.1:'+process.env.PORT+'/robots.txt').then(r=>process.exit(r.ok?0:1),()=>process.exit(1))"
ENTRYPOINT ["dumb-init", "--"]
CMD ["node", "/app/.output/server/index.mjs"]
+2
View File
@@ -0,0 +1,2 @@
*
!.output
+1 -19
View File
@@ -122,23 +122,6 @@ export default defineNuxtConfig({
},
},
hooks: {
async 'nitro:config'(nitroConfig) {
const emailTemplates = Object.keys(
await import('./src/templates/emails/index.ts').then((m) => m.default),
)
const docTemplates = Object.keys(
await import('./src/templates/docs/index.ts').then((m) => m.default),
)
nitroConfig.prerender = nitroConfig.prerender || {}
nitroConfig.prerender.routes = nitroConfig.prerender.routes || []
for (const template of emailTemplates) {
nitroConfig.prerender.routes.push(`/_internal/templates/email/${template}`)
}
for (const template of docTemplates) {
nitroConfig.prerender.routes.push(`/_internal/templates/doc/${template}`)
}
},
async 'build:before'() {
// 30 minutes
const TTL = 30 * 60 * 1000
@@ -290,6 +273,7 @@ export default defineNuxtConfig({
external: ['cloudflare:workers'],
},
preset: 'cloudflare_module',
noExternals: true,
cloudflare: {
nodeCompat: true,
},
@@ -325,14 +309,12 @@ export default defineNuxtConfig({
redirect: '/_internal/templates/email/**',
},
'/_internal/templates/email/**': {
prerender: true,
headers: {
'Content-Type': 'text/html',
'Cache-Control': 'public, max-age=3600',
},
},
'/_internal/templates/doc/**': {
prerender: true,
headers: {
'Content-Type': 'text/html',
'Cache-Control': 'public, max-age=3600',
+10 -9
View File
@@ -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']
}
+8 -12
View File
@@ -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
+22
View File
@@ -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]
}
+5
View File
@@ -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)
+17 -17
View File
@@ -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({
+10 -11
View File
@@ -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,
}