-
-
Notifications
You must be signed in to change notification settings - Fork 4.2k
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
Query Params Reset to Default Values on Initial Render If transitionTo is called in beforeModel #12169
Comments
@raytiley I'm wondering if this is a bug that exists on both 1.13 and 2.0. Also does the example work on a prior version of Ember, i.e. 1.12? |
@rwjblue It might be the same, I really need to dig a little bit deeper to understand where the desired behavior should be happening. The underlying issue is that the
My basic understanding without writing a test is this. You initially enter the route with a url with query params... this creates a new transition, but never calls |
@pixelhandler I haven't tested other versions yet, but I don't think this area of the code has changed drastically, except for @trek's recent work on moving queryParam config to the router. I don't believe that has landed yet, but might be a nice place to change this behavior. Ideally I think we need to finalize and queryParam changes for the current transition when calling For anyone who is following along, as a workaround I was able to schedule the transitionTo onto the run loop in |
+1 @raytiley I tried your workaround but my queryParams are still getting stripped like so:
That's the correct syntax, yes? |
@noslouch You could also try the |
I do not have this exact issue, but I do have issues with From phantom:
From chrome:
|
@kmiyashiro +1, I have exactly the same problem after upgrading to 2.1.0
|
This has just bitten me as well, if I add this to a completely unrelated route import Ember from 'ember';
export default Ember.Route.extend({
beforeModel: function() {
this.transitionTo('works');
}
}); Then my query params seem to get stripped and I get this error...
I'm using Thanks |
Happening on [email protected] for me. |
Happening here too... [email protected] |
I'm with @kmiyashiro, everything seems to be working as expected... but i'm still getting the error in console. I can easily recreate it in a twiddle. Just load the following with the console open. You can see the error in ember 2.1 and 2.2, doesn't seem to appear in 2.0. https://ember-twiddle.com/86849f6610f3a68a9840?route=%2F%3Ftest%3Dfoo |
Here is a work around that you can use to trigger transitions while this bug remains unresolved. let RedirectAfterDidTransition = Ember.Mixin.create({
redirectAfterDidTransition(...args) {
this.router.one('didTransition', ()=>{
this.router.transitionTo(...args);
});
}
});
export default Route.extend(RedirectAfterDidTransition, {
afterModel() {
this.redirectAfterDidTransition('index');
}
}); I cloned the above Twiddle and added the example https://ember-twiddle.com/2d49a9d1aa3ddaff6ef5?numColumns=1&route=%2F%3Ftest%3Dfoo Note: this will wait for the transition to finish before doing the redirect. |
Here is a simpler version that seems to work as well in my code import Ember from 'ember';
// This mixin provides `abortAndTransitionTo` method that'll abort the current
// transition and transition to specified parameters.
// It takes same arguments as Route#transitionTo
// it's necessary because of https://github.com/emberjs/ember.js/issues/12169
let AbortAndTransitionTo = Ember.Mixin.create({
abortAndTransitionTo(...args) {
this.router.router.activeTransition.abort();
this.router.transitionTo(...args);
}
});
export default Route.extend(AbortAndTransitionTo, {
afterModel() {
this.abortAndTransitionTo('index');
}
}); |
@taras doesn't this waste a fetch since it's after the model? My workaround is just to catch the error and log it since I couldn't find any side effects. beforeModel(transition) {
this._super(...arguments);
// Logic to set presetParams (dynamic initial params)
this.transitionTo({ queryParams: presetParams })
.catch(() => {
log('Ember 2.1 query param issue: https://github.com/emberjs/ember.js/issues/12169');
});
} |
@kmiyashiro what do you mean: "waste a fetch"? and what is it about your solution that prevents a fetch from being wasted? |
I'm assuming that you are fetching (find/query) in |
We might have a different use case. If you would like to explore this further, you can ping me on Ember Community slack or we can pair via http://j.mp/EmberSherpa |
Encountering this same issue, though i'm not explicitly calling Since I can't reliably suppress the error, I can't upgrade to Ember 2.1 or 2.2, since this error causes some of my unit tests to break, which my CI system doesn't like.
|
I was able to suppress the error by monkey-patching the Router:
|
Happening to me on Ember 2.2.2. I am catching and logging the error as there are no side-effects. |
It's perhaps not the most elegant solution, but this seems to work on Ember 1.13: // app/instance-initializers/global-query-params.js
import Ember from 'ember';
import _ from 'lodash';
const RouteSupport = Ember.Mixin.create({
transitionTo: superWithGlobalQueryParams,
replaceWith: superWithGlobalQueryParams
});
function getQueryParams(...names) {
return names.reduce(function(result, name) {
const regex = new RegExp(`[?&]${name}=([^?&]+)`);
const md = regex.exec(window.location.search || '');
if (md) { result[name] = md[1]; }
return result;
}, {});
}
function superWithGlobalQueryParams(...args) {
const globalQueryParams = getQueryParams('tango-version');
if (args.length === 1) {
// transitionTo('some.route');
args.push({ queryParams: globalQueryParams });
} else if (args.length === 2 && args[1].queryParams) {
// transitionTo('some.route', { queryParams: { ... } });
args[1].queryParams = _.extend({}, globalQueryParams, args[1].queryParams);
} else if (args.length === 2) {
// transitionTo('some.route', someModel);
args.push({ queryParams: globalQueryParams });
} else {
// transitionTo('some.route', someModel, { queryParams: { ... } });
args[2].queryParams = _.extend({}, globalQueryParams, args[2].queryParams);
}
return this._super(...args);
}
export default {
name: 'global-query-params',
initialize() {
Ember.Route.reopen(RouteSupport);
}
}; If you don't want to maintain the list of allowed query-params there, you might be able to just parse all of them using something like this. |
Same happens without directly calling transitionTo beforeModel(transition) {
let token = transition.queryParams.token;
return this.get('session').authenticate('authenticator:custom', token).then(...).catch(...);
} ember-simple-auth tries to transition to '/' if auth successful. Ember 2.3.0.beta2 |
Happening to me too. Using @taras's Ember: 2.2.0 |
I'm seeing a variant on this bug in my code. The error I'm seeing happens when I re-load a page that has query params. The page itself is in a nested route - the The specific error I see is:
The When I put in a breakpoint I can see that The exception thrown does seem to be harmless, so for me @rzurad's patch works around this issue. Ember: 2.4.2 |
Fix is included in v2.4.4. |
Hmm, after upgrading "ember" to "2.4.4" in my bower.json, and removing taras's |
@john-kurkowski can you try to get a simple ember-twiddle / jsbin reproduction together? Now that all this code is fresh in my mind I can probably figure it out pretty quick with a reproduction. |
@john-kurkowski I think this will fix that error: #13273 Hoping to finish up testing it tomorrow and then I'll pester @rwjblue for a merge :) |
Still experiencing issue on Ember 2.6.0 |
Hmm, I'm on 2.4.6 and still getting the issue. |
Awesome thanks @Unnumbered |
Still happening to me on 2.7.0. |
Still happening to me on 2.9.0. An
but that's painful because I don't actually want to refresh the model. (the model I have is perfectly fine!) But with this change all variations of the code above ( |
Still happening to me on 2.10.2 but with a bit different stack trace:
I suppose this issue should be reopened. |
Same problem in 2.10.0
|
I'm seeing the same stack trace as @slava-dzyba on 2.10.2 as well. |
Seeing the same error in 2.12.1. Tried the above workarounds and proposed fixes but to no avail. |
Also seeing this error on 2.14. |
My workaround for this is to manually abort the transition before transitioning into another route.
Seems to work on Ember 2.18 and 3.0 versions. Hope this helps. |
Same problem with 3.3 when using
|
Hey! I can confirm that @jorblume workaround works in Ember 2.18 Best regards, |
I solved it by returning just right after
If |
I hit same error but in my case I had to replace usage of
After adding the nested route with Ember.js v3.5.0. |
Any update on this? Haven't been able to get any of the workarounds to work. On Ember v3.4.0 |
Seeing this in ember 3.26.1 |
Hopefully the title and this JS bin say it all. The TL;DR; is if you call
transitionTo
in beforeModel on an initial load (hitting the refresh button) query params will be reset to there default state.http://emberjs.jsbin.com/kicoro/1#
The text was updated successfully, but these errors were encountered: