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

Deprecate auto location #19510

Merged
merged 3 commits into from
Nov 5, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -859,6 +859,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 @@ -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();
}

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