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

Adding an app for redirects when storing state in session storage #10822

Merged
merged 8 commits into from
Apr 18, 2017

Conversation

kobelb
Copy link
Contributor

@kobelb kobelb commented Mar 20, 2017

When using the state:storeInSessionStorage setting with the short-urls, we were previously doing a reply().redirect(fullUrl) and then the state was being extracted from the URL and being put into sessionStorage. This was causing issues with IE where the URL was too long.

This approach uses a separate 'app' and a variable that it's grabbing from the kbn-initial-state to get all the information to the browser, where it then puts it in sessionStorage before redirecting the user to the URL with the shortened hashes. It's not an incredibly elegant solution, and the user experience is somewhat 'choppy' for lack of a better description.

I'm open to alternatives to the current approach, but what's in this PR should work.

Resolves #10128

@kobelb
Copy link
Contributor Author

kobelb commented Mar 21, 2017

jenkins test this

Copy link
Contributor

@spalger spalger left a comment

Choose a reason for hiding this comment

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

Works great, while I still don't love the solution we don't have many options 🤷‍♂️

};

import uiRoutes from 'ui/routes';
uiRoutes.enable();
Copy link
Contributor

Choose a reason for hiding this comment

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

Do we really need the router here? Enabling the router runs all of the route setup work, which just slows down this simple page.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Makes sense, I'll remove it.

import { parse as parseUrl, format as formatUrl } from 'url';
import { stringify as stringifyQuerystring } from 'querystring';

const conservativeStringifyQuerystring = (query) => {
Copy link
Contributor

Choose a reason for hiding this comment

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

All of this hashing logic should be put in the state_management module next to src/ui/public/state_management/state_hashing/unhash_url.js, and it definitely needs tests.

const uiSettings = server.uiSettings();
const stateStoreInSessionStorage = await uiSettings.get(request, 'state:storeInSessionStorage');
if (!stateStoreInSessionStorage) {
reply().redirect(config.get('server.basePath') + url);
Copy link
Contributor

@spalger spalger Apr 7, 2017

Choose a reason for hiding this comment

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

Make sure to return here

} catch (err) {
console.log(err);
Copy link
Contributor

Choose a reason for hiding this comment

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

Make sure to remove this

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good catch, thanks!

src/ui/index.js Outdated
@@ -83,12 +83,12 @@ export default async (kbnServer, server, config) => {
vars: await reduceAsync(
uiExports.injectedVarsReplacers,
async (acc, replacer) => await replacer(acc, request, server),
defaults(await app.getInjectedVars() || {}, uiExports.defaultInjectedVars)
defaults(await app.getInjectedVars() || {}, uiExports.defaultInjectedVars, vars)
Copy link
Contributor

Choose a reason for hiding this comment

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

I think we should rename vars -> injectedVarsOverrides and move it to the front of the list here. If the code calling renderApp() has specific vars in mind they shouldn't be overridden by the defaults or the standard injected vars, only the "replacers" could override them.

@kobelb kobelb force-pushed the state_session_storage_redirect branch from a45365b to e3051c3 Compare April 11, 2017 13:07
@kobelb
Copy link
Contributor Author

kobelb commented Apr 11, 2017

@spalger I've addressed your concerns/comments, except the comment about not using the routing. I tried to do the following to remove the need for routing 14b09e3 but this led to the code being run whenever the javascript was loaded, is there a different way that I should be removing the need to use routing?

@spalger
Copy link
Contributor

spalger commented Apr 11, 2017

this led to the code being run whenever the javascript was loaded

I'm not sure I understand the issue.

@kobelb
Copy link
Contributor Author

kobelb commented Apr 11, 2017

If the following is run every-time that load Kibana in a browser:

const redirectUrl = chrome.getInjected('redirectUrl');

const hashedUrl = hashUrl([new AppState(), globalState], redirectUrl);
const url = chrome.addBasePath(hashedUrl);

$window.location = url;

it'll always try to redirect the user, when we only want it to be ran after the following call:

 const app = kbnServer.uiExports.apps.byId.stateSessionStorageRedirect;
 reply.renderApp(app, {
  redirectUrl: url,
});

@kobelb kobelb added v5.5.0 and removed v5.4.0 labels Apr 12, 2017
@kobelb kobelb requested a review from stacey-gammon April 14, 2017 17:35
@stacey-gammon
Copy link
Contributor

fixes #10128

Copy link
Contributor

@stacey-gammon stacey-gammon left a comment

Choose a reason for hiding this comment

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

LGTM, aside from a couple minor things.

});
};

export default hashUrl;
Copy link
Contributor

Choose a reason for hiding this comment

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

no more default exports, pleeease :) I will add an eslint rule to the ui/public folder this week, working on the last chunk of conversions.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

you got it! old habits die hard...

@@ -0,0 +1,4 @@
{
"name": "state_session_storage_redirect",
"version": "kibana"
Copy link
Contributor

Choose a reason for hiding this comment

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

Either here in a description block, or in index.js, can you add a comment describing the need for this and link to the github issue?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good call, I'll add a description in here.

@kobelb kobelb force-pushed the state_session_storage_redirect branch from e3051c3 to 3f559e3 Compare April 18, 2017 13:55
@kobelb kobelb merged commit 6d81baf into elastic:master Apr 18, 2017
kobelb added a commit to kobelb/kibana that referenced this pull request Apr 18, 2017
…astic#10822)

* Adding an app for redirects when storing state in session storage

* Removing errant console.log

* Adding early return after reply().redirect

* No longer using the router

* Renaming vars to injectedVarsOverrides

* Putting uiRoutes back in so the code is only executed for this app

* Extracting hash_url to it's own module, and adding tests

* Addressing peer-review comments
kobelb added a commit that referenced this pull request Apr 18, 2017
…0822) (#11305)

* Adding an app for redirects when storing state in session storage

* Removing errant console.log

* Adding early return after reply().redirect

* No longer using the router

* Renaming vars to injectedVarsOverrides

* Putting uiRoutes back in so the code is only executed for this app

* Extracting hash_url to it's own module, and adding tests

* Addressing peer-review comments
@kobelb
Copy link
Contributor Author

kobelb commented Apr 18, 2017

Backported

5.x - 9ca5059

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants