Skip to content

Commit

Permalink
Merge pull request #116 from tatsutakein/feature/#113
Browse files Browse the repository at this point in the history
👍 型安全に環境変数を扱うよう変更
  • Loading branch information
tatsutakein authored Jun 29, 2023
2 parents ab0a9dd + 5105b6d commit b0391bd
Show file tree
Hide file tree
Showing 9 changed files with 77 additions and 16 deletions.
3 changes: 0 additions & 3 deletions apps/frontend/next.config.js

This file was deleted.

14 changes: 14 additions & 0 deletions apps/frontend/next.config.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// Importing env files here to validate on build
import './src/env.mjs';

/** @type {import('next').NextConfig} */
const config = {
reactStrictMode: true,
/** Enables hot reloading for local packages without a build step */
transpilePackages: [],
/** We already do linting and typechecking as separate tasks in CI */
eslint: { ignoreDuringBuilds: true },
typescript: { ignoreBuildErrors: true },
};

export default config;
4 changes: 3 additions & 1 deletion apps/frontend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
"@radix-ui/react-dropdown-menu": "^2.0.5",
"@radix-ui/react-icons": "^1.3.0",
"@tailwindcss/line-clamp": "0.4.4",
"@t3-oss/env-nextjs": "^0.6.0",
"@types/react-syntax-highlighter": "15.5.7",
"@vercel/analytics": "^1.0.1",
"clsx": "1.2.1",
Expand All @@ -39,7 +40,8 @@
"react-hook-form": "7.45.1",
"react-markdown": "8.0.7",
"react-syntax-highlighter": "15.5.0",
"recoil": "0.7.7"
"recoil": "0.7.7",
"zod": "^3.21.4"
},
"devDependencies": {
"@babel/core": "7.22.5",
Expand Down
8 changes: 4 additions & 4 deletions apps/frontend/src/components/Utils/GoogleAnalytics.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@
import { usePathname, useSearchParams } from 'next/navigation';
import Script from 'next/script';
import { useEffect } from 'react';
import { GA_TRACKING_ID, pageView } from '@/lib';
import { pageView } from '@/lib';
import { env } from '@/env.mjs';

const GoogleAnalytics: React.FC = () => {
const pathname = usePathname();
const searchParams = useSearchParams();

useEffect(() => {
if (!GA_TRACKING_ID) return;
const url = pathname + searchParams.toString();
pageView(url);
}, [pathname, searchParams]);
Expand All @@ -19,7 +19,7 @@ const GoogleAnalytics: React.FC = () => {
<>
<Script
strategy='lazyOnload'
src={`https://www.googletagmanager.com/gtag/js?id=${GA_TRACKING_ID}`}
src={`https://www.googletagmanager.com/gtag/js?id=${env.NEXT_PUBLIC_GOOGLE_ANALYTICS_ID}`}
/>
<Script
id='gtag-init'
Expand All @@ -29,7 +29,7 @@ const GoogleAnalytics: React.FC = () => {
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', '${GA_TRACKING_ID}', {
gtag('config', '${env.NEXT_PUBLIC_GOOGLE_ANALYTICS_ID}', {
page_path: window.location.pathname,
});
`}
Expand Down
27 changes: 27 additions & 0 deletions apps/frontend/src/env.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
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: {
NODE_ENV: z.string().min(1),
},
/**
* Specify your client-side environment variables schema here.
* For them to be exposed to the client, prefix them with `NEXT_PUBLIC_`.
*/
client: {
NEXT_PUBLIC_GOOGLE_ANALYTICS_ID: z.string().min(1),
},
/**
* Destructure all variables from `process.env` to make sure they aren't tree-shaken away.
*/
runtimeEnv: {
NODE_ENV: process.env.NODE_ENV,
NEXT_PUBLIC_GOOGLE_ANALYTICS_ID: process.env.NEXT_PUBLIC_GOOGLE_ANALYTICS_ID,
},
skipValidation: !!process.env.CI || !!process.env.SKIP_ENV_VALIDATION,
});
5 changes: 2 additions & 3 deletions apps/frontend/src/lib/analytics/gtag.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
'use client';

import { GA_TRACKING_ID } from '@/lib';
import { env } from '@/env.mjs';

// https://developers.google.com/analytics/devguides/collection/gtagjs/pages
export const pageView = (url: string) => {
if (!GA_TRACKING_ID) return;
window.gtag('config', GA_TRACKING_ID, {
window.gtag('config', env.NEXT_PUBLIC_GOOGLE_ANALYTICS_ID, {
page_path: url,
});
};
4 changes: 0 additions & 4 deletions apps/frontend/src/lib/constants.ts

This file was deleted.

1 change: 0 additions & 1 deletion apps/frontend/src/lib/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
export * from './analytics';
export * from './constants';
27 changes: 27 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 comment on commit b0391bd

@vercel
Copy link

@vercel vercel bot commented on b0391bd Jun 29, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.