From 91ac4208d25bd782a38c5a5d14233547efd36119 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pavel=20Zbytovsk=C3=BD?= Date: Thu, 19 Dec 2024 17:28:54 +0100 Subject: [PATCH] general: fix lang links for bots (vercel bug) (#848) --- pages/_document.tsx | 29 ++++---------- src/components/FeaturePanel/ParentLink.tsx | 4 +- src/helpers/LangLinks.tsx | 44 ++++++++++++++++++++++ 3 files changed, 54 insertions(+), 23 deletions(-) create mode 100644 src/helpers/LangLinks.tsx diff --git a/pages/_document.tsx b/pages/_document.tsx index d5e7b5df..1e64fdd0 100644 --- a/pages/_document.tsx +++ b/pages/_document.tsx @@ -18,9 +18,9 @@ import { InjectIntl, setIntl } from '../src/services/intl'; import { FaviconsOsmapp } from '../src/helpers/FaviconsOsmapp'; import { PROJECT_ID, setProjectForSSR } from '../src/services/project'; import { FaviconsOpenClimbing } from '../src/helpers/FaviconsOpenClimbing'; -import { LANGUAGES } from '../src/config.mjs'; import styled from '@emotion/styled'; import { logRequest } from '../src/server/logRequest'; +import { getUrlForLangLinks, LangLinks } from '../src/helpers/LangLinks'; const Body = styled.body` @media (prefers-color-scheme: light) { @@ -31,17 +31,18 @@ const Body = styled.body` } `; -type Props = DocumentInitialProps & - DocumentProps & +type InitialProps = DocumentInitialProps & DocumentHeadTagsProps & { serverIntl: Awaited>; - asPath: string; + urlForLangLinks: string | false; }; +type Props = DocumentProps & InitialProps; + export default class MyDocument extends Document { render() { const isOpenClimbing = PROJECT_ID === 'openclimbing'; - const { serverIntl, asPath, emotionStyleTags } = this.props; + const { serverIntl, urlForLangLinks, emotionStyleTags } = this.props; return ( @@ -66,15 +67,7 @@ export default class MyDocument extends Document { - {/* only for bots - we dont need to change this after SSR: */} - {Object.keys(LANGUAGES).map((lang) => ( - - ))} + {isOpenClimbing ? : } {/* */} @@ -89,12 +82,6 @@ export default class MyDocument extends Document { } } -type InitialProps = DocumentInitialProps & - DocumentHeadTagsProps & { - serverIntl: Awaited>; - asPath: string | undefined; - }; - MyDocument.getInitialProps = async ( ctx: DocumentContext, ): Promise => { @@ -110,6 +97,6 @@ MyDocument.getInitialProps = async ( return { ...initialProps, serverIntl, - asPath: ctx.asPath, + urlForLangLinks: getUrlForLangLinks(ctx), }; }; diff --git a/src/components/FeaturePanel/ParentLink.tsx b/src/components/FeaturePanel/ParentLink.tsx index 493cb93d..3fe59d30 100644 --- a/src/components/FeaturePanel/ParentLink.tsx +++ b/src/components/FeaturePanel/ParentLink.tsx @@ -57,12 +57,12 @@ export const ParentLinkContent = () => { {hasMoreParents ? ( {feature.parentFeatures?.map((parentFeature, i) => ( - <> + {getLabel(parentFeature)} {feature.parentFeatures.length > i + 1 && ,} - + ))} ) : ( diff --git a/src/helpers/LangLinks.tsx b/src/helpers/LangLinks.tsx new file mode 100644 index 00000000..dc35bbdd --- /dev/null +++ b/src/helpers/LangLinks.tsx @@ -0,0 +1,44 @@ +import { LANGUAGES } from '../config.mjs'; +import React from 'react'; +import type { DocumentContext } from 'next/dist/shared/lib/utils'; + +export const getUrlForLangLinks = (ctx: DocumentContext) => { + // NOTE: there are two bugs in vercel deployments + // 1) the asPath contains the lang prefix, even though it shouldn't + // 2) sometimes it adds query like ?nxtPall=node/11580044107 + // Related issue - even though it is closed: https://github.com/vercel/next.js/issues/36275 + + const fixedPath = ctx.asPath + .replace(/\?nxtPall=.*$/, '') + .replace(/^\/[a-z]{2}(\/|$)/, '$1'); + + if (fixedPath === '/' || fixedPath === '') { + return ''; + } + + if (fixedPath.match(/^\/(node|way|relation)\/\d+$/)) { + return fixedPath; + } + + return false; + // Test cases: /, /node/6, /en, /en/node/6 +}; + +type Props = { + urlForLangLinks: string | false; +}; + +export const LangLinks = ({ urlForLangLinks }: Props) => + urlForLangLinks === false ? null : ( + <> + {/* only for bots - we dont need to change this after SSR: */} + {Object.keys(LANGUAGES).map((lang) => ( + + ))} + + );