diff --git a/.changeset/clever-geese-clap.md b/.changeset/clever-geese-clap.md new file mode 100644 index 0000000000..95b7560d65 --- /dev/null +++ b/.changeset/clever-geese-clap.md @@ -0,0 +1,5 @@ +--- +"create-t3-app": patch +--- + +fix: non planetscale env.js refers to YOUR_MYSQL_URL_HERE diff --git a/cli/src/installers/envVars.ts b/cli/src/installers/envVars.ts index d85afc73eb..e27e4c5f4f 100644 --- a/cli/src/installers/envVars.ts +++ b/cli/src/installers/envVars.ts @@ -15,6 +15,7 @@ export const envVariablesInstaller: Installer = ({ const usingDrizzle = packages?.drizzle.inUse; const usingDb = usingPrisma || usingDrizzle; + const usingPlanetScale = databaseProvider === "planetscale"; const envContent = getEnvContent( !!usingAuth, @@ -25,9 +26,17 @@ export const envVariablesInstaller: Installer = ({ ); let envFile = ""; - if (usingAuth && usingDb) envFile = "with-auth-db.js"; - else if (usingAuth) envFile = "with-auth.js"; - else if (usingDb) envFile = "with-db.js"; + if (usingDb) { + if (usingPlanetScale) { + if (usingAuth) envFile = "with-auth-db-planetscale.js"; + else envFile = "with-db-planetscale.js"; + } else { + if (usingAuth) envFile = "with-auth-db.js"; + else envFile = "with-db.js"; + } + } else { + if (usingAuth) envFile = "with-auth.js"; + } if (envFile !== "") { const envSchemaSrc = path.join( diff --git a/cli/template/extras/src/env/with-auth-db-planetscale.js b/cli/template/extras/src/env/with-auth-db-planetscale.js new file mode 100644 index 0000000000..35a053ec5b --- /dev/null +++ b/cli/template/extras/src/env/with-auth-db-planetscale.js @@ -0,0 +1,66 @@ +import { createEnv } from "@t3-oss/env-nextjs"; +import { z } from "zod"; + +export const env = createEnv({ + /** + * Specify your server-side environment variables schema here. This way you can ensure the app + * isn't built with invalid env vars. + */ + server: { + DATABASE_URL: z + .string() + .url() + .refine( + (str) => !str.includes("YOUR_MYSQL_URL_HERE"), + "You forgot to change the default URL" + ), + NODE_ENV: z + .enum(["development", "test", "production"]) + .default("development"), + NEXTAUTH_SECRET: + process.env.NODE_ENV === "production" + ? z.string() + : z.string().optional(), + NEXTAUTH_URL: z.preprocess( + // This makes Vercel deployments not fail if you don't set NEXTAUTH_URL + // Since NextAuth.js automatically uses the VERCEL_URL if present. + (str) => process.env.VERCEL_URL ?? str, + // VERCEL_URL doesn't include `https` so it cant be validated as a URL + process.env.VERCEL ? z.string() : z.string().url() + ), + DISCORD_CLIENT_ID: z.string(), + DISCORD_CLIENT_SECRET: z.string(), + }, + + /** + * Specify your client-side environment variables schema here. This way you can ensure the app + * isn't built with invalid env vars. To expose them to the client, prefix them with + * `NEXT_PUBLIC_`. + */ + client: { + // NEXT_PUBLIC_CLIENTVAR: z.string(), + }, + + /** + * You can't destruct `process.env` as a regular object in the Next.js edge runtimes (e.g. + * middlewares) or client-side so we need to destruct manually. + */ + runtimeEnv: { + DATABASE_URL: process.env.DATABASE_URL, + NODE_ENV: process.env.NODE_ENV, + NEXTAUTH_SECRET: process.env.NEXTAUTH_SECRET, + NEXTAUTH_URL: process.env.NEXTAUTH_URL, + DISCORD_CLIENT_ID: process.env.DISCORD_CLIENT_ID, + DISCORD_CLIENT_SECRET: process.env.DISCORD_CLIENT_SECRET, + }, + /** + * Run `build` or `dev` with `SKIP_ENV_VALIDATION` to skip env validation. This is especially + * useful for Docker builds. + */ + skipValidation: !!process.env.SKIP_ENV_VALIDATION, + /** + * Makes it so that empty strings are treated as undefined. `SOME_VAR: z.string()` and + * `SOME_VAR=''` will throw an error. + */ + emptyStringAsUndefined: true, +}); diff --git a/cli/template/extras/src/env/with-auth-db.js b/cli/template/extras/src/env/with-auth-db.js index 35a053ec5b..2467d22d0f 100644 --- a/cli/template/extras/src/env/with-auth-db.js +++ b/cli/template/extras/src/env/with-auth-db.js @@ -7,13 +7,7 @@ export const env = createEnv({ * isn't built with invalid env vars. */ server: { - DATABASE_URL: z - .string() - .url() - .refine( - (str) => !str.includes("YOUR_MYSQL_URL_HERE"), - "You forgot to change the default URL" - ), + DATABASE_URL: z.string().url(), NODE_ENV: z .enum(["development", "test", "production"]) .default("development"), diff --git a/cli/template/extras/src/env/with-db-planetscale.js b/cli/template/extras/src/env/with-db-planetscale.js new file mode 100644 index 0000000000..f4fdfe8415 --- /dev/null +++ b/cli/template/extras/src/env/with-db-planetscale.js @@ -0,0 +1,50 @@ +import { createEnv } from "@t3-oss/env-nextjs"; +import { z } from "zod"; + +export const env = createEnv({ + /** + * Specify your server-side environment variables schema here. This way you can ensure the app + * isn't built with invalid env vars. + */ + server: { + DATABASE_URL: z + .string() + .url() + .refine( + (str) => !str.includes("YOUR_MYSQL_URL_HERE"), + "You forgot to change the default URL" + ), + NODE_ENV: z + .enum(["development", "test", "production"]) + .default("development"), + }, + + /** + * Specify your client-side environment variables schema here. This way you can ensure the app + * isn't built with invalid env vars. To expose them to the client, prefix them with + * `NEXT_PUBLIC_`. + */ + client: { + // NEXT_PUBLIC_CLIENTVAR: z.string(), + }, + + /** + * You can't destruct `process.env` as a regular object in the Next.js edge runtimes (e.g. + * middlewares) or client-side so we need to destruct manually. + */ + runtimeEnv: { + DATABASE_URL: process.env.DATABASE_URL, + NODE_ENV: process.env.NODE_ENV, + // NEXT_PUBLIC_CLIENTVAR: process.env.NEXT_PUBLIC_CLIENTVAR, + }, + /** + * Run `build` or `dev` with `SKIP_ENV_VALIDATION` to skip env validation. This is especially + * useful for Docker builds. + */ + skipValidation: !!process.env.SKIP_ENV_VALIDATION, + /** + * Makes it so that empty strings are treated as undefined. `SOME_VAR: z.string()` and + * `SOME_VAR=''` will throw an error. + */ + emptyStringAsUndefined: true, +}); diff --git a/cli/template/extras/src/env/with-db.js b/cli/template/extras/src/env/with-db.js index 1107bd0faa..6ca7f3e93a 100644 --- a/cli/template/extras/src/env/with-db.js +++ b/cli/template/extras/src/env/with-db.js @@ -7,13 +7,7 @@ export const env = createEnv({ * isn't built with invalid env vars. */ server: { - DATABASE_URL: z - .string() - .url() - .refine( - (str) => !str.includes("YOUR_MYSQL_URL_HERE"), - "You forgot to change the default URL" - ), + DATABASE_URL: z.string().url(), NODE_ENV: z .enum(["development", "test", "production"]) .default("development"), @@ -43,8 +37,8 @@ export const env = createEnv({ */ skipValidation: !!process.env.SKIP_ENV_VALIDATION, /** - * Makes it so that empty strings are treated as undefined. - * `SOME_VAR: z.string()` and `SOME_VAR=''` will throw an error. + * Makes it so that empty strings are treated as undefined. `SOME_VAR: z.string()` and + * `SOME_VAR=''` will throw an error. */ emptyStringAsUndefined: true, });