본문 바로가기

프로그래밍/Vue

[Nuxt3] 4. SEO를 위한 meta 태그 관리하기

728x90
반응형
SMALL

이번에 nuxt3를 도입하기로 결정한 가장 큰 이유는 SEO 문제를 해결하기 위해서이다

 

nuxt3 공식문서에 SEO를 설정하기 위한 3가지 방법을 제시하고 있다.

 

1. App Head

nuxt.config.ts 파일에 아래와 같이 설정을 하면 전역으로 설정이 가능하다

export default defineNuxtConfig({
  app: {
    head: {
      charset: 'utf-16',
      viewport: 'width=500, initial-scale=1',
      title: 'My App',
      meta: [
        // <meta name="description" content="My amazing site">
        { name: 'description', content: 'My amazing site.' }
      ],
    }
  }
})

공통된 항목은 nuxt.config.ts 파일에 설정 한뒤 각 페이지마다 동적으로 변경되는 meta 데이터를 수정해서 쓰면 될 것 같다

 

2. Composable: useHead

두번째 방법은 각 페이지에 설정 할수 있는 useHead 컴포저블 방식이다

각 페이지 별로 설정할 수도 있고 app.vue 에 설정하면 전역으로 설정이 된다

<script setup lang="ts">
useHead({
  title: 'My App',
  meta: [
    { name: 'description', content: 'My amazing site.' }
  ],
  bodyAttrs: {
    class: 'test'
  },
  script: [ { children: 'console.log(\'Hello world\')' } ]
})
</script>

SEO 설정에 필요한 페이지에 각각 위에 코드처럼 설정해서 사용 할 수 있다

 

3. Components

마지막 3번째 방법은 컴포넌트 방식이다

nuxt3는 <Title>,<Base>,<NoScript>,<Style>,<Meta>,<Link>,<Body>,<Html>,<Head> 이런 태그들을 제공해 주기 때문에 template 내에 직접 작성을 할 수도 있다

<script setup>
const title = ref('Hello World')
</script>

<template>
  <div>
    <Head>
      <Title>{{ title }}</Title>
      <Meta name="description" :content="title" />
      <Style type="text/css" children="body { background-color: green; }" />
    </Head>
    <h1>{{ title }}</h1>
  </div>
</template>

위에 코드처럼 template 안에서 직접 컨트롤을 할 수도 있다

 

각 상황에 맞는 방법을 활용하여 SEO 최적화를 진행하면 될 것 같다

728x90
반응형
LIST