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

Add check if modules are same domain #226

Merged
merged 13 commits into from
Dec 6, 2018

Conversation

dariadomagala-sap
Copy link
Contributor

Description

Changes proposed in this pull request:

  • Add check if modules are same domain

Related issue(s)

}
return true;
};

const getDomainFromUrl = url => {
Copy link
Contributor

Choose a reason for hiding this comment

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

We should be able to detect multiple SPAs under one domain, i.e:
https://example.com/foo/ <-- root of one SPA
https://example.com/bar/ <-- root of another SPA

Copy link
Contributor

Choose a reason for hiding this comment

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

I guess we do not have a concept / configuration option for that, since you would be currently only able to detect hash based SPAs, path based are undetectable by only the url. In my opinion this is a followup feature, which requires grooming.

@dariadomagala-sap dariadomagala-sap added the WIP Work in progress label Nov 21, 2018
@dariadomagala-sap dariadomagala-sap removed the WIP Work in progress label Dec 3, 2018
Copy link
Contributor

@kwiatekus kwiatekus left a comment

Choose a reason for hiding this comment

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

Tested using tractor module:

      {
        label: 'Tractors',
        pathSegment: 'tractors',
        viewUrl: 'http://localhost:8080',
        keepSelectedForChildren: true,
        viewGroup : 'tractors',
        children : [
          {
            label: 'tractor',
            pathSegment: ':id',
            viewUrl: 'http://localhost:8080/:id',
            context: {
              projectId
            }
          }
        ]
      }

Copy link
Contributor

@jesusreal jesusreal left a comment

Choose a reason for hiding this comment

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

Please see my comments

@@ -98,11 +98,29 @@ export const isNotSameDomain = (config, component) => {
componentData.previousNodeValues.viewUrl
);
const nextUrl = getUrlWithoutHash(componentData.viewUrl);
return previousUrl != nextUrl;
Copy link
Contributor

Choose a reason for hiding this comment

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

I would suggest refactoring this code to a version where we have less returns, to make the code easier to understand and change in the future if needed. Here my suggestion:

export const isNotSameDomain = (config, component) => {
  if (config.iframe) {
    const componentData = component.get();
    
    const previousUrl = getUrlWithoutHash(
      componentData.previousNodeValues.viewUrl
      );
    const nextUrl = getUrlWithoutHash(componentData.viewUrl);
    if (previousUrl === nextUrl) {
      return false;
    }

    const previousUrlOrigin = getLocation(previousUrl);
    const nextUrlOrigin = getLocation(nextUrl);
    if (previousUrlOrigin === nextUrlOrigin) {
      const previousViewGroup = componentData.previousNodeValues.viewGroup;
      const nextViewGroup = componentData.viewGroup;
      if (previousViewGroup && nextViewGroup && previousViewGroup === nextViewGroup) {
        return false;
      }
    }
  }
  return true;
};

So we only return false if we detect we are in the same domain, otherwise code execution will come to the last line of the function and return true.

Also split the if..else block to initialise some variables only if we need it. I executed the unit tests and they are still passing.

@@ -244,7 +246,7 @@ The node parameters are as follows:
- **keepSelectedForChildren** focuses the navigation on its current hierarchy, omitting the display of children.
- **loadingIndicator.enabled** shows a loading indicator when switching between micro front-ends. If you have a fast micro front-end, you can disable this feature to prevent flickering of the loading indicator. This parameter is enabled by default.
- **loadingIndicator.hideAutomatically** disables the automatic hiding of the loading indicator once the micro front-end is loaded. It is only considered if the loading indicator is enabled. It does not apply if the loading indicator is activated manually with the `LuigiClient.uxManager().showLoadingIndicator()` function. If the loading indicator is enabled and automatic hiding is disabled, use `LuigiClient.uxManager().hideLoadingIndicator()` to hide it manually in your micro front-end during the startup. This parameter is enabled by default.

- **viewGroup** defines a group of views in the same domain sharing a common security context, which allows for example frame re-using for better performance.
Copy link
Contributor

Choose a reason for hiding this comment

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

There is a space too much in 'common security'

return node.viewGroup;
} else if (node.parent) {
return findViewGroup(node.parent);
} else return;
Copy link
Contributor

Choose a reason for hiding this comment

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

else return would not be needed, since javascript does implicitly that already

@@ -98,11 +98,29 @@ export const isNotSameDomain = (config, component) => {
componentData.previousNodeValues.viewUrl
);
const nextUrl = getUrlWithoutHash(componentData.viewUrl);
return previousUrl != nextUrl;
Copy link
Contributor

Choose a reason for hiding this comment

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

IMHO isNotSameDomain isn't anymore a valid name for the function. My proposal is isNotSameViewGroup. Or as it is a Luigi internal function, we could change the returns and how we use it and rename it to isSameViewGroup. In general I find functions with negation more difficult to understand.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I will refactor the function so it doesn't have negation, but I don't like the isSameViewGroup nam, because in hash routing we don't have viewGroups. Maybe something more like isSameApplication, isSameIFrame?

@@ -797,6 +797,45 @@ describe('Routing', () => {
previousNodeValues: { viewUrl: config.iframe.src }
});
assert.isTrue(routing.isNotSameDomain(config, component));

Copy link
Contributor

Choose a reason for hiding this comment

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

I would rather split this test in several tests, and have all under a describe('FUNCTION NAME', () => {}) block.

Copy link
Contributor

Choose a reason for hiding this comment

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

This would make also much easier to understand what each assertion is testing

Copy link
Contributor

@jesusreal jesusreal left a comment

Choose a reason for hiding this comment

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

LGTM after the changes performed.

I would specify in the description of the 2 tests cases for viewGroups "same domain", e.g.:
should return XXX if views have same domain and XXX viewGroups

Copy link
Contributor

@bszwarc bszwarc left a comment

Choose a reason for hiding this comment

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

Once comment :)

docs/navigation-configuration.md Outdated Show resolved Hide resolved
Copy link
Contributor

@bszwarc bszwarc left a comment

Choose a reason for hiding this comment

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

Added one more comment

@dariadomagala-sap dariadomagala-sap merged commit 79f7ea3 into SAP:master Dec 6, 2018
@dariadomagala-sap dariadomagala-sap deleted the same-domain-modules branch December 6, 2018 10:56
stanleychh pushed a commit to stanleychh/luigi that referenced this pull request Dec 30, 2021
Add check if modules are same domain
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants