-
-
Notifications
You must be signed in to change notification settings - Fork 760
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Component using withNamespaces inside next/head crashes the app #251
Comments
Hi @Nelrohd - looks like that error is coming out of |
Seems like Feel free to copy the component code (it's written in TypeScript).import _ from "lodash";
import Head from "next/head";
import { useContext } from "react";
import { format, parse } from "url";
import { TFunction } from "../../i18n";
import { SsrUrlContext } from "../../lib/appContext";
interface Props {
t: TFunction;
title?: string | false;
titleSuffix?: string | false;
description?: string | false;
keywords?: string | false;
keywordsSuffix?: string | false;
canonicalUrl?: string | false;
supportedGetParamsInCanonicalUrl?: string[];
}
const PageMeta = ({
t,
title,
titleSuffix,
description,
keywords,
keywordsSuffix,
canonicalUrl,
supportedGetParamsInCanonicalUrl,
}: Props) => {
const ssrUrl = useContext(SsrUrlContext);
let derivedCanonicalUrl;
if (canonicalUrl) {
derivedCanonicalUrl = canonicalUrl;
} else if (canonicalUrl !== false) {
const { port, query, ...rest } = parse(
typeof window !== "undefined" ? window.location.href : ssrUrl,
true,
);
const cleanedQuery = _.pick(query, supportedGetParamsInCanonicalUrl || []);
derivedCanonicalUrl = format({
...rest,
hostname: undefined,
host: undefined,
protocol: undefined,
search: undefined,
query: _.fromPairs(_.orderBy(_.toPairs(cleanedQuery), (pair) => pair[0])),
hash: undefined,
}).replace("//", "");
}
const derivedTitleSuffix =
titleSuffix !== false ? titleSuffix || t("common:pageTitleSuffix") : "";
const derivedTitle = title !== false ? title || t("pageTitle") : null;
const derivedDescription =
description !== false ? description || t("pageDescription") : undefined;
const derivedKeywordsSuffix =
keywordsSuffix !== false && keywords !== false
? keywordsSuffix || t("common:pageKeywordsSuffix")
: "";
const derivedKeywords =
keywords !== false ? keywords || t("pageKeywords") : null;
return (
<Head>
<title>
{derivedTitle}
{derivedTitleSuffix}
</title>
<meta name="description" content={derivedDescription} />
<meta
name="keywords"
content={`${derivedKeywords}${derivedKeywordsSuffix}`}
/>
{derivedCanonicalUrl ? (
<link rel="canonical" href={derivedCanonicalUrl} />
) : null}
</Head>
);
};
export default PageMeta; On most pages you just use |
That must mean that NextJs is putting the |
@Nelrohd I have a professional project wherein we're localising Head.tsx
_app.tsx
Not sure exactly what is going wrong for you, but I can confirm that this approach works just fine. |
@isaachinman Your example is different from mine. In my example, my component use withNamespaces and is placed inside In your example you create a component with withNamespaces and put its content inside |
Yes I understand that. Clearly NextJs is lifting |
@isaachinman For sure, I already did it before in fact but I wanted to point the issue to help others and maybe have a fix if it's possible. |
I don't think there's going to be a possible fix outside of wrapping as I showed above. It's an acceptable solution and works just fine, though. Happy to continue discussion if anyone feels it's necessary. |
I also faced with that issue. This issue should be pinned in the readme 😄 |
Another solution is to wrap your tags in your component with a import React, { Component } from "react";
import Head from 'next/head';
export const MetaOgTag = withNamespaces("common")(
class extends Component {
render() {
const { title } = this.props;
return (
<Head>
<meta property="og:title" content={t(title)} />
</Head>
)
}
}
); import React from "react";
import { MetaOgTag } from "../components/MetaOgTag";
import Head from "next/head";
export default class Index extends React.Component {
render() {
return (
<Head>
<title>Hello World</title>
</Head>
<MetaOgTag title={"Hello World"} />
);
}
} |
Describe the bug
Using a
MyComponent
withwithNamespaces("common")(MyComponent)
inside<Head>
ofnext/head
triggers an errors:Occurs in next-i18next version
Any version.
Steps to reproduce
withNamespaces
:pages/index.js
inside<head>...</head>
:Expected behaviour
It should not crash.
Screenshots
None.
OS (please complete the following information)
Additional context
It looks like it's not possible to use i18n inside head because something not ready/setup yet.
The text was updated successfully, but these errors were encountered: