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

[JSS][NextJS] - fix: Prevent passing internalLinkMatcher prop #847

Merged
merged 2 commits into from
Nov 5, 2021
Merged

[JSS][NextJS] - fix: Prevent passing internalLinkMatcher prop #847

merged 2 commits into from
Nov 5, 2021

Conversation

bhgeorge
Copy link
Contributor

@bhgeorge bhgeorge commented Nov 2, 2021

Prevented passing internalLinkMatcher prop to React Link component as it is an invalid HTML element prop.

Description / Motivation

While attempting to use a custom link matcher (/^\/|^\#/g) to allow internal anchor links, an error occurred for all non-internal link values used in the same component.

When a non-internal link is detected, it attempts to pass all props, including the unused internalLinkMatcher to the JSS React Link component, which then attempts to print it onto the anchor link, resulting in the following error:

Warning: React does not recognize the `internalLinkMatcher` prop on a DOM element. If you intentionally want it to appear in the DOM as a custom attribute, spell it as lowercase `internallinkmatcher` instead. If you accidentally passed it from a parent component, remove it from the DOM element.

We can prevent this error by simply not passing the prop to the JSS React Link component.

Testing Details

Tested by utilizing forked version of JSS in existing project.

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)
  • Documentation update (non-breaking change; modified files are limited to the /docs directory)

@vercel
Copy link

vercel bot commented Nov 2, 2021

@bhgeorge is attempting to deploy a commit to the Sitecore JSS Team on Vercel.

A member of the Team first needs to authorize it.

@CobyPear CobyPear requested a review from a team November 3, 2021 18:11
@CobyPear
Copy link
Contributor

CobyPear commented Nov 3, 2021

Hello @bhgeorge. This seems good to me. Can you please change the target branch to dev?

@bhgeorge bhgeorge changed the base branch from master to dev November 3, 2021 18:51
@bhgeorge
Copy link
Contributor Author

bhgeorge commented Nov 3, 2021

@CobyPear Rebased and changed target. Thanks!

@CobyPear
Copy link
Contributor

CobyPear commented Nov 3, 2021

@bhgeorge please check the following lint error:

  51:10  error  Replace `<ReactLink·{...htmlLinkProps}·editable={editable}·showLinkTextWithChildrenPresent={showLinkTextWithChildrenPresent}·/>` with `(␍⏎····<ReactLink␍⏎······{...htmlLinkProps}␍⏎······editable={editable}␍⏎······showLinkTextWithChildrenPresent={showLinkTextWithChildrenPresent}␍⏎····/>␍⏎··)`  prettier/prettier

You can run npm run lint --fix in the offending package to fix this.

Thinking about this a bit more, I wonder if another solution to this could be removing the link matcher from the props instead of changing the shape of the returned props, just to keep it easier to maintain in the future. Any thoughts @Sitecore/jss-dev-reviewers ?

@ambrauer
Copy link
Contributor

ambrauer commented Nov 3, 2021

@bhgeorge please check the following lint error:

  51:10  error  Replace `<ReactLink·{...htmlLinkProps}·editable={editable}·showLinkTextWithChildrenPresent={showLinkTextWithChildrenPresent}·/>` with `(␍⏎····<ReactLink␍⏎······{...htmlLinkProps}␍⏎······editable={editable}␍⏎······showLinkTextWithChildrenPresent={showLinkTextWithChildrenPresent}␍⏎····/>␍⏎··)`  prettier/prettier

You can run npm run lint --fix in the offending package to fix this.

Thinking about this a bit more, I wonder if another solution to this could be removing the link matcher from the props instead of changing the shape of the returned props, just to keep it easier to maintain in the future. Any thoughts @Sitecore/jss-dev-reviewers ?

Agree, removing the internalLinkMatcher property from props before passing on to <ReactLink /> seems like it would be preferred. Bit more future-proof. Would also be great to cover this issue with a unit test. @bhgeorge if you want to take a stab at it feel free; otherwise we can contribute that.

@bhgeorge
Copy link
Contributor Author

bhgeorge commented Nov 4, 2021

@bhgeorge please check the following lint error:

  51:10  error  Replace `<ReactLink·{...htmlLinkProps}·editable={editable}·showLinkTextWithChildrenPresent={showLinkTextWithChildrenPresent}·/>` with `(␍⏎····<ReactLink␍⏎······{...htmlLinkProps}␍⏎······editable={editable}␍⏎······showLinkTextWithChildrenPresent={showLinkTextWithChildrenPresent}␍⏎····/>␍⏎··)`  prettier/prettier

You can run npm run lint --fix in the offending package to fix this.
Thinking about this a bit more, I wonder if another solution to this could be removing the link matcher from the props instead of changing the shape of the returned props, just to keep it easier to maintain in the future. Any thoughts @Sitecore/jss-dev-reviewers ?

Agree, removing the internalLinkMatcher property from props before passing on to <ReactLink /> seems like it would be preferred. Bit more future-proof. Would also be great to cover this issue with a unit test. @bhgeorge if you want to take a stab at it feel free; otherwise we can contribute that.

I will go ahead and change the implementation and add a unit test.

Copy link
Contributor

@ambrauer ambrauer left a comment

Choose a reason for hiding this comment

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

Looks great to me. Thanks @bhgeorge for the contribution!

@@ -3,7 +3,7 @@ import { NextRouter } from 'next/router';
import NextLink from 'next/link';
import { Link as ReactLink } from '@sitecore-jss/sitecore-jss-react';
import { use, expect, spy } from 'chai';
import { mount } from 'enzyme';
import { mount, render } from 'enzyme';
Copy link
Contributor

Choose a reason for hiding this comment

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

Looks like unused 'render' import snuck in?

@ambrauer ambrauer merged commit 5f25de1 into Sitecore:dev Nov 5, 2021
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.

3 participants