Xử lý seo cho react
- User
Xử lý seo cho react
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
Xử lý seo cho react
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
Để xử lý SEO cho ứng dụng React, bạn cần giải quyết các thách thức chính như rendering phía client, thiếu thẻ meta động, và tốc độ tải trang. Dưới đây là giải pháp toàn diện kết hợp các công cụ và kỹ thuật tốt nhất:
Next.js (SSR/SSG): Giải pháp tối ưu nhất
npx create-next-app@latest
export async function getServerSideProps(context) {
const data = await fetchData();
return { props: { data } };
}
export async function getStaticProps() {
const data = await fetchData();
return { props: { data } };
}
Gatsby (SSG): Phù hợp blog/website nội dung tĩnh
npm install -g gatsby-cli
gatsby new my-site
Next.js: Sử dụng built-in next/head
import Head from 'next/head';
export default function ProductPage({ product }) {
return (
<>
<Head>
<title>{product.name} | My Store</title>
<meta name="description" content={product.description} />
<meta property="og:image" content={product.image} />
<script type="application/ld+json">
{JSON.stringify({
"@context": "https://schema.org",
"@type": "Product",
"name": product.name,
"image": product.image
})}
</script>
</Head>
{/* Page content */}
</>
);
}
React Helmet (cho CRA hoặc custom setups):
npm install react-helmet-async
import { Helmet } from 'react-helmet-async';
function SEOComponent() {
return (
<Helmet>
<title>Dynamic Title</title>
<link rel="canonical" href="https://example.com/page" />
</Helmet>
);
}
Code Splitting:
import React, { lazy, Suspense } from 'react';
const LazyComponent = lazy(() => import('./LazyComponent'));
function MyPage() {
return (
<Suspense fallback={<div>Loading...</div>}>
<LazyComponent />
</Suspense>
);
}
Image Optimization (Next.js):
import Image from 'next/image';
<Image src="/product.jpg" alt="Product" width={500} height={500} priority />
Preloading & Prefetching:
// Next.js automatic prefetching for links
import Link from 'next/link';
<Link href="/about" prefetch={false}>About</Link>
Next.js File-based Routing:
pages/
├── index.js
├── about.js
└── products/
├── [id].js // Dynamic route
Avoid Hash Routing: Sử dụng browser history
// React Router setup
import { BrowserRouter as Router } from 'react-router-dom';
Sitemap & Robots.txt:
next-sitemap
npm install next-sitemap
Tạo next-sitemap.config.js và thêm script vào package.jsonGoogle Search Console:
OpenGraph & Twitter Cards:
<Head>
<meta property="og:type" content="website" />
<meta name="twitter:card" content="summary_large_image" />
</Head>
Công cụ kiểm tra:
Kiểm tra render:
curl http://localhost:3000 > output.html
Kiểm tra HTML output có chứa đầy đủ nội dung
Nếu không thể dùng SSR/SSG:
# Ví dụ cấu hình Nginx
location / {
if ($http_user_agent ~* (googlebot|bingbot)) {
proxy_pass http://rendertron-server;
}
# ... normal app
}
// pages/products/[id].js
export async function getStaticPaths() {
const paths = await getAllProductIds();
return { paths, fallback: 'blocking' };
}
export async function getStaticProps({ params }) {
const product = await getProductData(params.id);
return { props: { product }, revalidate: 3600 }; // ISR: Cập nhật mỗi giờ
}
export default function Product({ product }) {
return (
<>
<Head>
<title>{product.name} | Store</title>
<meta name="description" content={product.metaDesc} />
</Head>
<ProductDetail product={product} />
</>
);
}
Bằng cách kết hợp Next.js/SSR với các kỹ thuật trên, bạn có thể đạt được:
[image: SEO React Workflow] open
Luôn kiểm tra kết quả bằng Google Rich Results Test và Search Console sau triển khai!