File tree Expand file tree Collapse file tree 4 files changed +16
-12
lines changed Expand file tree Collapse file tree 4 files changed +16
-12
lines changed Original file line number Diff line number Diff line change @@ -36,7 +36,7 @@ const GOOGLE_ANALYTICS_ID = PRODUCTION_GA ? 'UA-106598593-2' : 'UA-106598593-3';
36
36
37
37
export default class MyDocument extends Document {
38
38
render ( ) {
39
- const { canonicalAs , userLanguage } = this . props ;
39
+ const { canonicalAsServer , userLanguage } = this . props ;
40
40
41
41
return (
42
42
< Html lang = { userLanguage } >
@@ -63,9 +63,11 @@ export default class MyDocument extends Document {
63
63
{ /* SEO */ }
64
64
< link
65
65
rel = "canonical"
66
- href = { `https://mui.com${ userLanguage === 'en' ? '' : `/${ userLanguage } ` } ${ canonicalAs } ` }
66
+ href = { `https://mui.com${
67
+ userLanguage === 'en' ? '' : `/${ userLanguage } `
68
+ } ${ canonicalAsServer } `}
67
69
/>
68
- < link rel = "alternate" href = { `https://mui.com${ canonicalAs } ` } hrefLang = "x-default" />
70
+ < link rel = "alternate" href = { `https://mui.com${ canonicalAsServer } ` } hrefLang = "x-default" />
69
71
{ /*
70
72
Preconnect allows the browser to setup early connections before an HTTP request
71
73
is actually sent to the server.
@@ -233,7 +235,7 @@ MyDocument.getInitialProps = async (ctx) => {
233
235
234
236
return {
235
237
...initialProps ,
236
- canonicalAs : pathnameToLanguage ( url ) . canonicalAs ,
238
+ canonicalAsServer : pathnameToLanguage ( url ) . canonicalAsServer ,
237
239
userLanguage : ctx . query . userLanguage || 'en' ,
238
240
// Styles fragment is rendered after the app and page rendering finish.
239
241
styles : [
Original file line number Diff line number Diff line change @@ -64,8 +64,8 @@ function GoogleAnalytics() {
64
64
React . useEffect ( ( ) => {
65
65
// Wait for the title to be updated.
66
66
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 } ) ;
69
69
window . ga ( 'send' , { hitType : 'pageview' } ) ;
70
70
} ) ;
71
71
} , [ router . route ] ) ;
Original file line number Diff line number Diff line change @@ -215,7 +215,7 @@ function TopLayoutBlog(props) {
215
215
const finalTitle = title || headers . title ;
216
216
const router = useRouter ( ) ;
217
217
const slug = router . pathname . replace ( / \/ b l o g \/ / , '' ) ;
218
- const { canonicalAs } = pathnameToLanguage ( router . asPath ) ;
218
+ const { canonicalAsServer } = pathnameToLanguage ( router . asPath ) ;
219
219
const card =
220
220
headers . card === 'true'
221
221
? `https://mui.com/static/blog/${ slug } /card.png`
@@ -328,7 +328,7 @@ function TopLayoutBlog(props) {
328
328
]
329
329
},
330
330
"headline": "${ finalTitle } ",
331
- "url": "https://mui.com${ canonicalAs } ",
331
+ "url": "https://mui.com${ canonicalAsServer } ",
332
332
"datePublished": "${ headers . date } ",
333
333
"dateModified": "${ headers . date } ",
334
334
"image": {
Original file line number Diff line number Diff line change @@ -78,6 +78,7 @@ export function getCookie(name: string): string | undefined {
78
78
export function pathnameToLanguage ( pathname : string ) : {
79
79
userLanguage : string ;
80
80
canonicalAs : string ;
81
+ canonicalAsServer : string ;
81
82
canonicalPathname : string ;
82
83
} {
83
84
let userLanguage ;
@@ -93,14 +94,15 @@ export function pathnameToLanguage(pathname: string): {
93
94
}
94
95
95
96
const canonicalAs = userLanguage === 'en' ? pathname : pathname . substring ( 3 ) ;
96
- const canonicalPathname = canonicalAs
97
- . replace ( / ^ \/ a p i / , '/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 ( / ^ \/ a p i / , '/api-docs' ) . replace ( / \/ $ / , '' ) ;
100
101
101
102
return {
102
103
userLanguage,
103
104
canonicalAs,
105
+ canonicalAsServer,
104
106
canonicalPathname,
105
107
} ;
106
108
}
You can’t perform that action at this time.
0 commit comments