Skip to content

Commit b9257f0

Browse files
committed
update sw
1 parent 584295b commit b9257f0

1,719 files changed

Lines changed: 1830 additions & 1743 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

website/config.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,7 @@ defaultContentLanguageInSubdir = true
180180
# 登录代理:GitHub 的 /login/oauth/access_token 接口不返回 CORS 头,浏览器无法直接调用,必须经服务端中转。
181181
# 原来的 Heroku 免费应用已停服,故改为指向本站同源路径,由服务器反向代理到 GitHub。
182182
# 因为与站点同源(都是 books.halfrost.com),浏览器不触发 CORS,最稳。
183-
# 需要在服务器 web 配置里加一个反代到 https://github.com/login/oauth/access_token 的 location(见对话里的 nginx 片段)。
183+
# 需要在服务器 web 配置里加一个反代到 https://github.com/login/oauth/access_token 的 location。
184+
# 完整且修好 502(SNI) 的 nginx 配置见 website/deploy/nginx-gitalk-oauth.conf。
184185
proxy= "https://books.halfrost.com/gitalk-oauth"
185186

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
# Gitalk OAuth 反向代理 —— 修复点击「使用 GitHub 登录」报
2+
# Error: Request failed with status code 502
3+
#
4+
# 背景:
5+
# GitHub 的 token 接口 https://github.com/login/oauth/access_token 不返回 CORS 头,
6+
# 浏览器无法直接调用,必须由站点同源转发(config.toml 里 Gitalk.proxy =
7+
# https://books.halfrost.com/gitalk-oauth 就是指这里)。
8+
#
9+
# 关于 502 的定位(重要):
10+
# 经实测,GitHub 端一切正常——不带 SNI 也能握手,直接 POST 返回 200(incorrect_client_credentials
11+
# 的 JSON)。所以 502 是【本服务器侧】的故障,最常见两类:
12+
# (1) 原来的 /gitalk-oauth 没有直连 GitHub,而是转发给一个本地中转程序
13+
# (gatekeeper / cors-anywhere 之类的 node 应用),该进程挂了 → nginx 502。
14+
# (2) 服务器到 github.com 出网被阻断/重置(国内主机常见)→ upstream 连接失败 → 502。
15+
# 下面的配置改成【nginx 直连 GitHub】,可消除 (1)。若换上后仍 502,基本就是 (2),
16+
# 请看文件末尾的「仍然 502 怎么办」。
17+
#
18+
# 用法:把下面的 location 放进 books.halfrost.com 的 server {} 块,然后
19+
# nginx -t && nginx -s reload
20+
# 验证:
21+
# curl -s -o /dev/null -w '%{http_code}\n' -X POST https://books.halfrost.com/gitalk-oauth \
22+
# -H 'Accept: application/json' \
23+
# -d 'client_id=x&client_secret=x&code=x'
24+
# 修好后应返回 200(GitHub 回 incorrect_client_credentials 的 JSON,但 HTTP 状态是 200),不再是 502。
25+
26+
location = /gitalk-oauth {
27+
proxy_http_version 1.1;
28+
proxy_pass https://github.com/login/oauth/access_token;
29+
30+
proxy_set_header Host github.com; # GitHub 按 Host 路由
31+
proxy_ssl_server_name on; # 发送 SNI(最佳实践;某些网络下必需)
32+
proxy_ssl_name github.com;
33+
proxy_ssl_protocols TLSv1.2 TLSv1.3;
34+
35+
proxy_set_header X-Real-IP $remote_addr;
36+
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
37+
proxy_set_header X-Forwarded-Proto $scheme;
38+
proxy_set_header Accept application/json;
39+
40+
# 连接到 GitHub 慢/不稳时,给足超时,避免过早 502
41+
proxy_connect_timeout 15s;
42+
proxy_read_timeout 15s;
43+
44+
# 若改用变量形式的 proxy_pass(set $up https://github.com;)才需要显式 DNS:
45+
# resolver 1.1.1.1 8.8.8.8 valid=300s;
46+
}
47+
48+
# ───────────────────────── 仍然 502 怎么办 ─────────────────────────
49+
# 先看真正的报错(关键):
50+
# tail -n 50 /var/log/nginx/error.log
51+
# 再在【服务器上】直接测到 GitHub 的连通性:
52+
# curl -v -m 15 -X POST https://github.com/login/oauth/access_token \
53+
# -H 'Accept: application/json' -d 'client_id=x&client_secret=x&code=x'
54+
# · 若服务器上这条 curl 也连不上/超时/reset → 是服务器出网到 github 的问题(上面的(2)),
55+
# nginx 配置再怎么改都没用。此时改用下面的 Cloudflare Worker 兜底(部署在 GitHub 可达的边缘)。
56+
#
57+
# ── 兜底方案:Cloudflare Worker(绕开本服务器出网,免费)──
58+
# 1. 在 Cloudflare 新建一个 Worker,代码:
59+
# export default {
60+
# async fetch(req) {
61+
# const url = 'https://github.com/login/oauth/access_token';
62+
# const r = await fetch(url, { method: 'POST', headers: req.headers, body: req.body });
63+
# const res = new Response(r.body, r);
64+
# res.headers.set('Access-Control-Allow-Origin', '*');
65+
# return res;
66+
# }
67+
# }
68+
# 2. 把 config.toml 里的 Gitalk.proxy 改成该 Worker 的地址,重新构建发布即可。
9.34 KB
Loading

