Skip to content

Commit 3a3e699

Browse files
committed
Fix non-canonical crate name pages
Before the Ember Data 5 update it was possible to visit `/crates/foo-bar` when the crate was actually called `foo_bar` and vice-versa. The Ember Data 5 update broke this behavior, and this commit is restoring it, by using `queryRecord()` instead of `findRecord()`, which can no longer deal with requesting a certain `id` and getting a different one returned from the server.
1 parent 2b039e4 commit 3a3e699

File tree

3 files changed

+34
-1
lines changed

3 files changed

+34
-1
lines changed

app/adapters/crate.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,26 @@ export default class CrateAdapter extends ApplicationAdapter {
1414
return super.findRecord(store, type, id, snapshot);
1515
}
1616

17+
queryRecord(store, type, query, adapterOptions) {
18+
let { include } = query;
19+
// This ensures `crate.versions` are always fetched from another request.
20+
if (include === undefined) {
21+
query.include = 'keywords,categories,downloads';
22+
}
23+
return super.queryRecord(store, type, query, adapterOptions);
24+
}
25+
26+
urlForQueryRecord(query) {
27+
let baseUrl = super.urlForQueryRecord(...arguments);
28+
if (!query.name) {
29+
return baseUrl;
30+
}
31+
32+
let crateName = query.name;
33+
delete query.name;
34+
return `${baseUrl}/${crateName}`;
35+
}
36+
1737
groupRecordsForFindMany(store, snapshots) {
1838
let result = [];
1939
for (let i = 0; i < snapshots.length; i += BULK_REQUEST_GROUP_SIZE) {

app/routes/crate.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ export default class CrateRoute extends Route {
1212
let crateName = params.crate_id;
1313

1414
try {
15-
return await this.store.findRecord('crate', crateName);
15+
return await this.store.queryRecord('crate', { name: crateName });
1616
} catch (error) {
1717
if (error instanceof NotFoundError) {
1818
let title = `${crateName}: Crate not found`;

tests/acceptance/crate-test.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,19 @@ module('Acceptance | crate page', function (hooks) {
131131
assert.dom('[data-test-try-again]').exists();
132132
});
133133

134+
test('treats dash and underscore as equal', async function (assert) {
135+
let crate = this.server.create('crate', { name: 'foo-bar' });
136+
this.server.create('version', { crate });
137+
138+
await visit('/crates/foo_bar');
139+
140+
assert.strictEqual(currentURL(), '/crates/foo_bar');
141+
assert.strictEqual(currentRouteName(), 'crate.index');
142+
assert.strictEqual(getPageTitle(), 'foo-bar - crates.io: Rust Package Registry');
143+
144+
assert.dom('[data-test-heading] [data-test-crate-name]').hasText('foo-bar');
145+
});
146+
134147
test('navigating to the all versions page', async function (assert) {
135148
this.server.loadFixtures();
136149

0 commit comments

Comments
 (0)