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); }, 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 cb6a6d4ece7..c27a46b4734 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 { @@ -880,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(); } 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/', + }, + }); }); }