fix: resolve blank page on GitHub Pages sub-path deployment#198
fix: resolve blank page on GitHub Pages sub-path deployment#198
Conversation
Co-authored-by: kiyarose <75678535+kiyarose@users.noreply.github.com> Agent-Logs-Url: https://github.com/SillyLittleTech/Portf/sessions/2cadf2ca-1550-42e3-8991-ae0c7fbc628e
There was a problem hiding this comment.
Pull request overview
Fixes blank-page deployments when the app is served from a GitHub Pages repo sub-path by ensuring both Vite asset URLs and the client-side router respect the configured base path.
Changes:
- Configure Vite
baseviaVITE_BASE_URLto emit correct asset URLs for sub-path deployments. - Add base-path stripping/applying in navigation helpers using
import.meta.env.BASE_URL. - Update Pages build scripts and workflow to provide
VITE_BASE_URLduring GitHub Pages builds.
Reviewed changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated 3 comments.
| File | Description |
|---|---|
vite.config.ts |
Adds base driven by VITE_BASE_URL with explanatory deployment scenarios. |
src/utils/navigation.ts |
Strips base path from window.location.pathname for route matching and re-applies it on navigation. |
package.json |
Adds a build:pages script variant that builds with a sub-path base and a help script. |
.github/workflows/github-pages.yml |
Sets VITE_BASE_URL for the Pages build step and documents template-user edits. |
| "build:pages": "tsc -b && VITE_BASE_URL=/Portf/ vite build", | ||
| "build:pages:help": "echo \"\\nTEMPLATE USERS: build:pages sets VITE_BASE_URL=/Portf/ for the showcase sub-path.\\nIf your repo has a different name, replace /Portf/ with /<your-repo-name>/ in this script\\nand in the VITE_BASE_URL env var in .github/workflows/github-pages.yml.\\nDeploying to a root domain? Use 'npm run build' instead (no sub-path needed).\\n\"", |
There was a problem hiding this comment.
build:pages sets an env var using POSIX-style VITE_BASE_URL=/Portf/ ..., which won’t work on Windows shells. Consider switching to a cross-platform approach (e.g., a CLI --base flag or cross-env) so template users can run the script locally on any OS.
| "build:pages": "tsc -b && VITE_BASE_URL=/Portf/ vite build", | |
| "build:pages:help": "echo \"\\nTEMPLATE USERS: build:pages sets VITE_BASE_URL=/Portf/ for the showcase sub-path.\\nIf your repo has a different name, replace /Portf/ with /<your-repo-name>/ in this script\\nand in the VITE_BASE_URL env var in .github/workflows/github-pages.yml.\\nDeploying to a root domain? Use 'npm run build' instead (no sub-path needed).\\n\"", | |
| "build:pages": "tsc -b && vite build --base=/Portf/", | |
| "build:pages:help": "echo \"\\nTEMPLATE USERS: build:pages runs 'vite build --base=/Portf/' for the showcase sub-path.\\nIf your repo has a different name, replace /Portf/ with /<your-repo-name>/ in this script\\nand in the VITE_BASE_URL env var in .github/workflows/github-pages.yml.\\nDeploying to a root domain? Use 'npm run build' instead (no sub-path needed).\\n\"", |
| # TEMPLATE USERS: change /Portf/ below to /<your-repo-name>/ — or | ||
| # remove the env block entirely when deploying to a root domain. | ||
| env: | ||
| VITE_BASE_URL: /Portf/ | ||
| run: npm run build:pages |
There was a problem hiding this comment.
The workflow sets VITE_BASE_URL in the Build step, but npm run build:pages also hard-codes VITE_BASE_URL=/Portf/ in package.json; the script-level assignment will override whatever is set here. To avoid drift/confusion for template users, pick a single source of truth (either remove the env block here and rely on the script, or remove the hard-coded value from the script and rely on this env).
| # If you are using this as a template for your own project you will need to | ||
| # update TWO values so that assets load from the correct path: | ||
| # | ||
| # 1. VITE_BASE_URL (in the Build step below) | ||
| # Set this to "/<your-repo-name>/" — include both slashes. | ||
| # Example: if your repo is "github.com/jane/my-portfolio", use "/my-portfolio/". | ||
| # If you are deploying to a root domain (e.g. https://jane.github.io) or a | ||
| # custom domain pointed at the repo root, set VITE_BASE_URL to "/" or | ||
| # remove the env block entirely. | ||
| # | ||
| # 2. The "base" value in vite.config.ts | ||
| # Normally this is driven automatically by VITE_BASE_URL above, so you | ||
| # only need to change it if you want to hard-code the path instead of | ||
| # using the environment variable. |
There was a problem hiding this comment.
The header comment says template users must update TWO values, including “the base value in vite.config.ts”, but the config now reads base from VITE_BASE_URL and typically doesn’t need editing. Rewording this section to point to the actual second source (the build:pages script) or to clarify that vite.config.ts usually stays unchanged would prevent misconfiguration.
Deploying to a sub-path (e.g.
/Portf/) produced a blank page because Vite built withbase: "/", so every asset reference pointed at the domain root and 404'd silently. The client-side router also broke becausewindow.location.pathnamereturned the full/Portf/privacy-policyprefix, which matched no routes.Changes
vite.config.ts— addsbase: process.env.VITE_BASE_URL ?? "/". Includes scenario-annotated comments for root domain, GitHub Pages sub-path, and generic CDN deployments so template users know exactly what to set and where.src/utils/navigation.ts— readsimport.meta.env.BASE_URL(Vite-injected) intoBASE_PATHat module init.getCurrentPath()strips the prefix before route matching;navigateTo()re-applies it beforehistory.pushState. Both helpers are no-ops whenBASE_PATHis empty (root deployment). Includes a clear comment block telling template users they never need to touch this file.package.json—build:pagesnow runstsc -b && VITE_BASE_URL=/Portf/ vite build(env var scoped tovite buildonly). Adds abuild:pages:helpscript that echoes template guidance at the terminal..github/workflows/github-pages.yml— exposesVITE_BASE_URLas an explicitenv:block on the Build step with an inline change instruction. Adds a header comment block listing the two values a fork must update (VITE_BASE_URLhere + inpackage.json).Original prompt
📱 Kick off Copilot coding agent tasks wherever you are with GitHub Mobile, available on iOS and Android.