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

[templates/nextjs-sxa]: Fixed unsafe property access in SXA components #SXA-7749 #2035

Merged
merged 2 commits into from
Feb 11, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ Our versioning strategy is as follows:

## Unreleased

### 🐛 Bug Fixes

* `[templates/nextjs-sxa]`Fixed unsafe property access by replacing direct calls with optional chaining ([#2035](https://github.com/Sitecore/jss/pull/2035))

## 22.5.0

### 🎉 New Features & Improvements
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ interface ComponentProps {
}

export const Default = (props: ComponentProps): JSX.Element => {
const styles = `${props.params.GridParameters ?? ''} ${props.params.Styles ?? ''}`.trimEnd();
const styles = `${props?.params?.GridParameters ?? ''} ${props?.params?.Styles ?? ''}`.trimEnd();
const columnWidths = [
props.params.ColumnWidth1,
props.params.ColumnWidth2,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@ interface ComponentProps {
}

const DefaultContainer = (props: ComponentProps): JSX.Element => {
const containerStyles = props.params && props.params.Styles ? props.params.Styles : '';
const styles = `${props.params.GridParameters} ${containerStyles}`.trimEnd();
const phKey = `container-${props.params.DynamicPlaceholderId}`;
const id = props.params.RenderingIdentifier;
const containerStyles = props?.params?.Styles ?? '';
const styles = `${props?.params?.GridParameters} ${containerStyles}`.trimEnd();
const phKey = `container-${props?.params?.DynamicPlaceholderId}`;
const id = props?.params?.RenderingIdentifier;
const mediaUrlPattern = new RegExp(/mediaurl=\"([^"]*)\"/, 'i');
let backgroundImage = props.params.BackgroundImage as string;
let backgroundImage = props?.params?.BackgroundImage as string;
let backgroundStyle: { [key: string]: string } = {};

if (backgroundImage && backgroundImage.match(mediaUrlPattern)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ type ImageProps = {
};

const ImageDefault = (props: ImageProps): JSX.Element => (
<div className={`component image ${props.params.styles}`.trimEnd()}>
<div className={`component image ${props?.params?.styles}`.trimEnd()}>
<div className="component-content">
<span className="is-empty-hint">Image</span>
</div>
Expand All @@ -49,16 +49,16 @@ export const Banner = (props: ImageProps): JSX.Element => {
.replace(`height="${props?.fields?.Image?.value?.height}"`, 'height="100%"'),
}
: {
...props.fields.Image,
value: {
...props.fields.Image.value,
style: { width: '100%', height: '100%' },
},
};
...props.fields.Image,
value: {
...props.fields.Image.value,
style: { width: '100%', height: '100%' },
},
};

return (
<div
className={`component hero-banner ${props.params.styles} ${classHeroBannerEmpty}`}
className={`component hero-banner ${props?.params?.styles} ${classHeroBannerEmpty}`}
id={id ? id : undefined}
>
<div className="component-content sc-sxa-image-hero-banner" style={backgroundStyle}>
Expand All @@ -76,7 +76,7 @@ export const Default = (props: ImageProps): JSX.Element => {
const id = props.params.RenderingIdentifier;

return (
<div className={`component image ${props.params.styles}`} id={id ? id : undefined}>
<div className={`component image ${props?.params?.styles}`} id={id ? id : undefined}>
<div className="component-content">
{sitecoreContext.pageState === 'edit' || !props.fields.TargetUrl?.value?.href ? (
<Image />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ const LinkListItem = (props: LinkListItemProps) => {

export const Default = (props: LinkListProps): JSX.Element => {
const datasource = props.fields?.data?.datasource;
const styles = `component link-list ${props.params.styles}`.trimEnd();
const styles = `component link-list ${props?.params?.styles}`.trimEnd();
const id = props.params.RenderingIdentifier;

if (datasource) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ export const Default = (props: NavigationProps): JSX.Element => {
const { sitecoreContext } = useSitecoreContext();
const styles =
props.params != null
? `${props.params.GridParameters ?? ''} ${props.params.Styles ?? ''}`.trimEnd()
? `${props.params.GridParameters ?? ''} ${props?.params?.Styles ?? ''}`.trimEnd()
: '';
const id = props.params != null ? props.params.RenderingIdentifier : null;

Expand Down Expand Up @@ -110,7 +110,7 @@ export const Default = (props: NavigationProps): JSX.Element => {
const NavigationList = (props: NavigationProps) => {
const { sitecoreContext } = useSitecoreContext();
const [active, setActive] = useState(false);
const classNameList = `${props.fields.Styles.concat('rel-level' + props.relativeLevel).join(
const classNameList = `${props?.fields?.Styles.concat('rel-level' + props.relativeLevel).join(
' '
)}`;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ type ComponentContentProps = {
const ComponentContent = (props: ComponentContentProps) => {
const id = props.id;
return (
<div className={`component content ${props.styles}`} id={id ? id : undefined}>
<div className={`component content ${props?.styles}`} id={id ? id : undefined}>
<div className="component-content">
<div className="field-content">{props.children}</div>
</div>
Expand All @@ -37,7 +37,7 @@ export const Default = (props: PageContentProps): JSX.Element => {

if (!(props.fields && props.fields.Content) && !sitecoreContext?.route?.fields?.Content) {
return (
<div className={`component content ${props.params.styles}`} id={id ? id : undefined}>
<div className={`component content ${props?.params?.styles}`} id={id ? id : undefined}>
<div className="component-content">
<div className="field-content">[Content]</div>
</div>
Expand All @@ -50,7 +50,7 @@ export const Default = (props: PageContentProps): JSX.Element => {
: sitecoreContext?.route?.fields?.Content) as RichTextField;

return (
<ComponentContent styles={props.params.styles} id={id}>
<ComponentContent styles={props?.params?.styles} id={id}>
<JssRichText field={field} />
</ComponentContent>
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ type PromoProps = {
};

const PromoDefaultComponent = (props: PromoProps): JSX.Element => (
<div className={`component promo ${props.params.styles}`}>
<div className={`component promo ${props?.params?.styles}`}>
<div className="component-content">
<span className="is-empty-hint">Promo</span>
</div>
Expand All @@ -32,7 +32,7 @@ export const Default = (props: PromoProps): JSX.Element => {
const id = props.params.RenderingIdentifier;
if (props.fields) {
return (
<div className={`component promo ${props.params.styles}`} id={id ? id : undefined}>
<div className={`component promo ${props?.params?.styles}`} id={id ? id : undefined}>
<div className="component-content">
<div className="field-promoicon">
<JssImage field={props.fields.PromoIcon} />
Expand All @@ -59,7 +59,7 @@ export const WithText = (props: PromoProps): JSX.Element => {
const id = props.params.RenderingIdentifier;
if (props.fields) {
return (
<div className={`component promo ${props.params.styles}`} id={id ? id : undefined}>
<div className={`component promo ${props?.params?.styles}`} id={id ? id : undefined}>
<div className="component-content">
<div className="field-promoicon">
<JssImage field={props.fields.PromoIcon} />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export const Default = (props: RichTextProps): JSX.Element => {

return (
<div
className={`component rich-text ${props.params.styles.trimEnd()}`}
className={`component rich-text ${props?.params?.styles.trimEnd()}`}
id={id ? id : undefined}
>
<div className="component-content">{text}</div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ interface ComponentProps {
}

export const Default = (props: ComponentProps): JSX.Element => {
const styles = `${props.params.GridParameters ?? ''} ${props.params.Styles ?? ''}`.trimEnd();
const styles = `${props?.params?.GridParameters ?? ''} ${props?.params?.Styles ?? ''}`.trimEnd();
const rowStyles = [
props.params.Styles1,
props.params.Styles2,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ type ComponentContentProps = {
const ComponentContent = (props: ComponentContentProps) => {
const id = props.id;
return (
<div className={`component title ${props.styles}`} id={id ? id : undefined}>
<div className={`component title ${props?.styles}`} id={id ? id : undefined}>
<div className="component-content">
<div className="field-title">{props.children}</div>
</div>
Expand All @@ -79,7 +79,7 @@ export const Default = (props: TitleProps): JSX.Element => {
}

return (
<ComponentContent styles={props.params.styles} id={props.params.RenderingIdentifier}>
<ComponentContent styles={props?.params?.styles} id={props?.params?.RenderingIdentifier}>
<>
{sitecoreContext.pageEditing ? (
<Text field={text} />
Expand Down