Skip to content
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

[NextJS] Fix for bug in RichText component - links not reinitialised when content changes #1503

Conversation

pschofield
Copy link
Contributor

@pschofield pschofield commented May 30, 2023

Description / Motivation

When the RichText component content changes, the useEffect() hook is not re-run as it has no dependancies. This means the initializeLinks() function is not run so any embedded internal links within the component will not have the appropriate click event handler. This causes any link within the component to cause a full page load rather than client side routing occuring. This change adds the "hasText" property (essentially the rich text) as a dependancy for the useEffect() hook, ensuring the initializeLinks() function is run when the content changes, enabling client side routing to occur.

Testing Details

  • Unit Test Added
  • Manual Test/Other (Please elaborate)
    Unit test added.
    Manual testing involved applying the proposed changes to a local dev instance of Sitecore 10.2 and 10.3 and observing the appropriate event handler is applied to internal links within the rich text component, both on first render but also after the content changes

Types of changes

Subsequent pages don't have client side routing

Added a useEffect dependancy to the NextJs RichText component. This
fixes an issue where where the useEffect() hook doesn't run when
navigating to a page that contains a RichText component after clicking an
internal link in a RichText component on another page to cause the
navigation.

Description:
1. Open a page that contains a RichText component that has an <a> tag
   that links to a page within the same site (i.e. clicking the link
   fires the routeHandler() function within the richText component that
   performs a client side navigation)

2. When the RichText component on the new page renders, it doesn't run
   the useEffect hook so the event handler doesn't get attached to any
   internal links within the RichText component

3. Clicking on any of the embedded links causes a whole page refresh as
   the event handler isn't attached, so no client side routing occurs.
@pschofield pschofield changed the title Feature/jss nextjs richtext re-initialize links when content changes [NextJs] Feature/jss nextjs richtext re-initialize links when content changes May 30, 2023
@pschofield pschofield changed the title [NextJs] Feature/jss nextjs richtext re-initialize links when content changes [NextJS] Feature/jss nextjs richtext re-initialize links when content changes May 30, 2023
@pschofield pschofield changed the title [NextJS] Feature/jss nextjs richtext re-initialize links when content changes [NextJS] Fix for bug in RichText component - links not reinitialised when content changes May 30, 2023
@art-alexeyenko
Copy link
Contributor

Hi @pschofield ! Thanks for the report, and big thanks for the PR. I've added this to our backlog, we'll triage and see what to do with this next.

Copy link
Contributor

@art-alexeyenko art-alexeyenko left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@pschofield This looks good.
Can I ask you to add a Changelog entry, and then we'll merge it?

Copy link
Contributor

@art-alexeyenko art-alexeyenko left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Awesome, thank you so much!

@art-alexeyenko art-alexeyenko merged commit bd86495 into Sitecore:dev Jun 7, 2023
1 check passed
@pschofield pschofield deleted the feature/jss-nextjs-richtext-initialize-links branch June 11, 2023 21:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants