Clone Website Astro → Astro
Clone Website Astro → Astro
Section titled “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.
Phase 1: Phân tích Website gốc
Section titled “Phase 1: Phân tích Website gốc”Bước 1: Xác định công nghệ
Section titled “Bước 1: Xác định công nghệ”# Kiểm tra xem site có phải Astro không# Mở DevTools (F12) → Console → Gõ:__ASTRO__
# Nếu trả về object → là Astro siteKiểm tra trong DevTools Network tab:
- Có file
.astrođược serve không? - HTML có chứa
data-astro-*attributes không?
Bước 2: Phân tích cấu trúc
Section titled “Bước 2: Phân tích cấu trúc”Dùng DevTools Elements để:
- Chọn từng section
- Copy CSS classes
- Ghi lại CSS variables nếu có
Phase 2: Setup Project mới
Section titled “Phase 2: Setup Project mới”Khởi tạo Astro project
Section titled “Khởi tạo Astro project”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ự buildCài dependencies cần thiết
Section titled “Cài dependencies cần thiết”cd my-clone
# Nếu site gốc dùng Starlightnpm install @astrojs/starlight
# Nếu có Svelte componentsnpm install svelte @astrojs/svelte
# Nếu có React componentsnpm install react react-dom @astrojs/react
# Nếu dùng Tailwindnpm install tailwindcss @astrojs/tailwindnpx tailwindcss initPhase 3: Copy Assets
Section titled “Phase 3: Copy Assets”Copy hình ảnh và fonts
Section titled “Copy hình ảnh và fonts”# Tạo folder public/mkdir -p public/imagesmkdir -p public/fonts
# Copy từ site gốc (download hoặc từ repo nếu open source)# - Favicon# - Logo# - Hero images# - Font filesPhase 4: Copy CSS
Section titled “Phase 4: Copy CSS”Trích xuất CSS từ site gốc
Section titled “Trích xuất CSS từ site gốc”Cách 1: Từ DevTools
1. DevTools → Sources → Page2. Tìm các file .css3. Copy contentCách 2: Từ source (nếu open source)
git clone https://github.com/original/site.gitcd site# Copy từ src/styles/Tổ chức CSS trong project mới
Section titled “Tổ chức CSS trong project mới”/* 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 { /* ... */ }Phase 5: Copy Components
Section titled “Phase 5: Copy Components”Astro Components
Section titled “Astro Components”Cấu trúc file .astro:
---// Frontmatter: JavaScript/TypeScriptconst { 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
---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>Phase 6: Copy Pages
Section titled “Phase 6: Copy Pages”Layout chính
Section titled “Layout chính”---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>Trang content
Section titled “Trang content”---import Layout from '../layouts/Layout.astro';---
<Layout title="Home"> <section class="hero"> <h1>Welcome to My Site</h1> <p>Description...</p> </section></Layout>Phase 7: Config và Testing
Section titled “Phase 7: Config và Testing”astro.config.mjs
Section titled “astro.config.mjs”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'], }), ],});Test checklist
Section titled “Test checklist”- 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