Skip to content

Commit

Permalink
FIX: Ensure discovery-categories always clears PreloadStore (#18157)
Browse files Browse the repository at this point in the history
If the server is configured to use a view like `categories_and_latest_topics`, it will preload data for the topic list. If the client doesn't actually use it (e.g. on mobile), then that preloaded data would remain cached and be used for the next loaded topic list. This commit ensures it's always removed, and adds a test for the behaviour.

https://meta.discourse.org/t/237126/35
  • Loading branch information
davidtaylorhq committed Sep 1, 2022
1 parent 0f70eae commit a335492
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ const DiscoveryCategoriesRoute = DiscourseRoute.extend(OpenComposer, {
return this._findCategoriesAndTopics("latest");
} else if (style === "categories_and_top_topics") {
return this._findCategoriesAndTopics("top");
} else {
// The server may have serialized this. Based on the logic above, we don't need it
// so remove it to avoid it being used later by another TopicList route.
PreloadStore.remove("topic_list");
}

return CategoryList.list(this.store);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ import {
import { visit } from "@ember/test-helpers";
import { test } from "qunit";

import PreloadStore from "discourse/lib/preload-store";
import discoveryFixtures from "discourse/tests/fixtures/discovery-fixtures";
import { cloneJSON } from "discourse-common/lib/object";

acceptance("Categories - 'categories_only'", function (needs) {
needs.settings({
desktop_category_page_style: "categories_only",
Expand Down Expand Up @@ -99,3 +103,41 @@ acceptance(
});
}
);

acceptance("Categories - preloadStore handling", function () {
const styles = [
"categories_only",
"categories_with_featured_topics",
"categories_and_latest_topics_created_date",
"categories_and_latest_topics",
"categories_and_top_topics",
"categories_boxes",
"categories_boxes_with_topics",
"subcategories_with_featured_topics",
];

for (const style of styles) {
test(`${style} deletes data from PreloadStore to ensure it isn't left for another route`, async function (assert) {
this.siteSettings.desktop_category_page_style = style;
PreloadStore.store(
"topic_list",
cloneJSON(discoveryFixtures["/latest.json"])
);
PreloadStore.store(
"categories_list",
cloneJSON(discoveryFixtures["/categories.json"])
);

await visit(`/categories`);

assert.true(
PreloadStore.get("topic_list") === undefined,
`topic_list is removed from preloadStore for ${style}`
);
assert.true(
PreloadStore.get("categories_list") === undefined,
`topic_list is removed from preloadStore for ${style}`
);
});
}
});

1 comment on commit a335492

@discoursebot
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This commit has been mentioned on Discourse Meta. There might be relevant details there:

https://meta.discourse.org/t/clicking-on-a-category-doesnt-filter-the-right-topics/237126/41

Please sign in to comment.