Skip to content

Commit

Permalink
Revert "Keep current origin when clicking on (staging.)thegraph.com l…
Browse files Browse the repository at this point in the history
…inks (#860)"

This reverts commit db4fd82.

# Conflicts:
#	website/next.config.js
  • Loading branch information
benface committed Jan 28, 2025
1 parent 4811a66 commit 3d8c0e6
Show file tree
Hide file tree
Showing 10 changed files with 32 additions and 52 deletions.
1 change: 0 additions & 1 deletion .github/workflows/ci-cd-production.yml
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ jobs:
cache-to: type=inline
build-args: |
ENVIRONMENT=production
ORIGIN=https://thegraph.com
tags: |
${{ env.BASE_IMAGE }}:${{ github.sha }}
${{ env.BASE_IMAGE }}:latest
Expand Down
1 change: 0 additions & 1 deletion .github/workflows/ci-cd-pull-request.yml
Original file line number Diff line number Diff line change
Expand Up @@ -107,5 +107,4 @@ jobs:
cache-from: type=registry,ref=${{ env.BASE_IMAGE }}:latest
build-args: |
ENVIRONMENT=staging
ORIGIN=https://staging.thegraph.com
push: false
1 change: 0 additions & 1 deletion .github/workflows/ci-cd-staging.yml
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ jobs:
cache-to: type=inline
build-args: |
ENVIRONMENT=staging
ORIGIN=https://staging.thegraph.com
tags: |
${{ env.BASE_IMAGE }}:${{ github.sha }}
${{ env.BASE_IMAGE }}:latest
Expand Down
3 changes: 0 additions & 3 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
FROM node:20-alpine as builder

ARG ENVIRONMENT
ARG ORIGIN

ENV ENVIRONMENT=$ENVIRONMENT
ENV ORIGIN=$ORIGIN

ENV PNPM_HOME="/usr/bin"

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"scripts": {
"dev": "turbo run dev --parallel",
"build": "NODE_OPTIONS='--max_old_space_size=4096' turbo run build",
"docker:build": "source ./website/.env.local && DOCKER_BUILDKIT=1 docker build . -t docs --no-cache --build-arg ENVIRONMENT=$ENVIRONMENT --build-arg ORIGIN=$ORIGIN",
"docker:build": "source ./website/.env.local && DOCKER_BUILDKIT=1 docker build . -t docs --no-cache --build-arg ENVIRONMENT=$ENVIRONMENT",
"docker:clean": "docker builder prune",
"docker:up": "docker run --rm -it -p 3000:80 -v \"$(pwd)/nginx.conf:/etc/nginx/nginx.conf\" docs",
"check": "pnpm typecheck && pnpm lint && pnpm prettier:check",
Expand Down
2 changes: 1 addition & 1 deletion packages/nextra-theme/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@
"react": "^18.3.1",
"react-dom": "^18.3.1",
"theme-ui": "^0.17.1",
"tsup": "^8.3.6"
"tsup": "^8.3.5"
},
"sideEffects": false
}
30 changes: 15 additions & 15 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 0 additions & 2 deletions website/.env
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
ENVIRONMENT=local
ORIGIN=http://localhost:3000
BASE_PATH=/docs
ALGOLIA_API_KEY=9a358df50b02a5b66efeecbc0a2cab3d
ALGOLIA_APP_ID=WQ5FYJCL00
SITE_URL=https://thegraph.com/docs
3 changes: 1 addition & 2 deletions website/next.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@ import nextra from 'nextra'

const env = {
ENVIRONMENT: process.env.ENVIRONMENT,
ORIGIN: process.env.ORIGIN,
BASE_PATH: process.env.BASE_PATH ?? '/docs',
BASE_PATH: process.env.NODE_ENV === 'production' ? '/docs' : '',
ALGOLIA_API_KEY: process.env.ALGOLIA_API_KEY,
ALGOLIA_APP_ID: process.env.ALGOLIA_APP_ID,
MIXPANEL_TOKEN:
Expand Down
39 changes: 14 additions & 25 deletions website/src/_app.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,8 @@ import { supportedLocales, translations, useI18n } from '@/i18n'
import '@edgeandnode/gds/style.css'
import '@docsearch/css'

// Match either:
// 1. URLs that start with `/` followed by an optional path or query (root-relative URLs)
// 2. URLs that start with `http(s)://(www.|staging.)thegraph.com`, followed by an optional path/query
const rootRelativeOrTheGraphUrlRegex =
/^(?:\/(?!\/)|(?:(?:https?:)?\/\/(?:(?:www|staging)\.)?thegraph\.com)(?:$|\/|\?))(.+)?/i

// Match URLs that start with a protocol/scheme or `//`
const absoluteUrlRegex = /^(?:[a-zA-Z][a-zA-Z\d+.-]*:|\/\/)/
const internalAbsoluteHrefRegex = /^(((https?:)?\/\/((www|staging)\.)?thegraph\.com)?\/docs\/|\/(?!\/))/i
const externalHrefRegex = /^(?!(https?:)?\/\/((www|staging)\.)?thegraph\.com)([a-zA-Z0-9+.-]+:)?\/\//i

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

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

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

const matches = rootRelativeOrTheGraphUrlRegex.exec(href)
if (matches?.length) {
const path = matches[1] ? (matches[1].startsWith('/') ? matches[1] : `/${matches[1]}`) : '/'
const basePath = process.env.BASE_PATH ?? ''
if (path === basePath || path.startsWith(`${basePath}/`)) {
// If the link is a root-relative URL (or an absolute but internal URL), ensure it is relative to the base path
href = path.substring(basePath.length) || '/'
// Also ensure the link includes a locale
const { locale: pathLocale, pathWithoutLocale } = extractLocaleFromPath(href, supportedLocales)
href = `/${pathLocale ?? locale ?? defaultLocale}${pathWithoutLocale}`
} else if (process.env.ORIGIN && rootRelativeOrTheGraphUrlRegex.test(process.env.ORIGIN)) {
// If the link is an absolute URL under (staging.)thegraph.com, ensure we don't switch between staging and production
href = `${process.env.ORIGIN}${path}`
}
} else if (absoluteUrlRegex.test(href)) {
// If the link is an external URL, default the target to `_blank`
target ??= '_blank'
// If the link is internal and absolute, ensure `href` is relative to the base path (i.e. starts with `/`,
// not `/docs/` or `https://...`) and includes a locale (by prepending the current locale if there is none)
const internalAbsoluteHrefMatches = internalAbsoluteHrefRegex.exec(href)
if (internalAbsoluteHrefMatches) {
href = href.substring(internalAbsoluteHrefMatches[0].length - 1)
const { locale: pathLocale, pathWithoutLocale } = extractLocaleFromPath(href, supportedLocales)
href = `/${pathLocale ?? locale ?? defaultLocale}${pathWithoutLocale}`
}

// If the link is external, default the target to `_blank`
if (externalHrefRegex.test(href)) {
target = target ?? '_blank'
}

return { ...props, href, target }
Expand Down

0 comments on commit 3d8c0e6

Please sign in to comment.