Skip to content

Commit c88f4a4

Browse files
committed
Auto merge of #2405 - Turbo87:readme, r=locks
Improve README loading code The `readme_path` property is on the `version` resource, but we currently cache the README file content per crate. This PR cleans up the README loading code to cache the README per version instead, which means that we now show the correct README file corresponding to the selected version. r? @locks
2 parents 2ccfbd6 + 20a5eb0 commit c88f4a4

File tree

4 files changed

+32
-22
lines changed

4 files changed

+32
-22
lines changed

app/controllers/crate/version.js

Lines changed: 16 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -125,27 +125,24 @@ export default Controller.extend({
125125
return data;
126126
}),
127127

128+
readme: alias('loadReadmeTask.last.value'),
129+
128130
loadReadmeTask: task(function* () {
129-
if (this.currentVersion.get('readme_path')) {
130-
try {
131-
let r = yield fetch(this.currentVersion.get('readme_path'));
132-
if (r.ok) {
133-
this.crate.set('readme', yield r.text());
134-
135-
if (typeof document !== 'undefined') {
136-
setTimeout(() => {
137-
let e = document.createEvent('CustomEvent');
138-
e.initCustomEvent('hashchange', true, true);
139-
window.dispatchEvent(e);
140-
});
141-
}
142-
} else {
143-
this.crate.set('readme', null);
144-
}
145-
} catch (error) {
146-
this.crate.set('readme', null);
147-
}
131+
let version = this.currentVersion;
132+
133+
let readme = version.loadReadmeTask.lastSuccessful
134+
? version.loadReadmeTask.lastSuccessful.value
135+
: yield version.loadReadmeTask.perform();
136+
137+
if (typeof document !== 'undefined') {
138+
setTimeout(() => {
139+
let e = document.createEvent('CustomEvent');
140+
e.initCustomEvent('hashchange', true, true);
141+
window.dispatchEvent(e);
142+
});
148143
}
144+
145+
return readme;
149146
}),
150147

151148
documentationLink: computed(

app/models/version.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,4 +39,15 @@ export default Model.extend({
3939

4040
return { normal, build, dev };
4141
}).keepLatest(),
42+
43+
loadReadmeTask: task(function* () {
44+
if (this.readme_path) {
45+
let response = yield fetch(this.readme_path);
46+
if (!response.ok) {
47+
throw new Error(`README request for ${this.crateName} v${this.num} failed`);
48+
}
49+
50+
return yield response.text();
51+
}
52+
}).keepLatest(),
4253
});

app/routes/crate/version.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,9 @@ export default Route.extend({
6262
this._super(...arguments);
6363

6464
model.version.loadDepsTask.perform();
65-
controller.loadReadmeTask.perform();
65+
controller.loadReadmeTask.perform().catch(() => {
66+
// ignored
67+
});
6668

6769
let { crate } = model;
6870
if (!crate.documentation || crate.documentation.startsWith('https://docs.rs/')) {

app/templates/crate/version.hbs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,9 @@
5656
<CrateTomlCopy @copyText={{this.crateTomlText}} />
5757
{{/if}}
5858
</div>
59-
{{#if this.crate.readme}}
59+
{{#if this.readme}}
6060
<section local-class="crate-readme" aria-label="Readme" {{highlight-syntax selector="pre > code"}}>
61-
{{html-safe this.crate.readme}}
61+
{{html-safe this.readme}}
6262
</section>
6363
{{else}}
6464
{{#if this.crate.description}}

0 commit comments

Comments
 (0)