website/public/en/404.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@
4141

4242
<script async>
4343
if ('serviceWorker' in navigator) {
44-
navigator.serviceWorker.register("\/leetcode\/serviceworker-v1.min.3b670f07e91c506468df879f46040abc66c3be76ba5dbb98ae3a4b3bf63913c0.js").then(function() {
44+
navigator.serviceWorker.register("\/leetcode\/serviceworker-v1.min.js", { updateViaCache: 'none' }).then(function() {
4545
if (navigator.serviceWorker.controller) {
4646
console.log('Assets cached by the controlling service worker.');
4747
} else {

website/public/en/ChapterFour/0001~0099/0001.Two-Sum/index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@
5050

5151
<script async>
5252
if ('serviceWorker' in navigator) {
53-
navigator.serviceWorker.register("\/leetcode\/serviceworker-v1.min.3b670f07e91c506468df879f46040abc66c3be76ba5dbb98ae3a4b3bf63913c0.js").then(function() {
53+
navigator.serviceWorker.register("\/leetcode\/serviceworker-v1.min.js", { updateViaCache: 'none' }).then(function() {
5454
if (navigator.serviceWorker.controller) {
5555
console.log('Assets cached by the controlling service worker.');
5656
} else {

website/public/en/ChapterFour/0001~0099/0002.Add-Two-Numbers/index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@
5050

5151
<script async>
5252
if ('serviceWorker' in navigator) {
53-
navigator.serviceWorker.register("\/leetcode\/serviceworker-v1.min.3b670f07e91c506468df879f46040abc66c3be76ba5dbb98ae3a4b3bf63913c0.js").then(function() {
53+
navigator.serviceWorker.register("\/leetcode\/serviceworker-v1.min.js", { updateViaCache: 'none' }).then(function() {
5454
if (navigator.serviceWorker.controller) {
5555
console.log('Assets cached by the controlling service worker.');
5656
} else {

website/public/en/ChapterFour/0001~0099/0003.Longest-Substring-Without-Repeating-Characters/index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@
5252

5353
<script async>
5454
if ('serviceWorker' in navigator) {
55-
navigator.serviceWorker.register("\/leetcode\/serviceworker-v1.min.3b670f07e91c506468df879f46040abc66c3be76ba5dbb98ae3a4b3bf63913c0.js").then(function() {
55+
navigator.serviceWorker.register("\/leetcode\/serviceworker-v1.min.js", { updateViaCache: 'none' }).then(function() {
5656
if (navigator.serviceWorker.controller) {
5757
console.log('Assets cached by the controlling service worker.');
5858
} else {

website/public/en/ChapterFour/0001~0099/0004.Median-of-Two-Sorted-Arrays/index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@
5454

5555
<script async>
5656
if ('serviceWorker' in navigator) {
57-
navigator.serviceWorker.register("\/leetcode\/serviceworker-v1.min.3b670f07e91c506468df879f46040abc66c3be76ba5dbb98ae3a4b3bf63913c0.js").then(function() {
57+
navigator.serviceWorker.register("\/leetcode\/serviceworker-v1.min.js", { updateViaCache: 'none' }).then(function() {
5858
if (navigator.serviceWorker.controller) {
5959
console.log('Assets cached by the controlling service worker.');
6060
} else {

website/public/en/ChapterFour/0001~0099/0005.Longest-Palindromic-Substring/index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@
5656

5757
<script async>
5858
if ('serviceWorker' in navigator) {
59-
navigator.serviceWorker.register("\/leetcode\/serviceworker-v1.min.3b670f07e91c506468df879f46040abc66c3be76ba5dbb98ae3a4b3bf63913c0.js").then(function() {
59+
navigator.serviceWorker.register("\/leetcode\/serviceworker-v1.min.js", { updateViaCache: 'none' }).then(function() {
6060
if (navigator.serviceWorker.controller) {
6161
console.log('Assets cached by the controlling service worker.');
6262
} else {

website/public/en/ChapterFour/0001~0099/0006.ZigZag-Conversion/index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@
4848

4949
<script async>
5050
if ('serviceWorker' in navigator) {
51-
navigator.serviceWorker.register("\/leetcode\/serviceworker-v1.min.3b670f07e91c506468df879f46040abc66c3be76ba5dbb98ae3a4b3bf63913c0.js").then(function() {
51+
navigator.serviceWorker.register("\/leetcode\/serviceworker-v1.min.js", { updateViaCache: 'none' }).then(function() {
5252
if (navigator.serviceWorker.controller) {
5353
console.log('Assets cached by the controlling service worker.');
5454
} else {

0 commit comments

Comments
 (0)