-
Notifications
You must be signed in to change notification settings - Fork 825
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
71 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import { useEffect, useState } from "react"; | ||
import { isElementRenderedWithReact } from "./page-settings-meta"; | ||
|
||
type PageSettingsCanonicalLinkProps = { | ||
href: string; | ||
}; | ||
|
||
const isServer = typeof window === "undefined"; | ||
|
||
/** | ||
* Link canonical tag are deduplicated on the server using the HTMLRewriter interface. | ||
* This is not full deduplication. We simply skip rendering Page Setting link | ||
* if it has already been rendered using HeadSlot/HeadLink. | ||
* To prevent React on the client from re-adding the removed link tag, we skip rendering them client-side. | ||
* This approach works because React retains server-rendered link tag as long as they are not re-rendered by the client. | ||
* | ||
* The following component behavior ensures this: | ||
* 1. On the server: Render link tag as usual. | ||
* 2. On the client: Before rendering, remove any link tag with the same `name` or `property` that were not rendered by Client React, | ||
* and then proceed with rendering as usual. | ||
*/ | ||
export const PageSettingsCanonicalLink = ( | ||
props: PageSettingsCanonicalLinkProps | ||
) => { | ||
const [localProps, setLocalProps] = useState< | ||
PageSettingsCanonicalLinkProps | undefined | ||
>(); | ||
|
||
useEffect(() => { | ||
const selector = `head > link[rel="canonical"]`; | ||
let allLinks = document.querySelectorAll(selector); | ||
|
||
for (const meta of allLinks) { | ||
if (!isElementRenderedWithReact(meta)) { | ||
meta.remove(); | ||
} | ||
} | ||
|
||
allLinks = document.querySelectorAll(selector); | ||
|
||
if (allLinks.length === 0) { | ||
setLocalProps(props); | ||
} | ||
}, [props]); | ||
|
||
if (isServer) { | ||
return <link rel="canonical" {...props} />; | ||
} | ||
|
||
if (localProps === undefined) { | ||
// This method also works during hydration because React retains server-rendered tags | ||
// as long as they are not re-rendered by the client. | ||
return; | ||
} | ||
|
||
return <link rel="canonical" {...localProps} />; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters