sohaibuddinsyed/astro-basic

โ˜… 0Forks 1AstroGitHub โ†—Compare

Project website โ†—

README

๐Ÿš€ Astro Demo Application

A comprehensive demonstration of Astro's features including Static Site Generation (SSG), Server-Side Rendering (SSR), React/Svelte integration, content collections, image optimization, and more.

๐Ÿ“‹ Features Demonstrated

This application showcases all major Astro features through dedicated demo pages:

๐Ÿ—๏ธ Rendering Modes

  • Static Pre-rendered Pages - Build-time generation with export const prerender = true
  • SSG (Static Site Generation) - Default Astro behavior with build-time data fetching
  • SSR (Server-Side Rendering) - Runtime generation with export const prerender = false
  • Hybrid Mode - Mix of SSG and SSR in the same application

โš›๏ธ Framework Integration

  • React Components - Interactive components with client-side hydration
  • Svelte Components - Compiled components with built-in reactivity
  • Client-side JavaScript - Vanilla JavaScript for simple interactions

๐Ÿ”— Dynamic Routing

  • SSG Dynamic Routes - Pre-generated routes using getStaticPaths()
  • SSR Dynamic Routes - Runtime route handling with server-side processing

๐Ÿ–ผ๏ธ Image Optimization

  • Internal Images - Astro's built-in Image component with automatic optimization
  • External Images - Best practices for optimizing external images

๐Ÿ“š Content Management

  • Content Collections - Type-safe content management with schema validation
  • Blog System - Complete blog implementation with dynamic routes

๐Ÿšจ Error Handling

  • 404 Page - Custom not found page
  • 500 Page - Custom server error page

๐Ÿ—‚๏ธ Project Structure

/
โ”œโ”€โ”€ public/
โ”‚   โ”œโ”€โ”€ favicon.svg
โ”‚   โ””โ”€โ”€ sample-image.svg          # Sample image for demos
โ”œโ”€โ”€ src/
โ”‚   โ”œโ”€โ”€ assets/
โ”‚   โ”‚   โ””โ”€โ”€ sample-image.svg      # Internal image for optimization
โ”‚   โ”œโ”€โ”€ components/
โ”‚   โ”‚   โ”œโ”€โ”€ ReactCounter.tsx      # Interactive React component
โ”‚   โ”‚   โ””โ”€โ”€ SvelteCounter.svelte  # Interactive Svelte component
โ”‚   โ”œโ”€โ”€ content/
โ”‚   โ”‚   โ”œโ”€โ”€ config.ts             # Content collections configuration
โ”‚   โ”‚   โ””โ”€โ”€ blog/
โ”‚   โ”‚       โ”œโ”€โ”€ first-post.md     # Sample blog post
โ”‚   โ”‚       โ””โ”€โ”€ second-post.md    # Sample blog post
โ”‚   โ”œโ”€โ”€ layouts/
โ”‚   โ”‚   โ””โ”€โ”€ Layout.astro          # Base layout component
โ”‚   โ””โ”€โ”€ pages/
โ”‚       โ”œโ”€โ”€ index.astro           # Homepage with navigation
โ”‚       โ”œโ”€โ”€ 404.astro             # Custom 404 error page
โ”‚       โ”œโ”€โ”€ 500.astro             # Custom 500 error page
โ”‚       โ”œโ”€โ”€ static-prerendered.astro
โ”‚       โ”œโ”€โ”€ react-component.astro
โ”‚       โ”œโ”€โ”€ svelte-component.astro
โ”‚       โ”œโ”€โ”€ client-side.astro
โ”‚       โ”œโ”€โ”€ ssg-page.astro
โ”‚       โ”œโ”€โ”€ ssr-page.astro
โ”‚       โ”œโ”€โ”€ image-optimization-internal.astro
โ”‚       โ”œโ”€โ”€ image-optimization-external.astro
โ”‚       โ”œโ”€โ”€ content-collection.astro
โ”‚       โ”œโ”€โ”€ blog/
โ”‚       โ”‚   โ””โ”€โ”€ [slug].astro      # Dynamic blog post pages
โ”‚       โ”œโ”€โ”€ ssg-dynamic/
โ”‚       โ”‚   โ””โ”€โ”€ [slug].astro      # SSG dynamic routes
โ”‚       โ””โ”€โ”€ ssr-dynamic/
โ”‚           โ””โ”€โ”€ [id].astro        # SSR dynamic routes
โ”œโ”€โ”€ astro.config.mjs              # Astro configuration
โ”œโ”€โ”€ package.json
โ””โ”€โ”€ tsconfig.json

๐Ÿ› ๏ธ Technology Stack

  • Astro - Meta-framework for building fast websites
  • React - UI library for interactive components
  • Svelte - Compiled UI framework
  • TypeScript - Type-safe JavaScript
  • Sharp - Image optimization library

๐Ÿ“„ Demo Pages

๐Ÿ  Homepage (/)

Navigation hub with links to all demo pages and feature explanations.

๐Ÿ“„ Static Pre-rendered (/static-prerendered)

  • Demonstrates export const prerender = true
  • Shows build-time generation
  • Explains SSG benefits and use cases

