Skip to content

Commit

Permalink
Seo 관련 작업 (#50)
Browse files Browse the repository at this point in the history
* feat: 구글 애널리틱스 추가

* feat: 사이트맵 작업

* feat: robots 파일 추가

* feat: metadata 작업
title, description 추가
og-image 추가

* chore: package 이름 변경

* chore: env 변경

* fix: 힌트 한글 엔터 입력시 2번 호출되는 에러 수정
nativeEvent.isComposing 이벤트 추가
  • Loading branch information
jgjgill authored Dec 28, 2023
1 parent cb07dd6 commit 55ef0ac
Show file tree
Hide file tree
Showing 12 changed files with 143 additions and 4 deletions.
3 changes: 3 additions & 0 deletions .env.local.example
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,6 @@ NEXT_PUBLIC_SUPABASE_URL=your-project-url
NEXT_PUBLIC_SUPABASE_ANON_KEY=your-anon-key
NEXT_PUBLIC_CLIENT_ID=oauth-rest-api-key
NEXT_PUBLIC_CLIENT_SECRET=client-secret-code
NEXT_PUBLIC_GOOGLE_ID = google-analytics
NEXT_PUBLIC_PROD_URL=prod-url
NEXT_PUBLIC_BASE_URL=base-url
22 changes: 22 additions & 0 deletions app/api/quizzes/[id]/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { cookies } from 'next/headers';
import { createClient } from '@/utils/supabase/server';
import { NextRequest, NextResponse } from 'next/server';

export async function GET(
request: NextRequest,
{ params }: { params: { id: string } }
) {
const { id } = params;

const cookieStore = cookies();
const supabase = createClient(cookieStore);

const { data: quiz } = await supabase
.from('quizzes')
.select('*')
.eq('id', id)
.limit(1)
.single();

return NextResponse.json({ quiz });
}
12 changes: 12 additions & 0 deletions app/api/quizzes/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { cookies } from 'next/headers';
import { createClient } from '@/utils/supabase/server';
import { NextResponse } from 'next/server';

export async function GET() {
const cookieStore = cookies();
const supabase = createClient(cookieStore);

const { data: quizzes } = await supabase.from('quizzes').select('*');

return NextResponse.json({ quizzes });
}
31 changes: 31 additions & 0 deletions app/google-analytics.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
'use client';

import Script from 'next/script';
import * as gtag from '../gtag.js';

const GoogleAnalytics = () => {
return (
<>
<Script
strategy="afterInteractive"
src={`https://www.googletagmanager.com/gtag/js?id=${gtag.GA_TRACKING_ID}`}
/>
<Script
id="gtag-init"
strategy="afterInteractive"
dangerouslySetInnerHTML={{
__html: `
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', '${gtag.GA_TRACKING_ID}', {
page_path: window.location.pathname,
});
`,
}}
/>
</>
);
};

export default GoogleAnalytics;
2 changes: 2 additions & 0 deletions app/layout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import './globals.css';
import QueryProvider from '@/providers/query-provider';
import { PcScreen } from '@/components/pc-screen';
import OverlayProvider from '@/providers/overlay-provider';
import GoogleAnalytics from './google-analytics';

const defaultUrl = process.env.VERCEL_URL
? `https://${process.env.VERCEL_URL}`
Expand Down Expand Up @@ -33,6 +34,7 @@ export default function RootLayout({
</div>
</body>
</QueryProvider>
<GoogleAnalytics />
</html>
);
}
Binary file added app/opengraph-image.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 0 additions & 3 deletions app/quizzes/[id]/answers/layout.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import BackHeader from '@/components/common/headers/back-header';
import React from 'react';

type LayoutProps = {
Expand All @@ -8,8 +7,6 @@ type LayoutProps = {
export default function Layout({ children }: LayoutProps) {
return (
<>
<BackHeader />

<section className="p-4">{children}</section>
</>
);
Expand Down
31 changes: 31 additions & 0 deletions app/quizzes/sitemap.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { type MetadataRoute } from 'next';

async function getQuizzes(): Promise<
Database['public']['Tables']['quizzes']['Row'][]
> {
const res = await fetch(`${process.env.NEXT_PUBLIC_BASE_URL}/api/quizzes`);

const { quizzes } = await res.json();

return quizzes;
}

export async function generateSitemaps() {
const quizzes = await getQuizzes();

return quizzes.map((quiz) => ({
id: quiz.id,
}));
}
export default async function sitemap({
id,
}: {
id: number;
}): Promise<MetadataRoute.Sitemap> {
const quizzes = await getQuizzes();

return quizzes.map((quiz) => ({
url: `${process.env.NEXT_PUBLIC_PROD_URL}/quizzes/${id}`,
lastModified: quiz.updated_at,
}));
}
2 changes: 1 addition & 1 deletion app/quizzes/writer/_components/hints.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ export default function Hints({ form }: HintsProps) {
placeholder="힌트를 입력해주세요"
maxLength={formLiteral.hints.item.max.value}
onKeyDown={(e) => {
if (e.key === 'Enter') {
if (e.key === 'Enter' && !e.nativeEvent.isComposing) {
e.preventDefault();
addHint();
}
Expand Down
12 changes: 12 additions & 0 deletions app/robots.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { MetadataRoute } from 'next';

export default function robots(): MetadataRoute.Robots {
return {
rules: {
userAgent: '*',
allow: '/',
disallow: '/private/',
},
sitemap: `${process.env.NEXT_PUBLIC_PROD_URL}/sitemap.xml`,
};
}
14 changes: 14 additions & 0 deletions app/sitemap.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { MetadataRoute } from 'next';

const PROD_URL = process.env.NEXT_PUBLIC_PROD_URL;

export default function sitemap(): MetadataRoute.Sitemap {
return [
{
url: `${PROD_URL}`,
lastModified: new Date(),
changeFrequency: 'yearly',
priority: 1,
},
];
}
15 changes: 15 additions & 0 deletions gtag.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
export const GA_TRACKING_ID = process.env.NEXT_PUBLIC_GOOGLE_ID;

export const pageview = (url) => {
window.gtag('config', GA_TRACKING_ID, {
page_path: url,
});
};

export const event = ({ action, category, label, value }) => {
window.gtag('event', action, {
event_category: category,
event_label: label,
value: value,
});
};

0 comments on commit 55ef0ac

Please sign in to comment.