Skip to content

Commit 7464e75

Browse files
authored
Keep current origin when clicking on (staging.)thegraph.com links (fixed) (#864)
* Debug adding `ORIGIN` and `BASE_PATH` env vars * Ok, that seems to work, what about this? * Still works, what about this? * Still works, so… it’s this? * Ooooooooh this should fix it 🤦‍♂️
1 parent f1ed4f6 commit 7464e75

File tree

11 files changed

+62
-20
lines changed

11 files changed

+62
-20
lines changed

.github/workflows/ci-cd-production.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ jobs:
3737
cache-to: type=inline
3838
build-args: |
3939
ENVIRONMENT=production
40+
ORIGIN=https://thegraph.com
4041
tags: |
4142
${{ env.BASE_IMAGE }}:${{ github.sha }}
4243
${{ env.BASE_IMAGE }}:latest

.github/workflows/ci-cd-pull-request.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,4 +107,5 @@ jobs:
107107
cache-from: type=registry,ref=${{ env.BASE_IMAGE }}:latest
108108
build-args: |
109109
ENVIRONMENT=staging
110+
ORIGIN=https://staging.thegraph.com
110111
push: false

.github/workflows/ci-cd-staging.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ jobs:
3838
cache-to: type=inline
3939
build-args: |
4040
ENVIRONMENT=staging
41+
ORIGIN=https://staging.thegraph.com
4142
tags: |
4243
${{ env.BASE_IMAGE }}:${{ github.sha }}
4344
${{ env.BASE_IMAGE }}:latest

Dockerfile

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
FROM node:20-alpine as builder
22

33
ARG ENVIRONMENT
4+
ARG ORIGIN
5+
46
ENV ENVIRONMENT=$ENVIRONMENT
7+
ENV ORIGIN=$ORIGIN
58

69
ENV PNPM_HOME="/usr/bin"
710

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
"scripts": {
66
"dev": "turbo run dev --parallel",
77
"build": "NODE_OPTIONS='--max_old_space_size=4096' turbo run build",
8-
"docker:build": "source ./website/.env.local && DOCKER_BUILDKIT=1 docker build . -t docs --no-cache --build-arg ENVIRONMENT=$ENVIRONMENT",
8+
"docker:build": "source ./website/.env.local && DOCKER_BUILDKIT=1 docker build . -t docs --no-cache --build-arg ENVIRONMENT=$ENVIRONMENT --build-arg ORIGIN=$ORIGIN",
99
"docker:clean": "docker builder prune",
1010
"docker:up": "docker run --rm -it -p 3000:80 -v \"$(pwd)/nginx.conf:/etc/nginx/nginx.conf\" docs",
1111
"check": "pnpm typecheck && pnpm lint && pnpm prettier:check",

website/.env

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
ENVIRONMENT=local
2+
ORIGIN=http://localhost:3000
3+
BASE_PATH=/docs
24
ALGOLIA_API_KEY=9a358df50b02a5b66efeecbc0a2cab3d
35
ALGOLIA_APP_ID=WQ5FYJCL00
4-
SITE_URL=https://thegraph.com/docs

website/next-sitemap.config.mjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/** @type {import('next-sitemap').IConfig} */
22
export default {
3-
siteUrl: process.env.SITE_URL,
3+
siteUrl: '/',
44
generateIndexSitemap: false,
55
}

website/next.config.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@ import nextra from 'nextra'
22

33
const env = {
44
ENVIRONMENT: process.env.ENVIRONMENT,
5-
BASE_PATH: process.env.NODE_ENV === 'production' ? '/docs' : '',
5+
ORIGIN: process.env.ORIGIN,
6+
BASE_PATH: process.env.BASE_PATH,
67
ALGOLIA_API_KEY: process.env.ALGOLIA_API_KEY,
78
ALGOLIA_APP_ID: process.env.ALGOLIA_APP_ID,
89
MIXPANEL_TOKEN:

website/route-lockfile.txt

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
/ar/subgraphs/cookbook/grafting/
3737
/ar/subgraphs/cookbook/immutable-entities-bytes-as-ids/
3838
/ar/subgraphs/cookbook/near/
39+
/ar/subgraphs/cookbook/polymarket/
3940
/ar/subgraphs/cookbook/pruning/
4041
/ar/subgraphs/cookbook/secure-api-keys-nextjs/
4142
/ar/subgraphs/cookbook/subgraph-debug-forking/
@@ -115,6 +116,7 @@
115116
/cs/subgraphs/cookbook/grafting/
116117
/cs/subgraphs/cookbook/immutable-entities-bytes-as-ids/
117118
/cs/subgraphs/cookbook/near/
119+
/cs/subgraphs/cookbook/polymarket/
118120
/cs/subgraphs/cookbook/pruning/
119121
/cs/subgraphs/cookbook/secure-api-keys-nextjs/
120122
/cs/subgraphs/cookbook/subgraph-debug-forking/
@@ -192,6 +194,7 @@
192194
/de/subgraphs/cookbook/grafting/
193195
/de/subgraphs/cookbook/immutable-entities-bytes-as-ids/
194196
/de/subgraphs/cookbook/near/
197+
/de/subgraphs/cookbook/polymarket/
195198
/de/subgraphs/cookbook/pruning/
196199
/de/subgraphs/cookbook/secure-api-keys-nextjs/
197200
/de/subgraphs/cookbook/subgraph-debug-forking/
@@ -271,6 +274,7 @@
271274
/en/subgraphs/cookbook/grafting/
272275
/en/subgraphs/cookbook/immutable-entities-bytes-as-ids/
273276
/en/subgraphs/cookbook/near/
277+
/en/subgraphs/cookbook/polymarket/
274278
/en/subgraphs/cookbook/pruning/
275279
/en/subgraphs/cookbook/secure-api-keys-nextjs/
276280
/en/subgraphs/cookbook/subgraph-debug-forking/
@@ -355,6 +359,7 @@
355359
/es/subgraphs/cookbook/grafting/
356360
/es/subgraphs/cookbook/immutable-entities-bytes-as-ids/
357361
/es/subgraphs/cookbook/near/
362+
/es/subgraphs/cookbook/polymarket/
358363
/es/subgraphs/cookbook/pruning/
359364
/es/subgraphs/cookbook/secure-api-keys-nextjs/
360365
/es/subgraphs/cookbook/subgraph-debug-forking/
@@ -434,6 +439,7 @@
434439
/fr/subgraphs/cookbook/grafting/
435440
/fr/subgraphs/cookbook/immutable-entities-bytes-as-ids/
436441
/fr/subgraphs/cookbook/near/
442+
/fr/subgraphs/cookbook/polymarket/
437443
/fr/subgraphs/cookbook/pruning/
438444
/fr/subgraphs/cookbook/secure-api-keys-nextjs/
439445
/fr/subgraphs/cookbook/subgraph-debug-forking/
@@ -513,6 +519,7 @@
513519
/hi/subgraphs/cookbook/grafting/
514520
/hi/subgraphs/cookbook/immutable-entities-bytes-as-ids/
515521
/hi/subgraphs/cookbook/near/
522+
/hi/subgraphs/cookbook/polymarket/
516523
/hi/subgraphs/cookbook/pruning/
517524
/hi/subgraphs/cookbook/secure-api-keys-nextjs/
518525
/hi/subgraphs/cookbook/subgraph-debug-forking/
@@ -592,6 +599,7 @@
592599
/it/subgraphs/cookbook/grafting/
593600
/it/subgraphs/cookbook/immutable-entities-bytes-as-ids/
594601
/it/subgraphs/cookbook/near/
602+
/it/subgraphs/cookbook/polymarket/
595603
/it/subgraphs/cookbook/pruning/
596604
/it/subgraphs/cookbook/secure-api-keys-nextjs/
597605
/it/subgraphs/cookbook/subgraph-debug-forking/
@@ -671,6 +679,7 @@
671679
/ja/subgraphs/cookbook/grafting/
672680
/ja/subgraphs/cookbook/immutable-entities-bytes-as-ids/
673681
/ja/subgraphs/cookbook/near/
682+
/ja/subgraphs/cookbook/polymarket/
674683
/ja/subgraphs/cookbook/pruning/
675684
/ja/subgraphs/cookbook/secure-api-keys-nextjs/
676685
/ja/subgraphs/cookbook/subgraph-debug-forking/
@@ -748,6 +757,7 @@
748757
/ko/subgraphs/cookbook/grafting/
749758
/ko/subgraphs/cookbook/immutable-entities-bytes-as-ids/
750759
/ko/subgraphs/cookbook/near/
760+
/ko/subgraphs/cookbook/polymarket/
751761
/ko/subgraphs/cookbook/pruning/
752762
/ko/subgraphs/cookbook/secure-api-keys-nextjs/
753763
/ko/subgraphs/cookbook/subgraph-debug-forking/
@@ -827,6 +837,7 @@
827837
/mr/subgraphs/cookbook/grafting/
828838
/mr/subgraphs/cookbook/immutable-entities-bytes-as-ids/
829839
/mr/subgraphs/cookbook/near/
840+
/mr/subgraphs/cookbook/polymarket/
830841
/mr/subgraphs/cookbook/pruning/
831842
/mr/subgraphs/cookbook/secure-api-keys-nextjs/
832843
/mr/subgraphs/cookbook/subgraph-debug-forking/
@@ -904,6 +915,7 @@
904915
/nl/subgraphs/cookbook/grafting/
905916
/nl/subgraphs/cookbook/immutable-entities-bytes-as-ids/
906917
/nl/subgraphs/cookbook/near/
918+
/nl/subgraphs/cookbook/polymarket/
907919
/nl/subgraphs/cookbook/pruning/
908920
/nl/subgraphs/cookbook/secure-api-keys-nextjs/
909921
/nl/subgraphs/cookbook/subgraph-debug-forking/
@@ -981,6 +993,7 @@
981993
/pl/subgraphs/cookbook/grafting/
982994
/pl/subgraphs/cookbook/immutable-entities-bytes-as-ids/
983995
/pl/subgraphs/cookbook/near/
996+
/pl/subgraphs/cookbook/polymarket/
984997
/pl/subgraphs/cookbook/pruning/
985998
/pl/subgraphs/cookbook/secure-api-keys-nextjs/
986999
/pl/subgraphs/cookbook/subgraph-debug-forking/
@@ -1060,6 +1073,7 @@
10601073
/pt/subgraphs/cookbook/grafting/
10611074
/pt/subgraphs/cookbook/immutable-entities-bytes-as-ids/
10621075
/pt/subgraphs/cookbook/near/
1076+
/pt/subgraphs/cookbook/polymarket/
10631077
/pt/subgraphs/cookbook/pruning/
10641078
/pt/subgraphs/cookbook/secure-api-keys-nextjs/
10651079
/pt/subgraphs/cookbook/subgraph-debug-forking/
@@ -1137,6 +1151,7 @@
11371151
/ro/subgraphs/cookbook/grafting/
11381152
/ro/subgraphs/cookbook/immutable-entities-bytes-as-ids/
11391153
/ro/subgraphs/cookbook/near/
1154+
/ro/subgraphs/cookbook/polymarket/
11401155
/ro/subgraphs/cookbook/pruning/
11411156
/ro/subgraphs/cookbook/secure-api-keys-nextjs/
11421157
/ro/subgraphs/cookbook/subgraph-debug-forking/
@@ -1216,6 +1231,7 @@
12161231
/ru/subgraphs/cookbook/grafting/
12171232
/ru/subgraphs/cookbook/immutable-entities-bytes-as-ids/
12181233
/ru/subgraphs/cookbook/near/
1234+
/ru/subgraphs/cookbook/polymarket/
12191235
/ru/subgraphs/cookbook/pruning/
12201236
/ru/subgraphs/cookbook/secure-api-keys-nextjs/
12211237
/ru/subgraphs/cookbook/subgraph-debug-forking/
@@ -1295,6 +1311,7 @@
12951311
/sv/subgraphs/cookbook/grafting/
12961312
/sv/subgraphs/cookbook/immutable-entities-bytes-as-ids/
12971313
/sv/subgraphs/cookbook/near/
1314+
/sv/subgraphs/cookbook/polymarket/
12981315
/sv/subgraphs/cookbook/pruning/
12991316
/sv/subgraphs/cookbook/secure-api-keys-nextjs/
13001317
/sv/subgraphs/cookbook/subgraph-debug-forking/
@@ -1372,6 +1389,7 @@
13721389
/sw/subgraphs/cookbook/grafting/
13731390
/sw/subgraphs/cookbook/immutable-entities-bytes-as-ids/
13741391
/sw/subgraphs/cookbook/near/
1392+
/sw/subgraphs/cookbook/polymarket/
13751393
/sw/subgraphs/cookbook/pruning/
13761394
/sw/subgraphs/cookbook/secure-api-keys-nextjs/
13771395
/sw/subgraphs/cookbook/subgraph-debug-forking/
@@ -1451,6 +1469,7 @@
14511469
/tr/subgraphs/cookbook/grafting/
14521470
/tr/subgraphs/cookbook/immutable-entities-bytes-as-ids/
14531471
/tr/subgraphs/cookbook/near/
1472+
/tr/subgraphs/cookbook/polymarket/
14541473
/tr/subgraphs/cookbook/pruning/
14551474
/tr/subgraphs/cookbook/secure-api-keys-nextjs/
14561475
/tr/subgraphs/cookbook/subgraph-debug-forking/
@@ -1528,6 +1547,7 @@
15281547
/uk/subgraphs/cookbook/grafting/
15291548
/uk/subgraphs/cookbook/immutable-entities-bytes-as-ids/
15301549
/uk/subgraphs/cookbook/near/
1550+
/uk/subgraphs/cookbook/polymarket/
15311551
/uk/subgraphs/cookbook/pruning/
15321552
/uk/subgraphs/cookbook/secure-api-keys-nextjs/
15331553
/uk/subgraphs/cookbook/subgraph-debug-forking/
@@ -1607,6 +1627,7 @@
16071627
/ur/subgraphs/cookbook/grafting/
16081628
/ur/subgraphs/cookbook/immutable-entities-bytes-as-ids/
16091629
/ur/subgraphs/cookbook/near/
1630+
/ur/subgraphs/cookbook/polymarket/
16101631
/ur/subgraphs/cookbook/pruning/
16111632
/ur/subgraphs/cookbook/secure-api-keys-nextjs/
16121633
/ur/subgraphs/cookbook/subgraph-debug-forking/
@@ -1684,6 +1705,7 @@
16841705
/vi/subgraphs/cookbook/grafting/
16851706
/vi/subgraphs/cookbook/immutable-entities-bytes-as-ids/
16861707
/vi/subgraphs/cookbook/near/
1708+
/vi/subgraphs/cookbook/polymarket/
16871709
/vi/subgraphs/cookbook/pruning/
16881710
/vi/subgraphs/cookbook/secure-api-keys-nextjs/
16891711
/vi/subgraphs/cookbook/subgraph-debug-forking/
@@ -1763,6 +1785,7 @@
17631785
/zh/subgraphs/cookbook/grafting/
17641786
/zh/subgraphs/cookbook/immutable-entities-bytes-as-ids/
17651787
/zh/subgraphs/cookbook/near/
1788+
/zh/subgraphs/cookbook/polymarket/
17661789
/zh/subgraphs/cookbook/pruning/
17671790
/zh/subgraphs/cookbook/secure-api-keys-nextjs/
17681791
/zh/subgraphs/cookbook/subgraph-debug-forking/

website/scripts/sitemap-ci.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,7 @@ async function main() {
1515

1616
const d = parser.parse(await fs.readFile(sitemapPath, 'utf8'))
1717

18-
// TODO: `process.env.SITE_URL` never seems to be set here, so it always falls back to the default value
19-
const routes = d.urlset.url.map((url) => url.loc.replace(process.env.SITE_URL || 'https://thegraph.com/docs', ''))
18+
const routes = d.urlset.url.map((url) => url.loc.replace(/^\//, ''))
2019

2120
const redirectsPointingToNonExistingStuff = []
2221

website/src/_app.tsx

Lines changed: 26 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,14 @@ import { supportedLocales, translations, useI18n } from '@/i18n'
2727
import '@edgeandnode/gds/style.css'
2828
import '@docsearch/css'
2929

30-
const internalAbsoluteHrefRegex = /^(((https?:)?\/\/((www|staging)\.)?thegraph\.com)?\/docs\/|\/(?!\/))/i
31-
const externalHrefRegex = /^(?!(https?:)?\/\/((www|staging)\.)?thegraph\.com)([a-zA-Z0-9+.-]+:)?\/\//i
30+
// Match either:
31+
// 1. URLs that start with `/` followed by an optional path or query (root-relative URLs)
32+
// 2. URLs that start with `http(s)://(www.|staging.)thegraph.com`, followed by an optional path/query
33+
const rootRelativeOrTheGraphUrlRegex =
34+
/^(?:\/(?!\/)|(?:(?:https?:)?\/\/(?:(?:www|staging)\.)?thegraph\.com)(?:$|\/|\?))(.+)?/i
35+
36+
// Match URLs that start with a protocol/scheme or `//`
37+
const absoluteUrlRegex = /^(?:[a-zA-Z][a-zA-Z\d+.-]*:|\/\/)/
3238

3339
const removeBasePathFromUrl = (url: string) => url.substring((process.env.BASE_PATH ?? '').length)
3440

@@ -74,18 +80,24 @@ function MyApp({ Component, router, pageProps }: AppProps) {
7480

7581
let { href, target } = props as ButtonOrLinkProps.ExternalLinkProps
7682

77-
// If the link is internal and absolute, ensure `href` is relative to the base path (i.e. starts with `/`,
78-
// not `/docs/` or `https://...`) and includes a locale (by prepending the current locale if there is none)
79-
const internalAbsoluteHrefMatches = internalAbsoluteHrefRegex.exec(href)
80-
if (internalAbsoluteHrefMatches) {
81-
href = href.substring(internalAbsoluteHrefMatches[0].length - 1)
82-
const { locale: pathLocale, pathWithoutLocale } = extractLocaleFromPath(href, supportedLocales)
83-
href = `/${pathLocale ?? locale ?? defaultLocale}${pathWithoutLocale}`
84-
}
85-
86-
// If the link is external, default the target to `_blank`
87-
if (externalHrefRegex.test(href)) {
88-
target = target ?? '_blank'
83+
const matches = rootRelativeOrTheGraphUrlRegex.exec(href)
84+
if (matches?.length) {
85+
const path = matches[1] ? (matches[1].startsWith('/') ? matches[1] : `/${matches[1]}`) : '/'
86+
const basePath = process.env.BASE_PATH ?? ''
87+
const pathIncludesBasePath = path === basePath || path.startsWith(`${basePath}/`)
88+
if (href.startsWith('/') || pathIncludesBasePath) {
89+
// If the link is a root-relative URL or an absolute but internal URL, ensure it is relative to the base path
90+
href = pathIncludesBasePath ? path.substring(basePath.length) : path
91+
// Also ensure the link includes a locale
92+
const { locale: pathLocale, pathWithoutLocale } = extractLocaleFromPath(href, supportedLocales)
93+
href = `/${pathLocale ?? locale ?? defaultLocale}${pathWithoutLocale}`
94+
} else if (process.env.ORIGIN && rootRelativeOrTheGraphUrlRegex.test(process.env.ORIGIN)) {
95+
// If the link is an absolute URL under (staging.)thegraph.com, ensure we don't switch between staging and production
96+
href = `${process.env.ORIGIN}${path}`
97+
}
98+
} else if (absoluteUrlRegex.test(href)) {
99+
// If the link is an external URL, default the target to `_blank`
100+
target ??= '_blank'
89101
}
90102

91103
return { ...props, href, target }

0 commit comments

Comments
 (0)