Sitecore / jss

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

Link component is not checking if href is undefined #1834

Open deansimcox opened 2 months ago

deansimcox commented 2 months ago

Describe the Bug

If the field passed to the Link component has an undefined href, it will render undefined as a string in the dom.

To Reproduce

import {Link} from '@sitecore-jss/sitecore-jss-nextjs';

const field = {value: {href: undefined, text: 'This is a link with a href that is undefined'}};

<Link field={field} />

This will render a link like this:

<a href="undefined">This is a link with a href that is undefined</a>

Expected Behavior

I would expect the undefined href to be filtered out, leaving behind an anchor that with no href attribute.

E.g.

<a>This is a link with a href that is undefined</a>

Possible Fix

On line 102, we should be checking for undefined https://github.com/Sitecore/jss/blob/v20.3.3/packages/sitecore-jss-react/src/components/Link.tsx#L102

E.g.

href: link.href ? `${link.href}${querystring}${anchor}` : undefined,

Provide environment information

sc-addypathania commented 1 month ago

@deansimcox, thanks for reporting this bug. I will add it to our backlog and prioritize it accordingly. However, if you feel this is a blocker for you, feel free to contribute by issuing a PR to our repository.

deansimcox commented 1 month ago

All good, appreciate the update @sc-addypathania

pzi commented 1 month ago

@deansimcox, thanks for reporting this bug. I will add it to our backlog and prioritize it accordingly. However, if you feel this is a blocker for you, feel free to contribute by issuing a PR to our repository.

@sc-addypathania If we PR a fix, would you consider patching it into jss@20.3.3? or v21.x?

sc-addypathania commented 1 month ago

@pzi yes you can issue a fix on the dev branch for now and we will try to prioritize the patch releases soon.

sumon1991 commented 1 month ago

@sc-addypathania I created a PR of it please review it.

yavorsk commented 1 month ago

hey @sumon1991 thanks for your contribution :) I've created an item in our backlog to review and merge your PR