Skip to content

Commit db4fd82

Browse files
authored
Keep current origin when clicking on (staging.)thegraph.com links (#860)
1 parent 9498702 commit db4fd82

File tree

10 files changed

+52
-32
lines changed

10 files changed

+52
-32
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",

packages/nextra-theme/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@
5555
"react": "^18.3.1",
5656
"react-dom": "^18.3.1",
5757
"theme-ui": "^0.17.1",
58-
"tsup": "^8.3.5"
58+
"tsup": "^8.3.6"
5959
},
6060
"sideEffects": false
6161
}

pnpm-lock.yaml

Lines changed: 15 additions & 15 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

website/.env

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

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/src/_app.tsx

Lines changed: 25 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,23 @@ 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+
if (path === basePath || path.startsWith(`${basePath}/`)) {
88+
// If the link is a root-relative URL (or an absolute but internal URL), ensure it is relative to the base path
89+
href = path.substring(basePath.length) || '/'
90+
// Also ensure the link includes a locale
91+
const { locale: pathLocale, pathWithoutLocale } = extractLocaleFromPath(href, supportedLocales)
92+
href = `/${pathLocale ?? locale ?? defaultLocale}${pathWithoutLocale}`
93+
} else if (process.env.ORIGIN && rootRelativeOrTheGraphUrlRegex.test(process.env.ORIGIN)) {
94+
// If the link is an absolute URL under (staging.)thegraph.com, ensure we don't switch between staging and production
95+
href = `${process.env.ORIGIN}${path}`
96+
}
97+
} else if (absoluteUrlRegex.test(href)) {
98+
// If the link is an external URL, default the target to `_blank`
99+
target ??= '_blank'
89100
}
90101

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

0 commit comments

Comments
 (0)