-
Notifications
You must be signed in to change notification settings - Fork 8.3k
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 relative path handling to application.navigateToUrl
#78565
Merged
pgayvallet
merged 7 commits into
elastic:master
from
pgayvallet:kbn-78007-relative-navigateToUrl
Oct 1, 2020
Merged
Changes from 5 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
add095a
split application utilities and associated tests to distinct files
pgayvallet 462301a
do not match app if path does not start with the basePath
pgayvallet e14858f
add relative paths support to `navigateToUrl`
pgayvallet b0686e1
add null-check error
pgayvallet 4dc3cc6
update generated doc
pgayvallet 4f7ab15
Merge remote-tracking branch 'upstream/master' into kbn-78007-relativ…
pgayvallet 12dd5cf
nits on doc
pgayvallet File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -4,9 +4,11 @@ | |||||
|
||||||
## ApplicationStart.navigateToUrl() method | ||||||
|
||||||
Navigate to given url, which can either be an absolute url or a relative path, in a SPA friendly way when possible. | ||||||
Navigate to given url in a SPA friendly way when possible (when the url will redirect to a valid application within the current basePath). | ||||||
|
||||||
If all these criteria are true for the given url: - (only for absolute URLs) The origin of the URL matches the origin of the browser's current location - The pathname of the URL starts with the current basePath (eg. /mybasepath/s/my-space) - The pathname segment after the basePath matches any known application route (eg. /app/<id>/ or any application's `appRoute` configuration) | ||||||
The method resolves pathnames the same way browsers do when resolving a `<a href>` value. The provided url can be: - an absolute url - an absolute path - a path relative to the current url (window.location.href) | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
|
||||||
If all these criteria are true for the given url: - (only for absolute URLs) The origin of the URL matches the origin of the browser's current location - The resolved pathname of the provided URL/path starts with the current basePath (eg. /mybasepath/s/my-space) - The pathname segment after the basePath matches any known application route (eg. /app/<id>/ or any application's `appRoute` configuration) | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
|
||||||
Then a SPA navigation will be performed using `navigateToApp` using the corresponding application and path. Otherwise, fallback to a full page reload to navigate to the url using `window.location.assign` | ||||||
|
||||||
|
@@ -35,11 +37,14 @@ navigateToUrl(url: string): Promise<void>; | |||||
// will call `application.navigateToApp('discover', { path: '/some-path?foo=bar'})` | ||||||
application.navigateToUrl('https://kibana:8080/base-path/s/my-space/app/discover/some-path?foo=bar') | ||||||
application.navigateToUrl('/base-path/s/my-space/app/discover/some-path?foo=bar') | ||||||
application.navigateToUrl('./discover/some-path?foo=bar') | ||||||
|
||||||
// will perform a full page reload using `window.location.assign` | ||||||
application.navigateToUrl('https://elsewhere:8080/base-path/s/my-space/app/discover/some-path') // origin does not match | ||||||
application.navigateToUrl('/app/discover/some-path') // does not include the current basePath | ||||||
application.navigateToUrl('/base-path/s/my-space/app/unknown-app/some-path') // unknown application | ||||||
application.navigateToUrl('../discover') // resolve to `/base-path/s/my-space/discover` which is not a path of a known app. | ||||||
application.navigateToUrl('../../other-space/discover') // resolve to `/base-path/s/other-space/discover` which is not within the current basePath. | ||||||
|
||||||
``` | ||||||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
/* | ||
* Licensed to Elasticsearch B.V. under one or more contributor | ||
* license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright | ||
* ownership. Elasticsearch B.V. licenses this file to you under | ||
* the Apache License, Version 2.0 (the "License"); you may | ||
* not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
|
||
import { appendAppPath } from './append_app_path'; | ||
|
||
describe('appendAppPath', () => { | ||
it('appends the appBasePath with given path', () => { | ||
expect(appendAppPath('/app/my-app', '/some-path')).toEqual('/app/my-app/some-path'); | ||
expect(appendAppPath('/app/my-app/', 'some-path')).toEqual('/app/my-app/some-path'); | ||
expect(appendAppPath('/app/my-app', 'some-path')).toEqual('/app/my-app/some-path'); | ||
expect(appendAppPath('/app/my-app', '')).toEqual('/app/my-app'); | ||
}); | ||
|
||
it('preserves the trailing slash only if included in the hash or appPath', () => { | ||
expect(appendAppPath('/app/my-app', '/some-path/')).toEqual('/app/my-app/some-path'); | ||
expect(appendAppPath('/app/my-app', '/some-path#/')).toEqual('/app/my-app/some-path#/'); | ||
expect(appendAppPath('/app/my-app#/', '')).toEqual('/app/my-app#/'); | ||
expect(appendAppPath('/app/my-app#', '/')).toEqual('/app/my-app#/'); | ||
expect(appendAppPath('/app/my-app', '/some-path#/hash/')).toEqual( | ||
'/app/my-app/some-path#/hash/' | ||
); | ||
expect(appendAppPath('/app/my-app', '/some-path#/hash')).toEqual('/app/my-app/some-path#/hash'); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
/* | ||
* Licensed to Elasticsearch B.V. under one or more contributor | ||
* license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright | ||
* ownership. Elasticsearch B.V. licenses this file to you under | ||
* the Apache License, Version 2.0 (the "License"); you may | ||
* not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
|
||
import { removeSlashes } from './remove_slashes'; | ||
|
||
export const appendAppPath = (appBasePath: string, path: string = '') => { | ||
// Only prepend slash if not a hash or query path | ||
path = path === '' || path.startsWith('#') || path.startsWith('?') ? path : `/${path}`; | ||
// Do not remove trailing slash when in hashbang or basePath | ||
const removeTrailing = path.indexOf('#') === -1 && appBasePath.indexOf('#') === -1; | ||
return removeSlashes(`${appBasePath}${path}`, { | ||
trailing: removeTrailing, | ||
duplicates: true, | ||
leading: false, | ||
}); | ||
}; |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.