Skip to content

Commit

Permalink
Fix non-canonical crate name pages
Browse files Browse the repository at this point in the history
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 registering a custom identifier generation method.
  • Loading branch information
Turbo87 committed Jan 13, 2025
1 parent 3f361f1 commit 20d368e
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 1 deletion.
24 changes: 24 additions & 0 deletions app/app.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { setIdentifierGenerationMethod } from '@ember-data/store';
import Application from '@ember/application';

import loadInitializers from 'ember-load-initializers';
Expand All @@ -23,3 +24,26 @@ export default class App extends Application {
}

loadInitializers(App, config.modulePrefix);

function toCanonicalName(name) {
return name.toLowerCase().replace(/_/g, '-');
}

function generateIdentifier(resource) {
let { id, lid, type } = resource;
if (lid) {
return lid;
}

if (id) {
if (type === 'crate') {
id = toCanonicalName(id);
}

return `@lid:${type}-${id}`;
}

return crypto.randomUUID();
}

setIdentifierGenerationMethod(generateIdentifier);
16 changes: 15 additions & 1 deletion e2e/acceptance/crate.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { test, expect } from '@/e2e/helper';
import { expect, test } from '@/e2e/helper';

test.describe('Acceptance | crate page', { tag: '@acceptance' }, () => {
test('visiting a crate page from the front page', async ({ page, mirage }) => {
Expand Down Expand Up @@ -139,6 +139,20 @@ test.describe('Acceptance | crate page', { tag: '@acceptance' }, () => {
await expect(page.locator('[data-test-try-again]')).toBeVisible();
});

test('works for non-canonical names', async ({ page, mirage }) => {
await mirage.addHook(server => {
let crate = server.create('crate', { name: 'foo-bar' });
server.create('version', { crate });
});

await page.goto('/crates/foo_bar');

await expect(page).toHaveURL('/crates/foo_bar');
await expect(page).toHaveTitle('foo-bar - crates.io: Rust Package Registry');

await expect(page.locator('[data-test-heading] [data-test-crate-name]')).toHaveText('foo-bar');
});

test('navigating to the all versions page', async ({ page, mirage }) => {
await mirage.addHook(server => {
server.loadFixtures();
Expand Down
13 changes: 13 additions & 0 deletions tests/acceptance/crate-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,19 @@ module('Acceptance | crate page', function (hooks) {
assert.dom('[data-test-try-again]').exists();
});

test('works for non-canonical names', async function (assert) {
let crate = this.server.create('crate', { name: 'foo-bar' });
this.server.create('version', { crate });

await visit('/crates/foo_bar');

assert.strictEqual(currentURL(), '/crates/foo_bar');
assert.strictEqual(currentRouteName(), 'crate.index');
assert.strictEqual(getPageTitle(), 'foo-bar - crates.io: Rust Package Registry');

assert.dom('[data-test-heading] [data-test-crate-name]').hasText('foo-bar');
});

test('navigating to the all versions page', async function (assert) {
this.server.loadFixtures();

Expand Down

0 comments on commit 20d368e

Please sign in to comment.