求助:JavaScript在本地localhost运行正常,部署至GitHub Pages或Vercel后失效,仅index.html与cadastro.html异常
Hey there! Let's break down why your JavaScript files work perfectly locally but go missing on deployed platforms like GitHub Pages and Vercel—especially since your home.html runs without issues. Here are the most common fixes to try:
1. Fix Relative Path Mismatches
This is the #1 culprit for deployment path issues. Local servers and hosted platforms handle base URLs differently:
- On your local server,
localhost:XXXX/acts as your project root, so paths like/js/script.jswork because they point directly to your project'sjsfolder. - But on GitHub Pages, your site lives at
https://marincor.github.io/Bx-Bank/—so an absolute path like/js/script.jswould try to load fromhttps://marincor.github.io/js/script.js(which doesn't exist), instead of your repo's actualjsfolder.
Fix:
Compare the <script> tags in index.html/cadastro.html with the working ones in home.html. If the broken pages use absolute paths starting with /, switch to relative paths:
- Use
./js/your-script.js(anchors to the current file's directory) - Or just
js/your-script.js(works if the HTML file is in the root, same as yourjsfolder)
2. Check for Case Sensitivity Issues
Local systems like Windows don't care about uppercase/lowercase in file names, but GitHub Pages and Vercel run on Linux, which is strictly case-sensitive.
- If your folder is named
jsbut your script tag says<script src="JS/script.js"></script>, it'll work locally but fail once deployed.
Fix:
Double-check the spelling and case of your js folder and file names in both your HTML code and GitHub repo. Make sure they match exactly (e.g., js/main.js not Js/Main.js).
3. Verify File Presence & Git Ignore Settings
Even though home.html works, it's worth confirming:
- The JS files referenced in
index.html/cadastro.htmlactually exist in your GitHub repo'sjsfolder (no typos in file names). - Your
.gitignorefile isn't accidentally excluding any of these JS files or the entirejsfolder.
Fix:
Browse your GitHub repo directly to confirm the files are present. If they're missing, check your local .gitignore and commit/push the missing files to the repo.
4. Clear Browser & Deployment Cache
Sometimes old cached files can make it look like your JS is missing, even if it's deployed correctly.
- Browser cache: Press
Ctrl+Shift+R(Chrome) to force a hard refresh, or open DevTools > Network tab > check "Disable cache" and reload the page. - Deployment cache: Both GitHub Pages and Vercel sometimes cache old builds. For Vercel, trigger a manual redeploy; for GitHub Pages, push a tiny change (like updating a comment) to force a rebuild.
5. Check Deployment Logs
If none of the above works, look at the deployment logs for clues:
- GitHub Pages: Go to your repo's Settings > Pages > check the build status at the top for errors.
- Vercel: Log into your dashboard, find the
bx-bankproject, and review the deployment logs for any "file not found" errors related to your JS files.
Once you fix the path or case issues, your JS files should show up in DevTools' Sources tab on both platforms!
内容的提问来源于stack exchange,提问作者Marin

