A platform for community discussion. Free, open, simple.
DEV: Add `full-page-refresh-on-navigation` value transformer (#41436)
Rafael dos Santos Silva1daf5fe99392
diff --git a/frontend/discourse/app/lib/registry/transformers.js b/frontend/discourse/app/lib/registry/transformers.jsindex b11da2ec3c4..6df4d8cff01 100644--- a/frontend/discourse/app/lib/registry/transformers.js+++ b/frontend/discourse/app/lib/registry/transformers.js@@ -69,6 +69,7 @@ export const VALUE_TRANSFORMERS = Object.freeze(["flag-custom-placeholder","flag-description","flag-formatted-name",+ "full-page-refresh-on-navigation","hamburger-dropdown-click-outside-exceptions","header-notifications-avatar-size","home-logo-href",diff --git a/frontend/discourse/app/lib/url.js b/frontend/discourse/app/lib/url.jsindex e7f0e3e7da3..b59c996785e 100644--- a/frontend/discourse/app/lib/url.js+++ b/frontend/discourse/app/lib/url.js@@ -249,7 +249,15 @@ class DiscourseURL extends EmberObject {}}- if (Session.currentProp("requiresRefresh") && !this.isComposerOpen) {+ let shouldRefresh =+ Session.currentProp("requiresRefresh") && !this.isComposerOpen;+ shouldRefresh = applyValueTransformer(+ "full-page-refresh-on-navigation",+ shouldRefresh,+ { url: path }+ );++ if (shouldRefresh) {return this.redirectTo(path);}diff --git a/frontend/discourse/app/ui-kit/d-nav-item.gjs b/frontend/discourse/app/ui-kit/d-nav-item.gjsindex f97b438ea09..510e24b977c 100644--- a/frontend/discourse/app/ui-kit/d-nav-item.gjs+++ b/frontend/discourse/app/ui-kit/d-nav-item.gjs@@ -6,6 +6,7 @@ import { trustHTML } from "@ember/template";import { isPresent } from "@ember/utils";import getURL from "discourse/lib/get-url";import { iconHTML } from "discourse/lib/icon-library";+import { applyValueTransformer } from "discourse/lib/transformer";import dConcatClass from "discourse/ui-kit/helpers/d-concat-class";import { i18n } from "discourse-i18n";@@ -73,13 +74,20 @@ export default class DNavItem extends Component {}get refreshHref() {- if (!this.session.requiresRefresh || !this.args.route) {+ if (!this.args.route) {return null;}try {- return this.args.routeParam+ const href = this.args.routeParam? getURL(this.router.urlFor(this.args.route, this.args.routeParam)): getURL(this.router.urlFor(this.args.route));++ const shouldRefresh = applyValueTransformer(+ "full-page-refresh-on-navigation",+ this.session.requiresRefresh,+ { url: href }+ );+ return shouldRefresh ? href : null;} catch {return null;}diff --git a/frontend/discourse/tests/unit/lib/url-test.js b/frontend/discourse/tests/unit/lib/url-test.jsindex 8789ada1054..7b68b550cf9 100644--- a/frontend/discourse/tests/unit/lib/url-test.js+++ b/frontend/discourse/tests/unit/lib/url-test.js@@ -2,6 +2,7 @@ import { setupTest } from "ember-qunit";import { module, test } from "qunit";import sinon from "sinon";import { setPrefix } from "discourse/lib/get-url";+import { withPluginApi } from "discourse/lib/plugin-api";import DiscourseURL, {getCanonicalUrl,getCategoryAndTagUrl,@@ -9,6 +10,7 @@ import DiscourseURL, {prefixProtocol,userPath,} from "discourse/lib/url";+import Session from "discourse/models/session";import { logIn } from "discourse/tests/helpers/qunit-helpers";module("Unit | Utility | url", function (hooks) {@@ -329,6 +331,43 @@ module("Unit | Utility | url", function (hooks) {}});+ test("routeTo full page loads when a refresh is required", async function (assert) {+ sinon.stub(DiscourseURL, "isComposerOpen").get(() => false);+ sinon.stub(DiscourseURL, "redirectTo");+ sinon.stub(DiscourseURL, "handleURL");++ try {+ Session.currentProp("requiresRefresh", true);++ DiscourseURL.routeTo("/t/some-topic/123");+ assert.true(+ DiscourseURL.redirectTo.calledWith("/t/some-topic/123"),+ "does a full page load when a refresh is required"+ );++ DiscourseURL.redirectTo.resetHistory();++ withPluginApi((api) => {+ api.registerValueTransformer(+ "full-page-refresh-on-navigation",+ () => false+ );+ });++ DiscourseURL.routeTo("/t/some-topic/123");+ assert.false(+ DiscourseURL.redirectTo.called,+ "the transformer can defer the full page load"+ );+ assert.true(+ DiscourseURL.handleURL.calledWith("/t/some-topic/123"),+ "navigates within the app instead"+ );+ } finally {+ Session.resetCurrent(null);+ }+ });+test("anchor handling", async function (assert) {sinon.stub(DiscourseURL, "jumpToElement");sinon.stub(DiscourseURL, "replaceState");