Skip to content

Commit 3752763

Browse files
committed
scripts: new web server script
This provides a new script for serving the output of `yarn build`. This is needed since the app now requires special http headers to be defined to use certain web APIs.
1 parent f24db4a commit 3752763

File tree

2 files changed

+36
-2
lines changed

2 files changed

+36
-2
lines changed

CONTRIBUTING.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -118,10 +118,10 @@ So for local testing without https, we need to follow
118118
To serve the website, run:
119119

120120
yarn build
121-
python3 -m http.server --directory build
121+
./scripts/serve.py
122122

123123
Note: the usual `npx serve` doesn't properly serve parts of the site so we use
124-
Python instead.
124+
a custom Python script instead.
125125

126126
Then at `chrome://flags/#unsafely-treat-insecure-origin-as-secure`, add:
127127

scripts/serve.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
#!/usr/bin/env python3
2+
3+
"""
4+
Serve the build directory using builtin Python web server.
5+
"""
6+
7+
import http.server
8+
import pathlib
9+
import socketserver
10+
11+
BUILD_DIR = (pathlib.Path(__file__).parent.parent / "build").resolve()
12+
PORT = 8000
13+
14+
15+
class Handler(http.server.SimpleHTTPRequestHandler):
16+
def __init__(
17+
self,
18+
request: bytes,
19+
client_address: tuple[str, int],
20+
server: socketserver.BaseServer,
21+
directory: str | None = ...
22+
) -> None:
23+
super().__init__(request, client_address, server, directory=BUILD_DIR)
24+
25+
def end_headers(self) -> None:
26+
# custom headers needed for some web API features
27+
self.send_header("Cross-Origin-Opener-Policy", "same-origin")
28+
self.send_header("Cross-Origin-Embedder-Policy", "require-corp")
29+
return super().end_headers()
30+
31+
32+
with socketserver.TCPServer(("", PORT), Handler) as httpd:
33+
print(f"serving at http://0.0.0.0:{PORT}")
34+
httpd.serve_forever()

0 commit comments

Comments
 (0)