Sitecore / jss

Software development kit for JavaScript developers building web applications with Sitecore Experience Platform
https://jss.sitecore.com
Apache License 2.0
261 stars 275 forks source link

NextJs RichText doesn't allow client side routing when content changes #1502

Closed pschofield closed 1 year ago

pschofield commented 1 year ago

Describe the Bug

The RichText component in sitecore-jss-nextjs includes code to allow internal links embedded within the rich text field to use client side routing (using the next/router). This is done by adding an event handler to the links when the component renders. This issue occurs when the content in the rich text field changes (e.g. load another page with the same page structure but different content in the rich text field). When the content changes, the useEffect() hook isn't re-run (as it has no dependancies) so the initializeLinks() function is never run on the new content. This means clicking on any internal link within the RichText field doesn't use client side routing and causes a full page load.

To Reproduce

Expected Behavior

The initializeLinks() would run on the embedded links within the rich text component when the components first mounts and also when the content is updated

Possible Fix

Add the hasText property to the dependancy array or the useeffect() hook

Provide environment information

pschofield commented 1 year ago

Raised PR 1503: https://github.com/Sitecore/jss/pull/1503

as a proposed fix.

art-alexeyenko commented 1 year ago

Addressed by #1503 , thank you @pschofield .