Skip to content
This repository has been archived by the owner on Oct 30, 2024. It is now read-only.

Commit

Permalink
Merge pull request #719 from apollographql/pr/externalUrlCheck
Browse files Browse the repository at this point in the history
rewrite "external" urls pointing to the current domain into internal urls
  • Loading branch information
jerelmiller authored Jan 30, 2024
2 parents 1279571 + 2c3492c commit c581d57
Showing 1 changed file with 20 additions and 1 deletion.
21 changes: 20 additions & 1 deletion src/components/RelativeLink.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import React, {useContext} from 'react';
import {Button, Link} from '@chakra-ui/react';
import {Link as GatsbyLink, graphql, useStaticQuery} from 'gatsby';
import {PathContext, isUrl} from '../utils';
import {isAbsolute, resolve} from 'path';
import {isAbsolute, relative, resolve} from 'path';

export const PrimaryLink = props => (
<Link
Expand Down Expand Up @@ -33,6 +33,25 @@ function useLinkProps(href) {
return null;
}

if (process.env.CONTEXT !== 'production') {
// fix up absolute urls pointing to the docs
// for netlify previews and localhost
href = href.replace(
'https://www.apollographql.com/docs',
process.env.DEPLOY_URL
);
}

try {
// convert full urls for the current domain into absolute domain-relative urls
const url = new URL(href);
if (url.host === window.location.host) {
href = url.pathname;
}
} catch {
// it's okay if this fails, then it probably wasn't a full url
}

const isExternal = isUrl(href);
const isHash = href.startsWith('#');
const isFile = /\.[a-z]+$/.test(href);
Expand Down

0 comments on commit c581d57

Please sign in to comment.