From cec2980d97cfc5f5cfded08eb3f67bb90b033753 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alexander=20Sandstr=C3=B6m?= Date: Thu, 25 Mar 2021 21:51:51 +0100 Subject: [PATCH 1/3] Deprecate auto location --- .../-internals/routing/lib/system/router.ts | 16 ++++++++++ .../routing/tests/system/router_test.js | 29 +++++++++++-------- 2 files changed, 33 insertions(+), 12 deletions(-) diff --git a/packages/@ember/-internals/routing/lib/system/router.ts b/packages/@ember/-internals/routing/lib/system/router.ts index cb6a6d4ece7..dc7a07f7f61 100644 --- a/packages/@ember/-internals/routing/lib/system/router.ts +++ b/packages/@ember/-internals/routing/lib/system/router.ts @@ -859,6 +859,22 @@ class EmberRouter extends EmberObject.extend(Evented) implements Evented { if ('string' === typeof location) { let resolvedLocation = owner.lookup(`location:${location}`); + if (location === 'auto') { + deprecate( + "Router location 'auto' is deprecated. Most users will want to set `locationType` to 'history' in config/environment.js for no change in behavior. See deprecation docs for details.", + false, + { + id: 'deprecate-auto-location', + until: '5.0.0', + url: 'https://emberjs.com/deprecations/v3.x#toc_deprecate-auto-location', + for: 'ember-source', + since: { + enabled: '4.1.0', + }, + } + ); + } + if (resolvedLocation !== undefined) { location = set(this, 'location', resolvedLocation); } else { diff --git a/packages/@ember/-internals/routing/tests/system/router_test.js b/packages/@ember/-internals/routing/tests/system/router_test.js index 8a82387e405..fcad45e2bc6 100644 --- a/packages/@ember/-internals/routing/tests/system/router_test.js +++ b/packages/@ember/-internals/routing/tests/system/router_test.js @@ -98,7 +98,7 @@ moduleFor( } ['@test replacePath should be called with the right path'](assert) { - assert.expect(1); + assert.expect(2); let location = owner.lookup('location:auto'); @@ -117,11 +117,13 @@ moduleFor( location.global = { onhashchange() {} }; location.history = null; - createRouter({ - settings: { - location: 'auto', - rootURL: '/rootdir/', - }, + expectDeprecation(() => { + createRouter({ + settings: { + location: 'auto', + rootURL: '/rootdir/', + }, + }); }); } @@ -177,7 +179,7 @@ moduleFor( } ["@test AutoLocation should replace the url when it's not in the preferred format"](assert) { - assert.expect(1); + assert.expect(2); let location = owner.lookup('location:auto'); @@ -191,16 +193,19 @@ moduleFor( assert.equal(url, 'http://test.com/rootdir/#/welcome'); }, }; + location.history = null; location.global = { onhashchange() {}, }; - createRouter({ - settings: { - location: 'auto', - rootURL: '/rootdir/', - }, + expectDeprecation(() => { + createRouter({ + settings: { + location: 'auto', + rootURL: '/rootdir/', + }, + }); }); } From 5af5b9f1393e856bc7f8ab0c6c68e71cb3c57b4e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alexander=20Sandstr=C3=B6m?= Date: Thu, 25 Mar 2021 21:54:33 +0100 Subject: [PATCH 2/3] Deprecate calling Location.create This functionality has been deprecated since 2014, but there wasn't any deprecation message before. See commit #1d1d0db4 for details. --- .../-internals/routing/lib/location/api.ts | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/packages/@ember/-internals/routing/lib/location/api.ts b/packages/@ember/-internals/routing/lib/location/api.ts index cc4ac1d2040..5b8e16c2a59 100644 --- a/packages/@ember/-internals/routing/lib/location/api.ts +++ b/packages/@ember/-internals/routing/lib/location/api.ts @@ -1,4 +1,4 @@ -import { assert } from '@ember/debug'; +import { assert, deprecate } from '@ember/debug'; export interface EmberLocation { implementation: string; @@ -111,6 +111,20 @@ export default { Boolean(implementationClass) ); + deprecate( + "Calling `create` on Location class is deprecated. Instead, use `container.lookup('location:my-location')` to lookup the location you need.", + false, + { + id: 'deprecate-auto-location', + until: '5.0.0', + url: 'https://emberjs.com/deprecations/v3.x#toc_deprecate-auto-location', + for: 'ember-source', + since: { + enabled: '4.1.0', + }, + } + ); + return implementationClass.create(...arguments); }, From f74ef2a845d223705b4e227e3ab200f94e0c12d7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alexander=20Sandstr=C3=B6m?= Date: Thu, 25 Mar 2021 22:27:14 +0100 Subject: [PATCH 3/3] Deprecate Location.detect We're only showing the deprecation warning for locations other than 'auto', since auto already have it's own deprecation message. We're also moving the rootURL trailing slash check from detect to initState. That assert has nothing to do with detection, but it was likely added there because that method is called *after* the rootURL value is set on the location (by the router). Luckily, the initState method is also called after that value is set, so we can use that hook instead. We're hijacking a method for something that isn't it's primary purpose, but that was the case with the existing code too, so this seems like a reasonable solution. --- .../routing/lib/location/none_location.ts | 7 ++++++- .../-internals/routing/lib/system/router.ts | 16 ++++++++++++++++ 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/packages/@ember/-internals/routing/lib/location/none_location.ts b/packages/@ember/-internals/routing/lib/location/none_location.ts index a7ea318c471..808e0ed9fc1 100644 --- a/packages/@ember/-internals/routing/lib/location/none_location.ts +++ b/packages/@ember/-internals/routing/lib/location/none_location.ts @@ -38,9 +38,14 @@ export default class NoneLocation extends EmberObject implements EmberLocation { // Set in reopen so it can be overwritten with extend declare rootURL: string; - detect(): void { + initState(): void { + this._super(...arguments); + let { rootURL } = this; + // This assert doesn't have anything to do with state initialization, + // but we're hijacking this method since it's called after the route has + // set the rootURL property on its Location instance. assert( 'rootURL must end with a trailing forward slash e.g. "/app/"', rootURL.charAt(rootURL.length - 1) === '/' diff --git a/packages/@ember/-internals/routing/lib/system/router.ts b/packages/@ember/-internals/routing/lib/system/router.ts index dc7a07f7f61..c27a46b4734 100644 --- a/packages/@ember/-internals/routing/lib/system/router.ts +++ b/packages/@ember/-internals/routing/lib/system/router.ts @@ -896,6 +896,22 @@ class EmberRouter extends EmberObject.extend(Evented) implements Evented { // detecting history support. This gives it a chance to set its // `cancelRouterSetup` property which aborts routing. if (typeof location.detect === 'function') { + if (this.location !== 'auto') { + deprecate( + 'The `detect` method on the Location object is deprecated. If you need detection you can run your detection code in app.js, before setting the location type.', + false, + { + id: 'deprecate-auto-location', + until: '5.0.0', + url: 'https://emberjs.com/deprecations/v3.x#toc_deprecate-auto-location', + for: 'ember-source', + since: { + enabled: '4.1.0', + }, + } + ); + } + location.detect(); }