Skip to content

Commit 3d8c0e6

Browse files
committed
Revert "Keep current origin when clicking on (staging.)thegraph.com links (#860)"
This reverts commit db4fd82. # Conflicts: # website/next.config.js
1 parent 4811a66 commit 3d8c0e6

File tree

10 files changed

+32
-52
lines changed

10 files changed

+32
-52
lines changed

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

-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@ jobs:
3737
cache-to: type=inline
3838
build-args: |
3939
ENVIRONMENT=production
40-
ORIGIN=https://thegraph.com
4140
tags: |
4241
${{ env.BASE_IMAGE }}:${{ github.sha }}
4342
${{ env.BASE_IMAGE }}:latest

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

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

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

-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@ jobs:
3838
cache-to: type=inline
3939
build-args: |
4040
ENVIRONMENT=staging
41-
ORIGIN=https://staging.thegraph.com
4241
tags: |
4342
${{ env.BASE_IMAGE }}:${{ github.sha }}
4443
${{ env.BASE_IMAGE }}:latest

Dockerfile

-3
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
11
FROM node:20-alpine as builder
22

33
ARG ENVIRONMENT
4-
ARG ORIGIN
5-
64
ENV ENVIRONMENT=$ENVIRONMENT
7-
ENV ORIGIN=$ORIGIN
85

96
ENV PNPM_HOME="/usr/bin"
107

package.json

+1-1
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 --build-arg ORIGIN=$ORIGIN",
8+
"docker:build": "source ./website/.env.local && DOCKER_BUILDKIT=1 docker build . -t docs --no-cache --build-arg ENVIRONMENT=$ENVIRONMENT",
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

+1-1
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.6"
58+
"tsup": "^8.3.5"
5959
},
6060
"sideEffects": false
6161
}

pnpm-lock.yaml

+15-15
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

website/.env

-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11
ENVIRONMENT=local
2-
ORIGIN=http://localhost:3000
3-
BASE_PATH=/docs
42
ALGOLIA_API_KEY=9a358df50b02a5b66efeecbc0a2cab3d
53
ALGOLIA_APP_ID=WQ5FYJCL00
64
SITE_URL=https://thegraph.com/docs

website/next.config.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@ import nextra from 'nextra'
22

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

website/src/_app.tsx

+14-25
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,8 @@ import { supportedLocales, translations, useI18n } from '@/i18n'
2727
import '@edgeandnode/gds/style.css'
2828
import '@docsearch/css'
2929

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+.-]*:|\/\/)/
30+
const internalAbsoluteHrefRegex = /^(((https?:)?\/\/((www|staging)\.)?thegraph\.com)?\/docs\/|\/(?!\/))/i
31+
const externalHrefRegex = /^(?!(https?:)?\/\/((www|staging)\.)?thegraph\.com)([a-zA-Z0-9+.-]+:)?\/\//i
3832

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

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

8175
let { href, target } = props as ButtonOrLinkProps.ExternalLinkProps
8276

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'
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'
10089
}
10190

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

0 commit comments

Comments
 (0)