Skip to content

Commit

Permalink
fix root url again
Browse files Browse the repository at this point in the history
  • Loading branch information
zbycz committed Dec 19, 2024
1 parent 38888b0 commit b808e7d
Showing 1 changed file with 11 additions and 7 deletions.
18 changes: 11 additions & 7 deletions src/helpers/LangLinks.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,25 @@ import React from 'react';
import type { DocumentContext } from 'next/dist/shared/lib/utils';

export const getUrlForLangLinks = (ctx: DocumentContext) => {
// NOTE: there is bug in vercel deployment – the asPath contains the lang prefix, even though it shouldn't
// NOTE: there is bug in vercel deployments – the asPath contains the lang prefix, even though it shouldn't
// Here we make sure the lang prefix is removed.
const rootOrFeatureDetail = ctx.asPath.match(
/^(?:\/[a-z]{2})?(\/|\/(?:node|way|relation)\/\d+)?$/,
);
// Related issue - even though it is closed: https://github.com/vercel/next.js/issues/36275

const fixedPath = ctx.asPath.replace(/^\/[a-z]{2}(\/|^)/, '$1');
if (fixedPath === '/') {
return '';
}

return rootOrFeatureDetail ? rootOrFeatureDetail[1] : false;
const matches = fixedPath.match(/^\/(?:node|way|relation)\/\d+$/);
return matches ? `${matches[0]}` : false;
};

type Props = {
urlForLangLinks: string | false;
};

export const LangLinks = ({ urlForLangLinks }: Props) =>
urlForLangLinks ? (
urlForLangLinks === false ? null : (
<>
{/* only for bots - we dont need to change this after SSR: */}
{Object.keys(LANGUAGES).map((lang) => (
Expand All @@ -29,4 +33,4 @@ export const LangLinks = ({ urlForLangLinks }: Props) =>
/>
))}
</>
) : null;
);

0 comments on commit b808e7d

Please sign in to comment.