Migration Guide: GitHub Pages to Netlify
Note: This migration has been completed! The site is now fully deployed to Netlify at https://shaftengine.netlify.app/, and GitHub Pages (https://shafthq.github.io/) automatically redirects all visitors to the Netlify deployment.
Overview
This guide explains the migration from GitHub Pages to Netlify to address the security vulnerability where the Gemini API key was being embedded in client-side JavaScript files.
Security Issue
Problem: The previous deployment approach embedded the GEMINI_API_KEY in the static build files during compilation. This meant anyone could extract the API key from the website's JavaScript source code.
Solution: Use Netlify's serverless functions to create a secure API proxy that keeps the API key server-side only.
Architecture Changes
Before (Insecure)
User Browser → Static Site (with embedded API key) → Gemini API
- API key was in
docusaurus.config.jscustomFields - Key was bundled into
build/assets/js/main.*.js - Anyone could extract the key from browser DevTools
After (Secure)
User Browser → Static Site → Netlify Function (with API key) → Gemini API
- API key is stored as Netlify environment variable
- Frontend calls
/api/gemini-proxyendpoint - Netlify Function handles the actual Gemini API calls
- API key never reaches the client
Files Changed
1. docusaurus.config.js
Removed:
customFields: {
GEMINI_API_KEY: process.env.GEMINI_API_KEY || '',
},
2. src/components/AutoBot/index.tsx
Changed: Removed direct Gemini API integration
Added: Fetch call to /api/gemini-proxy endpoint
3. netlify.toml (New)
Configuration file for Netlify deployment:
- Build command:
yarn build - Publish directory:
build - Functions directory:
netlify/functions - API proxy redirect:
/api/*→/.netlify/functions/:splat
4. netlify/functions/gemini-proxy.mjs (New)
Serverless function that:
- Receives chat requests from the frontend
- Securely accesses Gemini API with server-side API key
- Returns responses to the frontend
5. .github/workflows/deploy.yml
Removed: GEMINI_API_KEY from build step environment variables
(Key is no longer needed during build since it's not embedded)
Migration Steps
Step 1: Set up Netlify Account
- Go to netlify.com and sign up
- Click "Add new site" → "Import an existing project"
- Choose GitHub and authorize Netlify
- Select the
shafthq.github.iorepository
Step 2: Configure Build Settings
Netlify should auto-detect settings from netlify.toml, but verify:
- Build command:
yarn build - Publish directory:
build - Functions directory:
netlify/functions
Step 3: Add Environment Variable
- Go to Site settings → Environment variables
- Click "Add a variable"
- Key:
GEMINI_API_KEY - Value: Your Gemini API key
- Click "Save"
Step 4: Deploy
- Click "Deploy site"
- Wait for the build to complete
- Netlify will provide you with a URL (e.g.,
random-name-123.netlify.app)
Step 5: Set up Custom Domain (Optional)
If you want to keep using shafthq.github.io:
Option A: Use Netlify subdomain
- Update DNS to point to Netlify
- Add custom domain in Netlify settings
Option B: Keep GitHub Pages URL
- Update repository settings to use Netlify deploy previews
- Keep GitHub Pages for documentation only
Step 6: Test the Chatbot
- Visit your Netlify site
- Open the AutoBot chatbot
- Send a test message
- Check browser DevTools → Network tab to verify requests go to
/api/gemini-proxy - Inspect the built JavaScript files to confirm no API key is embedded
Step 7: Disable GitHub Pages Deployment (Optional)
If moving entirely to Netlify:
- Disable the GitHub Actions workflow or modify it to skip deployment
- In repository settings, disable GitHub Pages
Verification
Verify API Key is NOT in Client Code
# Build the site
yarn build
# Search for API key patterns (should return nothing)
grep -r "AIza" build/
grep -r "GEMINI_API_KEY" build/
# Check the main JavaScript bundle
find build/assets/js -name "main.*.js" -exec grep -l "customFields" {} \;
# Should find customFields but NO API key value
Verify Function is Called
- Open browser DevTools (F12)
- Go to Network tab
- Open AutoBot and send a message
- Look for request to
/api/gemini-proxy - Response should come from Netlify Function, not direct Gemini API call
Rollback Plan
If you need to rollback for any reason:
- Revert changes to
docusaurus.config.js(restore customFields) - Revert changes to
src/components/AutoBot/index.tsx - Restore
GEMINI_API_KEYin.github/workflows/deploy.ymlbuild step - Delete
netlify.tomlandnetlify/functions/ - Re-deploy via GitHub Actions
Note: This rollback is NOT recommended as it reintroduces the security vulnerability.
Benefits of Migration
- Security: API key is never exposed to clients
- Control: Server-side validation and rate limiting possible
- Monitoring: Centralized logging of API usage
- Flexibility: Easy to add authentication, caching, or other middleware
- Cost Control: Better management of API quota usage
Support
If you encounter issues:
- Check Netlify Function logs in Netlify dashboard
- Verify
GEMINI_API_KEYenvironment variable is set - Check browser console for errors
- Review Network tab for failed requests
GitHub Pages Redirect
To ensure users visiting the old GitHub Pages URL are automatically redirected to Netlify:
- A simple redirect page is maintained in the
redirect-page/directory - The GitHub Actions workflow (
.github/workflows/deploy.yml) deploys only this redirect page to GitHub Pages - When users visit https://shafthq.github.io/, they are automatically redirected to https://shaftengine.netlify.app/
- The redirect uses both meta refresh and JavaScript for maximum compatibility
This ensures a seamless transition for users who have bookmarked or linked to the old URL.