SparMate・
TL;DR
- 各 Vercel プロジェクトに
NEXT_PUBLIC_GENRE=sparmate (または netapair/boardlink)を 1 つ設定するだけで配色・ テキスト・ 機能 フラグを 切り替える satisfies Record<Genre, GenreConfig>でジャンル 定義の 漏れを コンパイル 時に 検出できる NEXT_PUBLIC_*はビルド 時に バンドルへ 静的 インライン 展開されるため、 process形式の.env[dynamicKey] 動的 アクセスは 機能しない
if 文 30 件で気づいた設計のまずさ
BoardLink のif (genre === 'sparmate') ... else if (genre === 'netapair') ... が
これが
ジャンル設定ファイルを 1 枚に集約する
lib/genre.ts に
export type Genre = 'sparmate' | 'netapair' | 'boardlink'
export interface GenreConfig {
appName: string
primaryColor: string // Tailwind クラス(完全なクラス名で記述)
textColor: string
features: {
videoMatch: boolean // ビデオ通話マッチング
}
}
const genreConfig = {
sparmate: {
appName: 'SparMate',
primaryColor: 'bg-red-500',
textColor: 'text-red-600',
features: { videoMatch: true },
},
netapair: {
appName: 'NetaPair',
primaryColor: 'bg-yellow-500',
textColor: 'text-yellow-600',
features: { videoMatch: false },
},
boardlink: {
appName: 'BoardLink',
primaryColor: 'bg-blue-500',
textColor: 'text-blue-600',
features: { videoMatch: false },
},
} satisfies Record<Genre, GenreConfig>
const VALID_GENRES = Object.keys(genreConfig) as Genre[]
const raw = process.env.NEXT_PUBLIC_GENRE
export const genre: Genre = VALID_GENRES.includes(raw as Genre)
? (raw as Genre)
: 'sparmate'
export const config = genreConfig[genre]
satisfies Record<Genre, GenreConfig> がGenre 型にgenreConfig 側でもas const との
掲載時点videoMatch と、VideoCallButton は
Vercel への設定
各 Vercel プロジェクトの Settings → Environment Variables で
| プロジェクト | 変数名 | 値 |
|---|---|---|
| sparmate | NEXT_PUBLIC_GENRE | sparmate |
| netapair | NEXT_PUBLIC_GENRE | netapair |
| boardlink | NEXT_PUBLIC_GENRE | boardlink |
Environment の.env.local に NEXT_PUBLIC_GENRE= を.env.local は .gitignore に
コンポーネントでの使い方
import { config } from '@/lib/genre'
export function PrimaryButton({ children }: { children: React.ReactNode }) {
return (
<button className={`${config.primaryColor} text-white px-4 py-2 rounded`}>
{children}
</button>
)
}
機能config を
import { config } from '@/lib/genre'
export function MatchCard() {
return (
<div>
{/* ... */}
{config.features.videoMatch && <VideoCallButton />}
</div>
)
}
Tailwind での注意点
bg-${color}-500 のように文字列'bg-red-500')を
落とし穴: NEXT_PUBLIC_* の動的アクセスは機能しない
NEXT_PUBLIC_* のprocess というリテラルの
そのため、
// ❌ 動的アクセス → ビルド時に解決されず undefined になる
const key = 'NEXT_PUBLIC_GENRE'
const value = process.env[key]
// ✅ リテラルアクセス → ビルド時に文字列 'sparmate' などへ置き換えられる
const value = process.env.NEXT_PUBLIC_GENRE
私が.env.local がそのままファイルとしてundefined になる。
NEXT_PUBLIC_* は
まとめ
env var 1 つでsatisfies による
同一
参照:
SparMate — 格闘技の
NetaPair —
BoardLink — ボドゲ・
Cosoado Lab