@@ -27,14 +27,8 @@ import { supportedLocales, translations, useI18n } from '@/i18n'
2727import '@edgeandnode/gds/style.css'
2828import '@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- / ^ (?: \/ (? ! \/ ) | (?: (?: h t t p s ? : ) ? \/ \/ (?: (?: w w w | s t a g i n g ) \. ) ? t h e g r a p h \. c o m ) (?: $ | \/ | \? ) ) ( .+ ) ? / i
35-
36- // Match URLs that start with a protocol/scheme or `//`
37- const absoluteUrlRegex = / ^ (?: [ a - z A - Z ] [ a - z A - Z \d + . - ] * : | \/ \/ ) /
30+ const internalAbsoluteHrefRegex = / ^ ( ( ( h t t p s ? : ) ? \/ \/ ( ( w w w | s t a g i n g ) \. ) ? t h e g r a p h \. c o m ) ? \/ d o c s \/ | \/ (? ! \/ ) ) / i
31+ const externalHrefRegex = / ^ (? ! ( h t t p s ? : ) ? \/ \/ ( ( w w w | s t a g i n g ) \. ) ? t h e g r a p h \. c o m ) ( [ a - z A - Z 0 - 9 + . - ] + : ) ? \/ \/ / i
3832
3933const 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