# ติดตั้ง SEONuxt CDN บน React (Vite / CRA)
> ติดตั้งใน index.html ของโปรเจกต์ React + Vite เพื่อให้ทำงานทันทีที่โหลดหน้าเว็บ

- **URL:** https://seonuxt.com/docs/lang-react
- **Markdown:** https://seonuxt.com/docs/lang-react.md
- **Category:** React SPA
- **SEO Importance Score:** 95/100
- **Updated:** 2026-11-12

วิธีติดตั้ง SEONuxt CDN ในโปรเจกต์ React SPA (Vite หรือ Create React App) ผ่านไฟล์ index.html

## ภาพรวมและประเด็นสำคัญ (Overview)

สำหรับ React SPA ที่สร้างด้วย Vite หรือ Create React App สามารถวาง `<script defer __seonuxt="tracking" ...>` ใน `<head>` และ `<seonuxt_main>` ท้าย `<body>` ของไฟล์ `index.html` (หรือ `public/index.html` สำหรับ CRA) ได้โดยตรง

### สรุปประเด็นสำคัญที่ต้องรู้ (Key Takeaways)

1. Vite: แก้ไขไฟล์ `index.html` ที่ Root ของโปรเจกต์
2. Create React App: แก้ไขไฟล์ `public/index.html`
3. ช่วยให้ SPA มี Meta Tags, Open Graph และ JSON-LD ครบถ้วนบน DOM

## รายละเอียดและข้อกำหนด (2 หัวข้อ)

### 1. ติดตั้ง Global Site Tag (seon.js) ใน index.html (Root ของ Vite)
*โหลดแบบ defer ไม่บล็อกการเรนเดอร์หน้าเว็บ*
- **Impact:** Critical | **Category:** Head Integration

วางแท็ก <script defer __seonuxt="tracking" ...> เป็นรายการแรกใน <head> ของ React (Vite / CRA) เพื่อเริ่มดาวน์โหลดสคริปต์ทันทีโดยไม่กระทบ Core Web Vitals

**จุดเด่นและรายละเอียด:**
- อัปเดต Meta Tags, Open Graph, Twitter Card และ Canonical อัตโนมัติ
- ฉีด Structured Data (JSON-LD) เข้า <head> โดยไม่ต้องเขียนเองทุกหน้า
- สลับโหมดพัฒนาด้วย attribute `developers` และนำออกเมื่อขึ้น Production

**คำแนะนำ SEONuxt:** ตรวจสอบให้แน่ใจว่าใส่ `__seonuxt="tracking"` และ `data-tracking` ตรงตามพร็อพเพอร์ตี้ของคุณ
**Tags:** React, Vite, SPA, index.html

### 2. วาง Container Tag (<seonuxt_main>) ท้าย <body>
*ตำแหน่งใต้ </footer> ก่อนปิด </body>*
- **Impact:** Critical | **Category:** Body Container

เพิ่ม <seonuxt_main section="seonuxt" data-tracking="YOUR-TRACKING-CODE"></seonuxt_main> ท้ายสุดของ Layout หลักใน React (Vite / CRA) เพื่อรองรับ Dynamic Content Container

**จุดเด่นและรายละเอียด:**
- รหัส data-tracking จับคู่กับสคริปต์ใน <head> อัตโนมัติ
- ไม่รบกวน CSS หรือโครงสร้าง Layout เดิมของเว็บไซต์

**คำแนะนำ SEONuxt:** วางไว้ใต้ </footer> ก่อนปิดแท็ก </body> ใน Layout หลักที่ใช้ร่วมกันทุกหน้า
**Tags:** seonuxt_main, React, Vite, SPA, index.html

## แนวทางปฏิบัติที่ดี vs ข้อควรเลี่ยง (Do & Don't)

### ข้อแนะนำที่ 1
- **ควรทำ (Do):** ติดตั้งใน Master Layout (index.html (Root ของ Vite)) เพียงจุดเดียวเพื่อให้ครอบคลุมทุกหน้าของ React (Vite / CRA)
- **ควรหลีกเลี่ยง (Don't):** คัดลอกสคริปต์ไปวางซ้ำหลายไฟล์ หรือลืมลบ attribute `developers` ก่อน Deploy Production
- **เหตุผล:** การวางใน Master Layout ช่วยลดการดูแลซ้ำซ้อนและป้องกันสคริปต์ทำงานซ้ำสองรอบในหน้าเดียว

## ตัวอย่างโค้ดและโครงสร้าง (Code Examples)

### index.html (React + Vite)

```html
<!doctype html>
<html lang="th">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>React App</title>
    <script
      defer
      __seonuxt="tracking"
      src="https://cdn.seonuxt.com/seon/tracking.js"
      data-tracking="YOUR-TRACKING-CODE"
      data-api="https://cdn.seonuxt.com"
    ></script>
  </head>
  <body>
    <div id="root"></div>
    <script type="module" src="/src/main.tsx"></script>
    <seonuxt_main section="seonuxt" data-tracking="YOUR-TRACKING-CODE"></seonuxt_main>
  </body>
</html>
```

> วาง <script defer> ใน <head> และ <seonuxt_main> หลัง <div id="root"></div> ก่อนปิด </body>

## คำถามที่พบบ่อย (FAQs)

### Q: จะตรวจสอบได้อย่างไรว่า SEONuxt CDN ทำงานบน React (Vite / CRA) แล้ว?

กด Hard Refresh (Cmd+Shift+R / Ctrl+F5) แล้วเปิด DevTools → แท็บ Elements เพื่อดู Meta Tags / JSON-LD ใน <head> (ห้ามดูจาก View Page Source) และพิมพ์ `seonuxt.help()` ในแท็บ Console
