# 메타데이터

Next.js에서는 메타데이터(Metadata)를 설정하는 두 가지 방법이 있습니다. 설정 기반 메타데이터(Config-based Metadata)와 파일 기반 메타데이터(File-based Metadata)가 그 방법입니다. 이번 강의에서는 설정 기반 메타데이터를 중심으로 다룹니다.

설정 기반 메타데이터 방식에는 정적 방식(Static Metadata)과 동적 방식(Dynamic Metadata)이 있습니다.

### 정적 메타데이터

정적 방식은 Metadata 객체를 정의하는 방식입니다.

{% code title="layout.tsx 혹은 page.tsx" %}

```tsx
import type { Metadata } from 'next'
 
export const metadata: Metadata = {
  title: '...',
  description: '...',
}
 
export default function Page() {}
```

{% endcode %}

### 동적 메타데이터

동적 방식은 `generateMetadata`라는 함수를 이용해 동적으로 Metadata 객체를 생성하는 방식입니다.

{% code title="app/blog/\[id]/page.tsx" %}

```tsx
import type { Metadata, ResolvingMetadata } from 'next'
 
type Props = {
  params: { id: string }
  searchParams: { [key: string]: string | string[] | undefined }
}
 
export async function generateMetadata(
  { params, searchParams }: Props,
  parent?: ResolvingMetadata
): Promise<Metadata> {
  const id = params.id
  
  const post = await fetch(`https://.../${id}`).then((res) => res.json())
  
  const previousImages = (await parent).openGraph?.images || []
 
  return {
    title: post.title,
    openGraph: {
      images: ['/some-specific-page-image.jpg', ...previousImages],
    },
  }
}
 
export default function Page({ params, searchParams }: Props) {}
```

{% endcode %}

#### 참고 자료

* <https://nextjs.org/docs/app/building-your-application/optimizing/metadata>


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://book.hajoeun.dev/friendly-next-js/next.js-1/undefined-5.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
