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

rewrite "external" urls pointing to the current domain into internal urls #719

Merged
merged 3 commits into from
Jan 30, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading