Skip to content

Commit e0141b8

Browse files
[blog] Fix hydration mistmatch (mui#34857)
1 parent 49ef62d commit e0141b8

File tree

4 files changed

+16
-12
lines changed

4 files changed

+16
-12
lines changed

docs/pages/_document.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ const GOOGLE_ANALYTICS_ID = PRODUCTION_GA ? 'UA-106598593-2' : 'UA-106598593-3';
3636

3737
export default class MyDocument extends Document {
3838
render() {
39-
const { canonicalAs, userLanguage } = this.props;
39+
const { canonicalAsServer, userLanguage } = this.props;
4040

4141
return (
4242
<Html lang={userLanguage}>
@@ -63,9 +63,11 @@ export default class MyDocument extends Document {
6363
{/* SEO */}
6464
<link
6565
rel="canonical"
66-
href={`https://mui.com${userLanguage === 'en' ? '' : `/${userLanguage}`}${canonicalAs}`}
66+
href={`https://mui.com${
67+
userLanguage === 'en' ? '' : `/${userLanguage}`
68+
}${canonicalAsServer}`}
6769
/>
68-
<link rel="alternate" href={`https://mui.com${canonicalAs}`} hrefLang="x-default" />
70+
<link rel="alternate" href={`https://mui.com${canonicalAsServer}`} hrefLang="x-default" />
6971
{/*
7072
Preconnect allows the browser to setup early connections before an HTTP request
7173
is actually sent to the server.
@@ -233,7 +235,7 @@ MyDocument.getInitialProps = async (ctx) => {
233235

234236
return {
235237
...initialProps,
236-
canonicalAs: pathnameToLanguage(url).canonicalAs,
238+
canonicalAsServer: pathnameToLanguage(url).canonicalAsServer,
237239
userLanguage: ctx.query.userLanguage || 'en',
238240
// Styles fragment is rendered after the app and page rendering finish.
239241
styles: [

docs/src/modules/components/GoogleAnalytics.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,8 @@ function GoogleAnalytics() {
6464
React.useEffect(() => {
6565
// Wait for the title to be updated.
6666
setTimeout(() => {
67-
const { canonicalAs } = pathnameToLanguage(window.location.pathname);
68-
window.ga('set', { page: canonicalAs });
67+
const { canonicalAsServer } = pathnameToLanguage(window.location.pathname);
68+
window.ga('set', { page: canonicalAsServer });
6969
window.ga('send', { hitType: 'pageview' });
7070
});
7171
}, [router.route]);

docs/src/modules/components/TopLayoutBlog.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,7 @@ function TopLayoutBlog(props) {
215215
const finalTitle = title || headers.title;
216216
const router = useRouter();
217217
const slug = router.pathname.replace(/\/blog\//, '');
218-
const { canonicalAs } = pathnameToLanguage(router.asPath);
218+
const { canonicalAsServer } = pathnameToLanguage(router.asPath);
219219
const card =
220220
headers.card === 'true'
221221
? `https://mui.com/static/blog/${slug}/card.png`
@@ -328,7 +328,7 @@ function TopLayoutBlog(props) {
328328
]
329329
},
330330
"headline": "${finalTitle}",
331-
"url": "https://mui.com${canonicalAs}",
331+
"url": "https://mui.com${canonicalAsServer}",
332332
"datePublished": "${headers.date}",
333333
"dateModified": "${headers.date}",
334334
"image": {

docs/src/modules/utils/helpers.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ export function getCookie(name: string): string | undefined {
7878
export function pathnameToLanguage(pathname: string): {
7979
userLanguage: string;
8080
canonicalAs: string;
81+
canonicalAsServer: string;
8182
canonicalPathname: string;
8283
} {
8384
let userLanguage;
@@ -93,14 +94,15 @@ export function pathnameToLanguage(pathname: string): {
9394
}
9495

9596
const canonicalAs = userLanguage === 'en' ? pathname : pathname.substring(3);
96-
const canonicalPathname = canonicalAs
97-
.replace(/^\/api/, '/api-docs')
98-
.replace(/#(.*)$/, '')
99-
.replace(/\/$/, '');
97+
// Remove hash as it's never sent to the server
98+
// https://github.com/vercel/next.js/issues/25202
99+
const canonicalAsServer = canonicalAs.replace(/#(.*)$/, '');
100+
const canonicalPathname = canonicalAsServer.replace(/^\/api/, '/api-docs').replace(/\/$/, '');
100101

101102
return {
102103
userLanguage,
103104
canonicalAs,
105+
canonicalAsServer,
104106
canonicalPathname,
105107
};
106108
}

0 commit comments

Comments
 (0)