Skip to content

Clone Website Astro → Astro

Hướng dẫn này áp dụng khi bạn muốn clone/copy một website Astro (như Starlight docs, Astro blog template) để tùy chỉnh cho riêng mình.

Terminal window
# Kiểm tra xem site có phải Astro không
# Mở DevTools (F12) → Console → Gõ:
__ASTRO__
# Nếu trả về object → là Astro site

Kiểm tra trong DevTools Network tab:

  • Có file .astro được serve không?
  • HTML có chứa data-astro-* attributes không?

Dùng DevTools Elements để:

  1. Chọn từng section
  2. Copy CSS classes
  3. Ghi lại CSS variables nếu có
Terminal window
npm create astro@latest my-clone
# Chọn template phù hợp:
# - "A few best practices" (giống site gốc nhất)
# - Hoặc "Empty" nếu muốn tự build
Terminal window
cd my-clone
# Nếu site gốc dùng Starlight
npm install @astrojs/starlight
# Nếu có Svelte components
npm install svelte @astrojs/svelte
# Nếu có React components
npm install react react-dom @astrojs/react
# Nếu dùng Tailwind
npm install tailwindcss @astrojs/tailwind
npx tailwindcss init
Terminal window
# Tạo folder public/
mkdir -p public/images
mkdir -p public/fonts
# Copy từ site gốc (download hoặc từ repo nếu open source)
# - Favicon
# - Logo
# - Hero images
# - Font files

Cách 1: Từ DevTools

1. DevTools → Sources → Page
2. Tìm các file .css
3. Copy content

Cách 2: Từ source (nếu open source)

Terminal window
git clone https://github.com/original/site.git
cd site
# Copy từ src/styles/
src/styles/global.css
/* 1. CSS Variables (Design Tokens) */
:root {
/* Colors từ site gốc */
--color-primary: #e11d48;
--color-secondary: #0ea5e9;
/* Typography */
--font-sans: 'Inter', sans-serif;
--font-mono: 'Fira Code', monospace;
/* Spacing */
--space-1: 0.25rem;
--space-2: 0.5rem;
--space-4: 1rem;
--space-8: 2rem;
}
/* 2. Reset/Base */
*, *::before, *::after {
box-sizing: border-box;
}
/* 3. Layout styles */
.navbar { /* ... */ }
.sidebar { /* ... */ }
.footer { /* ... */ }
/* 4. Component styles */
.btn { /* ... */ }
.card { /* ... */ }

Cấu trúc file .astro:

---
// Frontmatter: JavaScript/TypeScript
const { title } = Astro.props;
---
<!-- Template: HTML với expressions -->
<div class="card">
<h2>{title}</h2>
<slot />
</div>
<style>
/* Scoped CSS */
.card {
border: 1px solid #ccc;
padding: 1rem;
}
</style>

Ví dụ: Clone Navigation component

src/components/Navigation.astro
---
const navItems = [
{ label: 'Home', href: '/' },
{ label: 'About', href: '/about' },
{ label: 'Blog', href: '/blog' },
];
---
<nav class="navbar">
<div class="logo">
<a href="/">Logo</a>
</div>
<ul class="nav-links">
{navItems.map(item => (
<li>
<a href={item.href}>{item.label}</a>
</li>
))}
</ul>
</nav>
<style>
.navbar {
display: flex;
justify-content: space-between;
padding: 1rem 2rem;
}
.nav-links {
display: flex;
gap: 2rem;
list-style: none;
}
a {
text-decoration: none;
color: var(--color-base-content);
font-size: 1rem; /* ← Quan trọng! */
}
</style>
src/layouts/Layout.astro
---
const { title } = Astro.props;
---
<!DOCTYPE html>
<html lang="vi">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width" />
<title>{title}</title>
<link rel="stylesheet" href="/styles/global.css" />
</head>
<body>
<Navigation />
<main>
<slot />
</main>
<Footer />
</body>
</html>
src/pages/index.astro
---
import Layout from '../layouts/Layout.astro';
---
<Layout title="Home">
<section class="hero">
<h1>Welcome to My Site</h1>
<p>Description...</p>
</section>
</Layout>
import { defineConfig } from 'astro/config';
import starlight from '@astrojs/starlight';
export default defineConfig({
site: 'https://your-site.com',
integrations: [
starlight({
title: 'My Docs',
customCss: ['./src/styles/custom.css'],
}),
],
});
  • Build thành công (npm run build)
  • Không có lỗi console
  • Responsive hoạt động
  • Dark mode (nếu có)
  • Links hoạt động
  • Images load đúng