โš›๏ธ React Component Demo (/react-component)

  • Interactive React counter component
  • Client-side hydration with client:load
  • Comparison of hydration strategies

๐Ÿ”ฅ Svelte Component Demo (/svelte-component)

  • Interactive Svelte counter component
  • Scoped styles and reactivity
  • Framework comparison with React

๐Ÿ’ป Client-side Rendering (/client-side)

  • Vanilla JavaScript interactions
  • DOM manipulation examples
  • Progressive enhancement techniques

๐Ÿ—๏ธ SSG Page (/ssg-page)

  • Static site generation demonstration
  • Build-time data fetching
  • Performance characteristics

๐Ÿ”— SSG Dynamic Routes (/ssg-dynamic/[slug])

  • Pre-generated dynamic routes
  • getStaticPaths() implementation
  • Available routes: example-slug, another-post, third-post

๐Ÿ–ฅ๏ธ SSR Page (/ssr-page)

  • Server-side rendering with export const prerender = false
  • Real-time data generation
  • Request-specific information

๐Ÿ”— SSR Dynamic Routes (/ssr-dynamic/[id])

  • Runtime dynamic route handling
  • Server-side parameter processing
  • Test routes: example-id, product-123, user-456, order-789

๐Ÿ–ผ๏ธ Internal Image Optimization (/image-optimization-internal)

  • Astro's Image component usage
  • Automatic format conversion (WebP, AVIF)
  • Responsive image generation

๐ŸŒ External Image Optimization (/image-optimization-external)

  • External image optimization techniques
  • CDN service examples (Cloudinary, ImageKit)
  • Performance best practices

๐Ÿ“š Content Collection Demo (/content-collection)

  • Type-safe content management
  • Schema validation with Zod
  • Blog post listing and metadata

๐Ÿ“ Blog Posts (/blog/[slug])

  • Individual blog post pages
  • Content rendering with <Content />
  • Dynamic routes from content collections

๐Ÿšจ Error Pages

  • 404 Page (/404) - Custom not found page with helpful navigation
  • 500 Page (/500) - Server error page with troubleshooting info

๐Ÿš€ Getting Started

Prerequisites

  • Node.js version 18.20.8 or higher
  • npm or pnpm package manager

Installation

  1. Clone or navigate to the project directory:

    cd /local/home/sohaibus/dev/astro/astro-basic
  2. Install dependencies:

    npm install
  3. Start the development server:

    npm run dev
  4. Open your browser: Visit http://localhost:4321

Available Commands

Command Action
npm install Installs dependencies
npm run dev Starts local dev server at localhost:4321
npm run build Build your production site to ./dist/
npm run preview Preview your build locally, before deploying
npm run astro ... Run CLI commands like astro add, astro check
npm run astro -- --help Get help using the Astro CLI

๐ŸŽฏ Key Learning Points

Rendering Strategies

  • SSG: Best for content that doesn't change often (blogs, marketing pages)
  • SSR: Best for dynamic, user-specific content (dashboards, e-commerce)
  • Hybrid: Mix both strategies in the same application

Component Integration

  • React: Large ecosystem, virtual DOM, extensive tooling
  • Svelte: Compile-time optimizations, smaller bundles, built-in reactivity
  • Astro: Framework-agnostic, partial hydration, performance-focused

Content Management

  • Content Collections: Type-safe, schema validation, powerful querying
  • Markdown/MDX: Rich content authoring with component embedding
  • Dynamic Routes: Automatic page generation from content

Image Optimization

  • Internal: Automatic optimization, format conversion, responsive sizing
  • External: Manual optimization, CDN services, performance techniques

Error Handling

  • Custom Pages: Better user experience, helpful navigation
  • Status Codes: Proper HTTP responses for SEO and debugging

๐Ÿ”ง Configuration

Astro Config (astro.config.mjs)

export default defineConfig({
    adapter: amplify(),           // AWS Amplify adapter
    integrations: [react(), svelte()], // Framework integrations
    output: 'hybrid'              // Enable both SSG and SSR
});

Content Collections (src/content/config.ts)

const blogCollection = defineCollection({
  type: 'content',
  schema: z.object({
    title: z.string(),
    description: z.string(),
    pubDate: z.date(),
    author: z.string(),
    tags: z.array(z.string()).optional(),
  }),
});

๐Ÿš€ Deployment

This application is configured for AWS Amplify deployment but can be adapted for other platforms:

  • Vercel: Change adapter to @astrojs/vercel
  • Netlify: Change adapter to @astrojs/netlify
  • Node.js: Change adapter to @astrojs/node
  • Static: Remove adapter for static deployment

๐Ÿ“š Learning Resources

๐Ÿค Contributing

This is a demo application for learning purposes. Feel free to:

  • Experiment with the code
  • Add new demo pages
  • Try different integrations
  • Optimize performance

๐Ÿ“ License

This project is for educational purposes. Feel free to use it as a learning resource or starting point for your own Astro projects.


Happy coding with Astro! ๐Ÿš€

Contributors

sohaibussohaibuddinsyedastrobot-houston

Issues