diff --git a/app/assets/javascripts/discourse/initializers/live-development.js.es6 b/app/assets/javascripts/discourse/initializers/live-development.js.es6 index c6dba5fdfcf..de482647b35 100644 --- a/app/assets/javascripts/discourse/initializers/live-development.js.es6 +++ b/app/assets/javascripts/discourse/initializers/live-development.js.es6 @@ -7,6 +7,28 @@ export default { initialize(container) { const messageBus = container.lookup('message-bus:main'); + if (window.history && window.location.search.indexOf("?preview_theme_key=") === 0) { + // force preview theme key to always be carried along + const themeKey = window.location.search.slice(19).split('&')[0]; + if (themeKey.match(/^[a-z0-9-]+$/i)) { + const patchState = function(f) { + const patched = window.history[f]; + + window.history[f] = function(stateObj, name, url) { + if (url.indexOf("preview_theme_key=") === -1) { + const joiner = url.indexOf("?") === -1 ? "?" : "&"; + url = `${url}${joiner}preview_theme_key=${themeKey}`; + } + + return patched.call(window.history, stateObj, name, url); + }; + }; + patchState("replaceState"); + patchState("pushState"); + } + + } + // Custom header changes $('header.custom').each(function() { const header = $(this); diff --git a/app/controllers/admin/themes_controller.rb b/app/controllers/admin/themes_controller.rb index 77fb2009d1c..99b4caa3bd7 100644 --- a/app/controllers/admin/themes_controller.rb +++ b/app/controllers/admin/themes_controller.rb @@ -7,8 +7,7 @@ class Admin::ThemesController < Admin::AdminController def preview @theme = Theme.find(params[:id]) - - redirect_to path("/"), flash: { preview_theme_key: @theme.key } + redirect_to path("/?preview_theme_key=#{@theme.key}") end def upload_asset diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index e02d7343368..f2a7b475012 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -315,7 +315,7 @@ class ApplicationController < ActionController::Base resolve_safe_mode return if request.env[NO_CUSTOM] - theme_key = flash[:preview_theme_key] + theme_key = request[:preview_theme_key] user_option = current_user&.user_option