Skip to content

Commit

Permalink
🧑‍💻 Automatically guess env URLs for Vercel preview deployments
Browse files Browse the repository at this point in the history
  • Loading branch information
baptisteArno committed Dec 5, 2023
1 parent 99c5aaf commit b6b2876
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 5 deletions.
21 changes: 21 additions & 0 deletions apps/builder/next.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,27 @@ const __filename = fileURLToPath(import.meta.url)

const __dirname = dirname(__filename)

const injectViewerUrlIfVercelPreview = (val) => {
if (
(val && typeof val === 'string' && val.length > 0) ||
process.env.VERCEL_ENV !== 'preview' ||
!process.env.VERCEL_BUILDER_PROJECT_NAME ||
!process.env.NEXT_PUBLIC_VERCEL_VIEWER_PROJECT_NAME
)
return
const isViewer = process.env.VERCEL_URL.includes(
process.env.NEXT_PUBLIC_VERCEL_VIEWER_PROJECT_NAME
)
process.env.NEXT_PUBLIC_VIEWER_URL = isViewer
? `https://${process.env.VERCEL_URL}`
: `https://${process.env.VERCEL_URL}`.replace(
process.env.VERCEL_BUILDER_PROJECT_NAME,
process.env.NEXT_PUBLIC_VERCEL_VIEWER_PROJECT_NAME
)
}

injectViewerUrlIfVercelPreview(process.env.NEXT_PUBLIC_VIEWER_URL)

configureRuntimeEnv()

/** @type {import('next').NextConfig} */
Expand Down
21 changes: 21 additions & 0 deletions apps/viewer/next.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,27 @@ const __filename = fileURLToPath(import.meta.url)

const __dirname = dirname(__filename)

const injectViewerUrlIfVercelPreview = (val) => {
if (
(val && typeof val === 'string' && val.length > 0) ||
process.env.VERCEL_ENV !== 'preview' ||
!process.env.VERCEL_BUILDER_PROJECT_NAME ||
!process.env.NEXT_PUBLIC_VERCEL_VIEWER_PROJECT_NAME
)
return
const isViewer = process.env.VERCEL_BRANCH_URL.includes(
process.env.NEXT_PUBLIC_VERCEL_VIEWER_PROJECT_NAME
)
process.env.NEXT_PUBLIC_VIEWER_URL = isViewer
? `https://${process.env.VERCEL_BRANCH_URL}`
: `https://${process.env.VERCEL_BRANCH_URL}`.replace(
process.env.VERCEL_BUILDER_PROJECT_NAME,
process.env.NEXT_PUBLIC_VERCEL_VIEWER_PROJECT_NAME
)
}

injectViewerUrlIfVercelPreview(process.env.NEXT_PUBLIC_VIEWER_URL)

configureRuntimeEnv()

const landingPagePaths = [
Expand Down
53 changes: 48 additions & 5 deletions packages/env/env.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,42 @@ declare const window: {
__ENV?: any
}

const guessNextAuthUrlForVercelPreview = (val: unknown) => {
if (
(val && typeof val === 'string' && val.length > 0) ||
process.env.VERCEL_ENV !== 'preview' ||
!process.env.VERCEL_BUILDER_PROJECT_NAME ||
!process.env.NEXT_PUBLIC_VERCEL_VIEWER_PROJECT_NAME
)
return val
const isBuilder = (process.env.VERCEL_URL as string).includes(
process.env.VERCEL_BUILDER_PROJECT_NAME
)
if (isBuilder) return `https://${process.env.VERCEL_URL}`
return `https://${process.env.VERCEL_URL}`.replace(
process.env.NEXT_PUBLIC_VERCEL_VIEWER_PROJECT_NAME,
process.env.VERCEL_BUILDER_PROJECT_NAME
)
}

const guessViewerUrlForVercelPreview = (val: unknown) => {
if (
(val && typeof val === 'string' && val.length > 0) ||
process.env.VERCEL_ENV !== 'preview' ||
!process.env.VERCEL_BUILDER_PROJECT_NAME ||
!process.env.NEXT_PUBLIC_VERCEL_VIEWER_PROJECT_NAME
)
return val
const isViewer = (process.env.VERCEL_URL as string).includes(
process.env.NEXT_PUBLIC_VERCEL_VIEWER_PROJECT_NAME
)
if (isViewer) return `https://${process.env.VERCEL_URL}`
return `https://${process.env.VERCEL_URL}`.replace(
process.env.VERCEL_BUILDER_PROJECT_NAME,
process.env.NEXT_PUBLIC_VERCEL_VIEWER_PROJECT_NAME
)
}

const boolean = z.enum(['true', 'false']).transform((value) => value === 'true')

const baseEnv = {
Expand All @@ -16,7 +52,10 @@ const baseEnv = {
.url()
.refine((url) => url.startsWith('postgres') || url.startsWith('mysql')),
ENCRYPTION_SECRET: z.string().length(32),
NEXTAUTH_URL: z.string().url(),
NEXTAUTH_URL: z.preprocess(
guessNextAuthUrlForVercelPreview,
z.string().url()
),
DISABLE_SIGNUP: boolean.optional().default('false'),
ADMIN_EMAIL: z.string().email().optional(),
DEFAULT_WORKSPACE_PLAN: z
Expand All @@ -29,10 +68,13 @@ const baseEnv = {
},
client: {
NEXT_PUBLIC_E2E_TEST: boolean.optional(),
NEXT_PUBLIC_VIEWER_URL: z
.string()
.min(1)
.transform((string) => string.split(',')),
NEXT_PUBLIC_VIEWER_URL: z.preprocess(
guessViewerUrlForVercelPreview,
z
.string()
.min(1)
.transform((val) => val.split(','))
),
NEXT_PUBLIC_ONBOARDING_TYPEBOT_ID: z.string().min(1).optional(),
NEXT_PUBLIC_BOT_FILE_UPLOAD_MAX_SIZE: z.coerce.number().optional(),
},
Expand Down Expand Up @@ -178,6 +220,7 @@ const vercelEnv = {
VERCEL_TOKEN: z.string().min(1).optional(),
VERCEL_TEAM_ID: z.string().min(1).optional(),
VERCEL_GIT_COMMIT_SHA: z.string().min(1).optional(),
VERCEL_BUILDER_PROJECT_NAME: z.string().min(1).optional(),
},
client: {
NEXT_PUBLIC_VERCEL_VIEWER_PROJECT_NAME: z.string().min(1).optional(),
Expand Down

0 comments on commit b6b2876

Please sign in to comment.