📝
marcyannotes
  • Welcome
  • Student Guidelines & Policies
    • Student Handbook
    • AI Policy
    • Academic Calendar
  • Environment Setup
    • Local Environment Setup - Mac
    • Local Environment Setup - Windows
    • GitHub Setup
    • Postgres Setup
  • Fullstack Software Engineering Curriculum
    • Overview
    • How-Tos
      • How To Code at Marcy: Code Style Guide
      • How to Do Short Response and Coding Assignments
      • How to Debug
      • How to PEDAC
      • How to Create Projects with Vite
      • How to Deploy on GitHub Pages
      • How to Deploy on Render
    • Mod 0 - Command Line Interfaces, Git, and GitHub
      • Mod 0 Overview
      • Command Line Interfaces
      • Git & GitHub
      • Git Pulling & Merging
      • Git Branching & PRs
      • Pair Programming: BONUS
    • Mod 1 - JavaScriptFundamentals
      • Mod 1 Overview
      • Intro to Programming
      • Errors
      • Node & Node Modules
      • Variables, Functions & String Methods
      • Control Flow, typeof, and Math
      • Loops
      • Arrays
      • Objects
      • Higher Order Functions: Callbacks
      • Higher Order Functions: Array Methods
      • Regex
    • Mod 2 - HTML, CSS & the DOM
      • Mod 2 Overview
      • HTML
      • CSS
      • Accessibility (a11y)
      • The DOM
      • Events
      • Forms
      • The Box Model and Positioning
      • Flexbox
      • Grid & Media Queries
      • ESModules
      • LocalStorage
    • Mod 3 - Async & APIs
      • Mod 3 Overview
      • Promises
      • Fetch
      • Building a Fetching App
      • Async & Await
    • Mod 4 - Project Week!
      • Project Week Overview
    • Mod 5 - Object-Oriented Programming
      • Mod 5 Overview
      • Intro to OOP, Encapsulation, Factory Functions, and Closure
      • Classes
      • Private & Static
      • Has Many/Belongs To
      • Polymorphism
    • Mod 6 - Data Structures & Algorithms
      • Mod 6 Overview
      • Stacks & Queues
      • Nodes & Linked Lists
      • Singly & Doubly Linked Lists
      • Recursion
      • Trees
    • Mod 7 - React
      • Mod 7 Overview
      • Intro to React
      • Events, State, and Forms
      • Fetching with useEffect
      • Building a Flashcards App
      • React Context
      • Global Context Pattern
      • React Router
    • Mod 8 - Backend
      • Mod 8 Overview
      • Intro to Express
      • Building a Static Web Server with Middleware
      • Securing API Keys with Environment Variables
      • Building a RESTful API with MVC
      • SQL and Databases
      • JOIN (Association) SQL Queries
      • Knex
      • Your First Fullstack App!
      • Migrations & Seeds
      • Schema Design & Normalization
      • Hashing Passwords with Bcrypt
  • Code Challenge Curriculum
    • Unit 0
      • Lecture: Functions in JS
      • CC-00: Functions and Console Logs
      • CC-01: Conditionals
      • CC-02: Conditionals 2
    • Unit 1
      • CC-03: For Loops
      • CC-04: For Loops and Conditionals
      • CC-05: For Loops and Conditionals 2
    • Unit 2
      • CC-06: String Mutations
      • CC-07: Array Iteration
      • CC-08: String Mutation and Array Iteration
      • CC-09: Array Mutations
      • CC-10: Reading Objects
      • CC-11: Objects
      • CC-12: Objects
      • Unit 2 Diagnostic
    • Unit 3
      • Intro to PEDAC (and Algorithms)
      • validTime
      • fizzBuzz (array)
      • digitSumDifference
      • firstNotRepeating
      • compareEvenAndOddSum
      • countVowelConsonants
      • finalHP
      • canMakeTriangle
    • Unit 4
    • Unit 5
    • Unit 6
    • Unit 7
    • Unit 8
    • Sorting
Powered by GitBook
On this page
  • Prerequisites
  • What is Github Pages?
  • Configure Vite for Deployment on Github Pages
  • Publish on Github Pages
  1. Fullstack Software Engineering Curriculum
  2. How-Tos

How to Deploy on GitHub Pages

PreviousHow to Create Projects with ViteNextHow to Deploy on Render

Last updated 8 months ago

So, you've built an app - congrats! You can run it locally, but wouldn't it be sweet if everyone on the internet could use it??

This resource covers deploying a Vanilla JS Vite app using Github Pages.

Note: An alternate (and more complex, but potentially smoother) approach can be found on .

Table of Contents

Prerequisites

In order to deploy to Github using this guide, you will need

  • A project built using Vite

  • A Github repo with that Vite project inside.

What is Github Pages?

... a static site hosting service that takes HTML, CSS, and JavaScript files straight from a repository on GitHub, optionally runs the files through a build process, and publishes a website.

There are a few ways to utilize Github Pages but the most straightforward is to have an index.html in the root of our repo that Github Pages can easily find and serve.

With a few VERY easy settings, we can publish our repo as long as that index.html file is in the root.

To test this out:

  • add an index.html file to the root of your repo

  • commit and push it

  • delete the index.html file once you've tested it.

Configure Vite for Deployment on Github Pages

Objective(s): Build the production version of the app in the root of the directory.

If you made an index.html file for testing above, delete it now.

Assuming you built that app using Vite, the first step is to make the production version of the application. To do this, run the command:

npm run build

This will create the production version of your app in a folder called dist/ (short for "distribution"). Take a look inside! It will have an index.html file and an assets/ folder with your JavaScript and CSS.

However, we need that index.html to be in the root of the repo:

In order to do that, we'll need to configure Vite to create that version in the right location.

Create a Vite configuration file

touch vite.config.js

And put this inside:

import { defineConfig } from 'vite'

export default defineConfig({
  // GitHub Pages expects an index.html in the root directory
  // so just run npm build before pushing to GitHub and this will rebuild our assets to the root
  build: { outDir: '..' },
  // needed for github pages just put the repo name here
  base: '/your-repo-name-here/', 
});

This does two things:

  • build and outDir determine where to put your compiled application. Normally it would put it in a folder called dist/ but in order for Github pages to work, we want to build in the root of the repo.

  • base determines how the index.html file connects to the .js and .css files in your assets/ folder

Now, run the command

npm run build

This will compile the code you've written in your app/ folder into optimized static files that can quickly be served by Github pages. It will put those files in the root directory of your repo, where Github expects to find an index.html file and any associated assets.

You can see what this "deployed" version will look like by running the command...

npm run preview

...which will serve the application at http://localhost:4173/

Finally, commit and push your new compiled version to Github!

Note: each time your npm run build, new versions of your assets will be created and you will need to manually delete them.

Publish on Github Pages

Objective(s): Publish your web app!

Deploying your application on Github Pages is about as easy as it gets:

  1. Open the repo on Github.com

  2. Go to the Settings tab

  3. Find the Pages section

  4. Make sure that Source is Deploy from a branch

  5. Below, set Branch to main (or whatever branch you're using)

  6. Click Save and wait a few minutes! You should see the URL of your application.

  7. Each time you push a new commit to main, your page will redeploy!

Github Pages is ()

follow the instructions below in the .

according to their website
Publish On Github Page section
Vite's own docs
Prerequisites
What is Github Pages?
Configure Vite for Deployment on Github Pages
Publish on Github Pages