Skip to content

Commit a712db3

Browse files
authored
feat: serve markdown automatically based on Accept header (#1997)
Alters the nginx config so it checks the `Accept` header, if it contains `text/plain` or `text/markdown`, we serve the markdown version directly. Also added tests for this behavior to the existing PR check. The previews are likely using the config from master, so there it doesn't work yet (it should after we merge this). Since the test workflow now effectively verifies the nginx config is valid, I guess we can drop the `nginx.conf-test.yaml` workflow.
1 parent 10da313 commit a712db3

File tree

4 files changed

+102
-26
lines changed

4 files changed

+102
-26
lines changed

.github/workflows/nginx.conf-test.yaml

Lines changed: 0 additions & 24 deletions
This file was deleted.

.github/workflows/test.yaml

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,65 @@ jobs:
3535
INTERCOM_APP_ID: ${{ secrets.INTERCOM_APP_ID }}
3636
SEGMENT_TOKEN: ${{ secrets.SEGMENT_TOKEN }}
3737

38+
- name: Install Nginx
39+
run: |
40+
sudo apt-get update
41+
sudo apt-get install -y nginx
42+
43+
- name: Start Docusaurus server
44+
run: |
45+
nohup npx docusaurus serve --port 3000 --no-open &
46+
sleep 5
47+
curl -f http://localhost:3000 > /dev/null
48+
49+
- name: Start Nginx with project config
50+
run: |
51+
cat > default.conf <<EOF
52+
worker_processes auto;
53+
error_log $(pwd)/logs/error.log;
54+
pid $(pwd)/logs/nginx.pid;
55+
events {}
56+
http {
57+
access_log $(pwd)/logs/access.log;
58+
include $(pwd)/nginx.conf;
59+
}
60+
EOF
61+
mkdir -p $(pwd)/logs
62+
nginx -c $(pwd)/default.conf
63+
sleep 1
64+
65+
- name: Run header assertions
66+
run: |
67+
set -euo pipefail
68+
function assert_header() {
69+
url=$1
70+
header=$2
71+
expected=$3
72+
shift 3
73+
extra_args=("$@")
74+
actual=$(curl -s -D - -o /dev/null "${extra_args[@]}" "$url" | grep -i "^$header" | tr -d '\r' || true)
75+
echo "→ $url → $actual"
76+
echo "$actual" | grep -q "$expected" || (echo "❌ Expected '$expected' in '$header' for $url" && exit 1)
77+
}
78+
79+
echo "🧪 Checking Nginx responses..."
80+
81+
assert_header "http://localhost:8080/" "Content-Type" "text/html"
82+
assert_header "http://localhost:8080/" "Content-Type" "text/markdown" -H "Accept: text/markdown"
83+
assert_header "http://localhost:8080/platform/proxy/usage" "Content-Type" "text/html"
84+
assert_header "http://localhost:8080/platform/proxy/usage.md" "Content-Type" "text/markdown"
85+
assert_header "http://localhost:8080/platform/proxy/usage" "Content-Type" "text/markdown" -H "Accept: text/markdown"
86+
assert_header "http://localhost:8080/img/docs-og.png" "Content-Type" "image/png"
87+
assert_header "http://localhost:8080/img/javascript-40x40.svg" "Content-Type" "image/svg"
88+
assert_header "http://localhost:8080/llms.txt" "Content-Type" "text/markdown"
89+
assert_header "http://localhost:8080/llms-full.txt" "Content-Type" "text/markdown"
90+
91+
echo "✅ All Nginx header checks passed."
92+
93+
- name: Stop Nginx
94+
if: always()
95+
run: nginx -c $(pwd)/default.conf -s stop
96+
3897
lint_content:
3998
name: Lint markdown content
4099
runs-on: ubuntu-latest

CONTRIBUTING.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ This will be enough to work on Platform, Academy and OpenAPI. If you want to wor
7070
5. Add a record to `/etc/hosts`, which maps the `docs.apify.loc` to a localhost:
7171
7272
```text
73-
127.0.01 docs.apify.loc
73+
127.0.0.1 docs.apify.loc
7474
```
7575
7676
You should be able to open https://docs.apify.loc in your browser and run all the repositories jointly as one project.

nginx.conf

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,51 @@
1+
map $http_accept $wants_markdown {
2+
default 0;
3+
"~*text/markdown" 1;
4+
"~*text/plain" 1;
5+
}
6+
17
server {
28
listen 0.0.0.0:8080;
39
server_name 'docs.apify.com';
410

11+
# homepage: only rewrite to internal proxy when client wants markdown
12+
location = / {
13+
if ($wants_markdown) {
14+
# use 'last' to force a new location lookup so /__proxy/ handler runs
15+
rewrite ^ /__proxy/llms.txt last;
16+
}
17+
proxy_pass http://localhost:3000;
18+
}
19+
20+
# direct llms.txt files, serve with markdown content type
21+
location ~ ^/llms(-full)?\.txt$ {
22+
proxy_pass http://localhost:3000;
23+
proxy_hide_header Content-Type;
24+
add_header Content-Type "text/markdown; charset=utf-8" always;
25+
}
26+
27+
# generic docs fallback: rewrite clean URLs to .md when client wants markdown
528
location / {
6-
proxy_pass https://apify.github.io/apify-docs/;
29+
if ($wants_markdown) {
30+
# rewrite to internal proxy path; use last so new location is matched
31+
rewrite ^(/[^.]+)$ /__proxy$1.md last;
32+
}
33+
proxy_pass http://localhost:3000;
34+
}
35+
36+
# internal proxy handler that actually forces the markdown header
37+
location ^~ /__proxy/ {
38+
internal;
39+
40+
# strip the /__proxy prefix so upstream sees the real path
41+
rewrite ^/__proxy(/.*)$ $1 break;
42+
43+
proxy_pass http://localhost:3000;
44+
proxy_hide_header Content-Type;
45+
add_header Content-Type "text/markdown; charset=utf-8" always;
746
}
47+
48+
# proxies to other repositories
849
location /api/client/js {
950
proxy_pass https://apify.github.io/apify-client-js/;
1051
}

0 commit comments

Comments
 (0)