To improve security and reduce information leaks and spyware, HTTP (the protocol used to share most web resources) have a set of rules about cross-origin resource sharing
. For our purposes there are two broad ideas to consider here.
fetch during developmentIf you run your page using the file:// scheme, fetch and its friends are unlikely to work. Instead, you’ll need to run a local webserver. But many local webservers encourage browsers to cache files, meaning that edits you make to files will seem not to have happened.
We have multiple possible solutions, using different local server tools.
Make a file webserver.py with the following contents:
import http.server
import socketserver
PORT = 8080
class NoCacheHTTPRequestHandler(http.server.SimpleHTTPRequestHandler):
def end_headers(self):
# Send headers to prevent the browser from caching anything
self.send_header("Cache-Control", "no-cache, no-store, must-revalidate")
self.send_header("Pragma", "no-cache")
self.send_header("Expires", "0")
super().end_headers()
# Allow restarting the server immediately without "Address already in use" errors
socketserver.TCPServer.allow_reuse_address = True
with socketserver.TCPServer(("", PORT), NoCacheHTTPRequestHandler) as httpd:
print(self_address := f"Server running at http://localhost:{PORT}/")
try:
httpd.serve_forever()
except KeyboardInterrupt:
print("\nServer stopped.")Run python webserver.py (or python3 or py3 or whatever your Python executable is called) from the directory containing your HTML file, and then open http://localhost:8080
Run npx http-server -c-1 from the directory containing your HTML file, and then open http://localhost:8080.
Run npx live-server from the directory containing your HTML file, and then open http://localhost:8080.
This inserts JavaScript into your HTML files to automatically reload the page when a file is edited. That can be convenient, but can also be confusing to debug if you try to trace everything the page is doing.
Install the Live server
extension by Ritwick Dey in VS Code and use its Go Live
button. This inserts JavaScript into your HTML files to automatically reload the page when a file is edited. That can be convenient, but can also be confusing to debug if you try to trace everything the page is doing.
Run any local server you know how to run. To bypass caching,
Disable Cachebutton
Run any local server you know how to run. To bypass caching,
fetch calls, use fetch(myFileName, {cache:"no-store"}) to bypass the cache.<script> tags, they can be forcibly reloaded despite caching in a platform-specific way: