Skip to content

Commit

Permalink
Merge pull request #19510 from sandstrom/deprecate-auto-location
Browse files Browse the repository at this point in the history
  • Loading branch information
rwjblue authored Nov 5, 2021
2 parents 4f120c8 + f74ef2a commit 90b5d20
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 14 deletions.
16 changes: 15 additions & 1 deletion packages/@ember/-internals/routing/lib/location/api.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { assert } from '@ember/debug';
import { assert, deprecate } from '@ember/debug';

export interface EmberLocation {
implementation: string;
Expand Down Expand Up @@ -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);
},

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) === '/'
Expand Down
32 changes: 32 additions & 0 deletions packages/@ember/-internals/routing/lib/system/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -822,6 +822,22 @@ class EmberRouter extends EmberObject.extend(Evented) implements Evented {
if ('string' === typeof location) {
let resolvedLocation = owner.lookup<IEmberLocation>(`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 {
Expand All @@ -843,6 +859,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();
}

Expand Down
29 changes: 17 additions & 12 deletions packages/@ember/-internals/routing/tests/system/router_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');

Expand All @@ -117,11 +117,13 @@ moduleFor(
location.global = { onhashchange() {} };
location.history = null;

createRouter({
settings: {
location: 'auto',
rootURL: '/rootdir/',
},
expectDeprecation(() => {
createRouter({
settings: {
location: 'auto',
rootURL: '/rootdir/',
},
});
});
}

Expand Down Expand Up @@ -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');

Expand All @@ -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/',
},
});
});
}

Expand Down

0 comments on commit 90b5d20

Please sign in to comment.