This document explains the architecture and workflow for deploying your React application to GitHub Pages, clarifying the roles of different branches, tools, and processes.
npm's Role:npmis the command-line tool that comes with Node.js. It's used to manage your project's dependencies (like React, Vite,gh-pages) and to run predefined scripts.package.jsonScripts: Yourpackage.jsonfile defines a set of custom commands under the"scripts"section. These automate common tasks:"build": "vite build": This is the core command that triggers your build tool (Vite) to compile your source code."predeploy": "npm run build": This is a specialnpmscript. When you runnpm run deploy,npmautomatically looks for and executes a script namedpredeploybefore runningdeploy. This ensures your project is always built before it's deployed."deploy": "gh-pages -d dist": This is the command you run to initiate the deployment. It uses thegh-pagesnpm package.
- Purpose: The
masterbranch is your primary development branch. It contains all your original, human-readable source code.- This includes your React components (
.tsxfiles), styling files, configuration files (vite.config.ts,tsconfig.json),package.json,README.md, etc.
- This includes your React components (
- What's NOT here: It does not contain the compiled, optimized, or bundled files that a web browser needs to run your application. These are generated during the build process.
- Why it's separate: Keeping source code separate from built code ensures a clean, manageable development history. You commit changes to your source code here.
- Role: Vite is a modern build tool that takes your source code and transforms it into a set of static assets (HTML, CSS, JavaScript) that web browsers can understand and execute efficiently.
- Process: When
npm run build(i.e.,vite build) runs:- It transpiles your TypeScript (
.tsx) into plain JavaScript. - It bundles all your JavaScript modules into fewer, larger files (often called "chunks" or "bundles") to reduce network requests.
- It minifies your code (removes whitespace, shortens variable names) to reduce file size.
- It processes your
index.htmland injects references to the bundled JavaScript and CSS. - It places all these final, optimized, static files into a directory named
dist/(short for "distribution").
- It transpiles your TypeScript (
- Purpose: The
gh-pagesbranch is a special branch specifically recognized by GitHub Pages. It serves as the repository for your built, static website files. - What's here: After
npm run buildcreates thedist/folder, thegh-pagesnpm package takes the contents of thatdist/folder and pushes them to thegh-pagesbranch.- This is why you see
index.htmland theassets/directory (containing your bundled.jsand.cssfiles) directly at the root of this branch.
- This is why you see
- Why it's separate: It keeps your deployment history clean and distinct from your source code history. It also ensures that GitHub Pages only serves the necessary, optimized files.
- Role: This package automates the process of taking your built
dist/directory and pushing its contents to thegh-pagesbranch of your GitHub repository. - How it works: When
gh-pages -d distruns, it essentially performs Git operations behind the scenes:- It creates a temporary Git repository or works in a temporary context.
- It copies the contents of your
dist/folder into this context. - It commits these files.
- It then force-pushes these commits to the
gh-pagesbranch on youroriginremote. This overwrites the previous content ofgh-pageswith your new build.
- Role: GitHub Pages is a free static site hosting service provided by GitHub.
- How it works: Once you enable GitHub Pages for your repository (which you have done), GitHub continuously monitors the designated branch (in your case,
gh-pages). Whenever new commits are pushed togh-pages, GitHub Pages automatically detects the changes and deploys the new version of your website.
- The Connection: Your
homepagefield inpackage.json("homepage": "https://manamama.github.io/AI/") tells thegh-pagespackage what the base URL of your deployed site will be. - How it's served: This URL directly points to the content of the
gh-pagesbranch.https://manamama.github.io/is the base domain for GitHub Pages for user/organization pages./AI/is derived from your repository name (AI).
- The
assetsdirectory: When your browser loadshttps://manamama.github.io/AI/, it first requestsindex.html. Insideindex.html, there are references to your JavaScript and CSS files, which are located in theassets/directory (e.g.,<script src="/AI/assets/index-d5qmFW8U.js"></script>). GitHub Pages serves these files directly from theassets/folder within yourgh-pagesbranch.
- You write and commit source code to your
masterbranch. - You run
npm run deploy. npmfirst runsnpm run build(Vite).- Vite compiles your source code into optimized, static files in the
dist/folder. - The
gh-pagespackage takes the contents ofdist/and pushes them to thegh-pagesbranch on GitHub. - GitHub Pages detects the new commits on
gh-pages. - GitHub Pages serves the content of the
gh-pagesbranch athttps://manamama.github.io/AI/, making your application live.
This two-branch system (master for source, gh-pages for built output) is crucial for maintaining a clean development workflow while simultaneously providing a functional, optimized live website via GitHub Pages.