From 3d8c0e6e94ba6617c399989c0a787c2957e8185d Mon Sep 17 00:00:00 2001 From: benface Date: Mon, 27 Jan 2025 21:57:50 -0500 Subject: [PATCH] Revert "Keep current origin when clicking on (staging.)thegraph.com links (#860)" This reverts commit db4fd8210244907c1b30564825cb7f0ff387743d. # Conflicts: # website/next.config.js --- .github/workflows/ci-cd-production.yml | 1 - .github/workflows/ci-cd-pull-request.yml | 1 - .github/workflows/ci-cd-staging.yml | 1 - Dockerfile | 3 -- package.json | 2 +- packages/nextra-theme/package.json | 2 +- pnpm-lock.yaml | 30 +++++++++--------- website/.env | 2 -- website/next.config.js | 3 +- website/src/_app.tsx | 39 +++++++++--------------- 10 files changed, 32 insertions(+), 52 deletions(-) diff --git a/.github/workflows/ci-cd-production.yml b/.github/workflows/ci-cd-production.yml index 3ca7c781c007..173066125360 100644 --- a/.github/workflows/ci-cd-production.yml +++ b/.github/workflows/ci-cd-production.yml @@ -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 diff --git a/.github/workflows/ci-cd-pull-request.yml b/.github/workflows/ci-cd-pull-request.yml index 8fb9209b7c26..b6bc12b206e0 100644 --- a/.github/workflows/ci-cd-pull-request.yml +++ b/.github/workflows/ci-cd-pull-request.yml @@ -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 diff --git a/.github/workflows/ci-cd-staging.yml b/.github/workflows/ci-cd-staging.yml index 3fc8a8596875..09740ae54b47 100644 --- a/.github/workflows/ci-cd-staging.yml +++ b/.github/workflows/ci-cd-staging.yml @@ -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 diff --git a/Dockerfile b/Dockerfile index c2138875e68c..cc1a50314b14 100644 --- a/Dockerfile +++ b/Dockerfile @@ -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" diff --git a/package.json b/package.json index 2e929da5046c..ed02f717e97a 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/packages/nextra-theme/package.json b/packages/nextra-theme/package.json index 7f334dee5e20..7f74361d61ca 100644 --- a/packages/nextra-theme/package.json +++ b/packages/nextra-theme/package.json @@ -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 } diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 714ade547366..6091afdcba5a 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -106,8 +106,8 @@ importers: specifier: ^0.17.1 version: 0.17.1(@emotion/react@11.14.0(@types/react@18.3.18)(react@18.3.1))(react@18.3.1) tsup: - specifier: ^8.3.6 - version: 8.3.6(jiti@1.21.7)(postcss@8.5.1)(tsx@4.19.2)(typescript@5.7.3)(yaml@2.7.0) + specifier: ^8.3.5 + version: 8.3.5(jiti@1.21.7)(postcss@8.5.1)(tsx@4.19.2)(typescript@5.7.3)(yaml@2.7.0) packages/og-image: dependencies: @@ -3719,11 +3719,11 @@ packages: peerDependencies: tailwindcss: '>=3.2.0' - '@tanem/react-nprogress@5.0.55': - resolution: {integrity: sha512-Y9Ywyr/5T7P8PB19DOoIHmTfDAFJ07lEo/KsGBAuHnWMP6XGgAnxzakfLR9j+mxPP3aFP464Hl4/WQsY0MfIgg==} + '@tanem/react-nprogress@5.0.53': + resolution: {integrity: sha512-leYKdK85/MSFz2WdcZKMI6Xaq63U06V6ERk1H+ovPPIWeVemK3HIBPb3O3Hsee9RliV5kolcM06epcuY36q0Dw==} peerDependencies: - react: ^16.8.0 || ^17.0.0 || ^18.0.0 || ^19.0.0 - react-dom: ^16.8.0 || ^17.0.0 || ^18.0.0 || ^19.0.0 + react: ^16.8.0 || ^17.0.0 || ^18.0.0 + react-dom: ^16.8.0 || ^17.0.0 || ^18.0.0 '@tanstack/react-virtual@3.11.2': resolution: {integrity: sha512-OuFzMXPF4+xZgx8UzJha0AieuMihhhaWG0tCqpp6tDzlFwOmNBPYMuLOtMJ1Tr4pXLHmgjcWhG6RlknY2oNTdQ==} @@ -6081,8 +6081,8 @@ packages: graphemer@1.4.0: resolution: {integrity: sha512-EtKwoO6kxCL9WO5xipiHTZlSzBm7WLT627TqC/uVRd0HKmq8NXyebnNYxDoBi7wt8eTWrUrKXCOVaFq9x1kgag==} - graphemesplit@2.5.1: - resolution: {integrity: sha512-KWxi3NIPD6HxC8rXHHV7FpN48XvrWUPS7yrTVxNFG08o298FdSsGd012NuWHxQJ3IaH1wcnSVoY56mZxpl4T6A==} + graphemesplit@2.4.4: + resolution: {integrity: sha512-lKrpp1mk1NH26USxC/Asw4OHbhSQf5XfrWZ+CDv/dFVvd1j17kFgMotdJvOesmHkbFX9P9sBfpH8VogxOWLg8w==} graphql-import-node@0.0.5: resolution: {integrity: sha512-OXbou9fqh9/Lm7vwXT0XoRN9J5+WCYKnbiTalgFDvkQERITRmcfncZs6aVABedd5B85yQU5EULS4a5pnbpuI0Q==} @@ -9310,8 +9310,8 @@ packages: tsort@0.0.1: resolution: {integrity: sha512-Tyrf5mxF8Ofs1tNoxA13lFeZ2Zrbd6cKbuH3V+MQ5sb6DtBj5FjrXVsRWT8YvNAQTqNoz66dz1WsbigI22aEnw==} - tsup@8.3.6: - resolution: {integrity: sha512-XkVtlDV/58S9Ye0JxUUTcrQk4S+EqlOHKzg6Roa62rdjL1nGWNUstG0xgI4vanHdfIpjP448J8vlN0oK6XOJ5g==} + tsup@8.3.5: + resolution: {integrity: sha512-Tunf6r6m6tnZsG9GYWndg0z8dEV7fD733VBFzFJ5Vcm1FtlXB8xBD/rtrBi2a3YKEV7hHtxiZtW5EAVADoe1pA==} engines: {node: '>=18'} hasBin: true peerDependencies: @@ -10715,7 +10715,7 @@ snapshots: '@radix-ui/react-tooltip': 1.1.7(@types/react-dom@18.3.5(@types/react@18.3.18))(@types/react@18.3.18)(react-dom@18.3.1(react@18.3.1))(react@18.3.1) '@react-aria/utils': 3.27.0(react-dom@18.3.1(react@18.3.1))(react@18.3.1) '@tailwindcss/container-queries': 0.1.1(tailwindcss@3.4.17(ts-node@10.9.2(@types/node@22.10.10)(typescript@5.7.3))) - '@tanem/react-nprogress': 5.0.55(react-dom@18.3.1(react@18.3.1))(react@18.3.1) + '@tanem/react-nprogress': 5.0.53(react-dom@18.3.1(react@18.3.1))(react@18.3.1) '@theme-ui/css': 0.17.1(@emotion/react@11.14.0(@types/react@18.3.18)(react@18.3.1)) '@theme-ui/match-media': 0.17.1(@theme-ui/core@0.17.1(@emotion/react@11.14.0(@types/react@18.3.18)(react@18.3.1))(react@18.3.1))(@theme-ui/css@0.17.1(@emotion/react@11.14.0(@types/react@18.3.18)(react@18.3.1)))(react@18.3.1) '@web3icons/react': 3.16.0(react@18.3.1)(typescript@5.7.3) @@ -14704,7 +14704,7 @@ snapshots: dependencies: tailwindcss: 3.4.17(ts-node@10.9.2(@types/node@22.10.10)(typescript@5.7.3)) - '@tanem/react-nprogress@5.0.55(react-dom@18.3.1(react@18.3.1))(react@18.3.1)': + '@tanem/react-nprogress@5.0.53(react-dom@18.3.1(react@18.3.1))(react@18.3.1)': dependencies: '@babel/runtime': 7.26.7 hoist-non-react-statics: 3.3.2 @@ -17335,7 +17335,7 @@ snapshots: fast-fuzzy@1.12.0: dependencies: - graphemesplit: 2.5.1 + graphemesplit: 2.4.4 fast-glob@3.3.3: dependencies: @@ -17744,7 +17744,7 @@ snapshots: graphemer@1.4.0: {} - graphemesplit@2.5.1: + graphemesplit@2.4.4: dependencies: js-base64: 3.7.7 unicode-trie: 2.0.0 @@ -21794,7 +21794,7 @@ snapshots: tsort@0.0.1: {} - tsup@8.3.6(jiti@1.21.7)(postcss@8.5.1)(tsx@4.19.2)(typescript@5.7.3)(yaml@2.7.0): + tsup@8.3.5(jiti@1.21.7)(postcss@8.5.1)(tsx@4.19.2)(typescript@5.7.3)(yaml@2.7.0): dependencies: bundle-require: 5.1.0(esbuild@0.24.2) cac: 6.7.14 diff --git a/website/.env b/website/.env index f655c2fb16fc..a8f62a06ecb9 100644 --- a/website/.env +++ b/website/.env @@ -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 diff --git a/website/next.config.js b/website/next.config.js index 6db2a72e792d..29b90667d7c3 100644 --- a/website/next.config.js +++ b/website/next.config.js @@ -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: diff --git a/website/src/_app.tsx b/website/src/_app.tsx index 16d41660241e..4dd5b1662208 100644 --- a/website/src/_app.tsx +++ b/website/src/_app.tsx @@ -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) @@ -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 }