Skip to content

The Unified and Autonomous Development Platform

Fix the issue that page reloads infinitely on Safari due to different behavior of Safari history API.

robin shine3588e3177013
diff --git a/commons.wicket/pom.xml b/commons.wicket/pom.xml
index 538012a1fd..6340c5a2be 100644
--- a/commons.wicket/pom.xml
+++ b/commons.wicket/pom.xml
@@ -64,6 +64,11 @@
<artifactId>pegdown</artifactId>
<version>1.4.2</version>
</dependency>
+ <dependency>
+ <groupId>org.webjars</groupId>
+ <artifactId>historyjs</artifactId>
+ <version>1.8.0</version>
+ </dependency>
<dependency>
<groupId>org.apache.wicket</groupId>
<artifactId>wicket-bean-validation</artifactId>
diff --git a/commons.wicket/src/main/java/com/pmease/commons/wicket/CommonPage.java b/commons.wicket/src/main/java/com/pmease/commons/wicket/CommonPage.java
index 05dd56f848..1227dabeec 100644
--- a/commons.wicket/src/main/java/com/pmease/commons/wicket/CommonPage.java
+++ b/commons.wicket/src/main/java/com/pmease/commons/wicket/CommonPage.java
@@ -80,15 +80,18 @@ public abstract class CommonPage extends WebPage {
public void renderHead(Component component, IHeaderResponse response) {
super.renderHead(component, response);
+ // Use history.js instead of native history API to solve the problem that Safari
+ // (and previous versions of Chrome) fires event "onpopstate" on initial page load
+ // and this causes the page to reload infinitely with below code
String script = String.format(""
- + "window.onpopstate=function(e) {"
- + " if (e.state != null) {"
+ + "History.Adapter.bind(window, 'statechange', function() {"
+ + " if (History.getState().data.state != null) {"
+ " %s"
+ " } else {"
+ " location.reload();"
+ " }"
- + "};",
- getCallbackFunctionBody(CallbackParameter.resolved("state", "e.state")));
+ + "});",
+ getCallbackFunctionBody(CallbackParameter.resolved("state", "History.getState().data.state")));
response.render(OnDomReadyHeaderItem.forScript(script));
}
diff --git a/commons.wicket/src/main/java/com/pmease/commons/wicket/CommonResourceReference.java b/commons.wicket/src/main/java/com/pmease/commons/wicket/CommonResourceReference.java
index f5dcf82a7b..5a3c8d8d40 100644
--- a/commons.wicket/src/main/java/com/pmease/commons/wicket/CommonResourceReference.java
+++ b/commons.wicket/src/main/java/com/pmease/commons/wicket/CommonResourceReference.java
@@ -14,6 +14,7 @@ import com.google.common.collect.Lists;
import com.pmease.commons.wicket.asset.Assets;
import de.agilecoders.wicket.core.Bootstrap;
+import de.agilecoders.wicket.webjars.request.resource.WebjarsJavaScriptResourceReference;
/**
* Common resource is intended to provide a common look&feel for all products using
@@ -81,6 +82,9 @@ class CommonResourceReference extends JavaScriptResourceReference {
dependencies.add(JavaScriptHeaderItem.forReference(Bootstrap.getSettings().getModernizrResourceReference()));
dependencies.addAll(bootstrapHeaderItems);
+
+ dependencies.add(JavaScriptHeaderItem.forReference(
+ new WebjarsJavaScriptResourceReference("historyjs/current/scripts/bundled/html5/jquery.history.js")));
dependencies.add(JavaScriptHeaderItem.forReference(Assets.ALIGN_JS));
diff --git a/commons.wicket/src/main/java/com/pmease/commons/wicket/component/history/HistoryAwarePanel.java b/commons.wicket/src/main/java/com/pmease/commons/wicket/component/history/HistoryAwarePanel.java
index e61230eb86..323ce6c7a0 100644
--- a/commons.wicket/src/main/java/com/pmease/commons/wicket/component/history/HistoryAwarePanel.java
+++ b/commons.wicket/src/main/java/com/pmease/commons/wicket/component/history/HistoryAwarePanel.java
@@ -36,7 +36,7 @@ public abstract class HistoryAwarePanel extends Panel {
public void pushState(AjaxRequestTarget target, String url, Serializable state) {
HistoryState historyState = new HistoryState(getPageRelativePath(), state);
String encodedHistoryState = new String(Base64.encodeBase64(SerializationUtils.serialize(historyState)));
- target.appendJavaScript(String.format("history.pushState('%s', '', '%s');", encodedHistoryState, url));
+ target.appendJavaScript(String.format("History.pushState({state:'%s'}, '', '%s');", encodedHistoryState, url));
}
}