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

SLING-11087: Fix for multiple components being fetched via JSI #23

Merged
merged 3 commits into from Jan 31, 2022

Conversation

jar-miracle
Copy link
Contributor

Bugfix: https://issues.apache.org/jira/browse/SLING-11087

Fixes scope related issues in JSI causing only one component to be shown in cases where multiple components should be fetched asynchronously.

Copy link
Contributor

@rombert rombert left a comment

Choose a reason for hiding this comment

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

Thanks for the contributions! I don't think we really have someone familiar with the module ready to review, so I'll give this a shot.

My only question is whether using let instead of var is safe from the point of view of browser support. If that is not the case, have you considered using an IIFE?

@jar-miracle
Copy link
Contributor Author

Seems let is supported by most major browsers since at least 2016: https://caniuse.com/let

Using var pollutes the global variable space of the browser. This causes the same component to be inserted multiple times rather than different components. I don't know too much about JavaScript, but I don't see how using var along with IIFE would solve this issue.

Another solution could be to append the xhr variable with a number similar to how the div is appended. We went with the let solution at the company where I needed this.

@jsedding
Copy link

Personally, I would be slightly more comfortable with the IIFE approach. Not because it is better, but because it conveys the intent about encapsulation of the scope more clearly than. While I am sure you are correct and let stays within the scope of the if statement, it does require background knowledge about a subtlety of the javascript programming language. Given Sling is predominantly Java, I don't think it is safe to assume this knowledge as a given.

@jar-miracle
Copy link
Contributor Author

The primary issue is that it should stay within the <script>-scope. If multiple components are to be replaced you have multiple <script> sections with the same variables. Using var here causes issues. We can use IIFE, no problem, but that's a further change to functionality than just fixing the bug.

I will update the PR however when I get time to test it properly as well.

@jar-miracle
Copy link
Contributor Author

Upon testing I agree your proposal is much cleaner. As I fall into the previously mentioned Java group of Sling developers I am not too sure about browser support for the anonymous function used in the IIFE pattern. Reverted previous commit and introduced an IIFE-based solution.

@sonarcloud
Copy link

sonarcloud bot commented Jan 26, 2022

Kudos, SonarCloud Quality Gate passed!    Quality Gate passed

Bug A 0 Bugs
Vulnerability A 0 Vulnerabilities
Security Hotspot A 0 Security Hotspots
Code Smell A 0 Code Smells

No Coverage information No Coverage information
No Duplication information No Duplication information

Copy link
Contributor

@rombert rombert left a comment

Choose a reason for hiding this comment

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

LGTM now, thanks! @jsedding - any comments?

@jsedding
Copy link

LFTM as well. Thank you!

@rombert rombert merged commit d72b485 into apache:master Jan 31, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
3 participants