Skip to content

Commit a3ac8ed

Browse files
authored
fix(open graph images): added full url, file extension, and switch from query params to paths (#1191)
* fix: add full url to open graph images * fix double slash * add file extension * add .png to url * update baseUrl * fix branch name * fix * fix env variable name * fix env name * fix .env * more space for X * add content type header * remove headers * encode * switch to paths instead of params * cleanup * remove center
1 parent 0848236 commit a3ac8ed

5 files changed

Lines changed: 37 additions & 12 deletions

File tree

.github/workflows/deploy.yml

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,15 +31,32 @@ jobs:
3131
run: yarn install
3232
- name: Sync playground bundles
3333
run: yarn build:sync-bundles
34+
- name: Set VITE_DEPLOYMENT_URL
35+
shell: bash
36+
run: |
37+
RAW_BRANCH="${{ github.head_ref || github.ref_name }}"
38+
39+
if [[ "$RAW_BRANCH" == "master" ]]; then
40+
echo "VITE_DEPLOYMENT_URL=" >> "$GITHUB_ENV"
41+
else
42+
SAFE_BRANCH="${RAW_BRANCH//\//-}"
43+
44+
SAFE_BRANCH=$(echo "$SAFE_BRANCH" | tr '[:upper:]' '[:lower:]')
45+
46+
echo "SAFE_BRANCH=$SAFE_BRANCH" >> "$GITHUB_ENV"
47+
echo "VITE_DEPLOYMENT_URL=https://${SAFE_BRANCH}.rescript-lang.pages.dev" >> "$GITHUB_ENV"
48+
fi
3449
- name: Build
3550
run: yarn build
51+
env:
52+
VITE_DEPLOYMENT_URL: ${{ env.VITE_DEPLOYMENT_URL }}
3653
- name: Deploy
3754
id: deploy
3855
uses: cloudflare/wrangler-action@v3
3956
with:
4057
apiToken: ${{ secrets.CLOUDFLARE_API_TOKEN }}
4158
accountId: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }}
42-
command: pages deploy out --project-name=rescript-lang-org
59+
command: pages deploy out --project-name=rescript-lang-org --branch=${{ env.SAFE_BRANCH }}
4360
gitHubToken: ${{ secrets.GITHUB_TOKEN }}
4461
wranglerVersion: 4.63.0
4562
env:

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,4 +66,4 @@ functions/**/*.jsx
6666
_scripts
6767

6868
# Local env files
69-
.env
69+
.env.local
Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,15 @@ let loadGoogleFont = async (family: string) => {
1313
await response->Response.arrayBuffer
1414
}
1515

16-
type context = {request: FetchAPI.request}
16+
type context = {request: FetchAPI.request, params: {path: array<string>}}
1717

18-
let onRequest = async ({request}: context) => {
19-
let url = WebAPI.URL.make(~url=request.url)
20-
let title = url.searchParams->URLSearchParams.get("title")
21-
let descripton = url.searchParams->URLSearchParams.get("description")
18+
let onRequest = async ({params}: context) => {
19+
Console.log(params.path)
20+
21+
let title = params.path[0]->Option.getOr("ReScript")->decodeURIComponent
22+
// let url = WebAPI.URL.make(~url=request.url)
23+
// let title = url.searchParams->URLSearchParams.get("title")
24+
let descripton = params.path[1]->Option.getOr("ReScript")->decodeURIComponent
2225

2326
// we want to split the title if it contains a |
2427
let (title, subTitle) = {
@@ -36,11 +39,10 @@ let onRequest = async ({request}: context) => {
3639
color: "#efefef",
3740
display: "flex",
3841
flexDirection: "column",
39-
justifyContent: "center",
4042
alignItems: "flex-start",
4143
textAlign: "left",
4244
position: "relative",
43-
padding: "60px",
45+
padding: "0 60px",
4446
boxSizing: "border-box",
4547
}}
4648
>
@@ -93,6 +95,7 @@ let onRequest = async ({request}: context) => {
9395
opacity: "0.9",
9496
maxWidth: "900px",
9597
textWrap: "balance",
98+
// extra space since X wants to overlay the text
9699
}}
97100
>
98101
{React.string(descripton)}

src/bindings/Env.res

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
external dev: bool = "import.meta.env.DEV"
33

44
// Cloudflare deployment URL
5-
external deployment_url: option<string> = "import.meta.env.DEPLOYMENT_URL"
5+
external deployment_url: option<string> = "import.meta.env.VITE_DEPLOYMENT_URL"
66

77
// the root url of the site, e.g. "https://rescript-lang.org/" or "http://localhost:5173/"
88
let root_url = switch deployment_url {

src/common/Util.res

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,13 @@ module Url = {
6060
}
6161
}
6262

63-
let makeOpenGraphImageUrl = (title, description) =>
64-
`/ogimage?title=${title}&description=${description}`
63+
let makeOpenGraphImageUrl = (title, description) => {
64+
let baseUrl = Env.deployment_url->Option.getOr(Env.root_url)
65+
Console.log(baseUrl)
66+
`${baseUrl}${baseUrl->Stdlib.String.endsWith("/") ? "" : "/"}ogimage/${encodeURIComponent(
67+
title,
68+
)}/${encodeURIComponent(description)}/index.png`
69+
}
6570
}
6671

6772
module Date = {

0 commit comments

Comments
 (0)