Permalink
Browse files

Fix unload fail in Safari (XHR POST was missing explicit content type)

  • Loading branch information...
BalusC committed Jul 19, 2017
1 parent 020bc1a commit 5d47da16250e60bb251a32b94e6afff4c040b96c
@@ -22,6 +22,7 @@
import static org.omnifaces.util.Components.getClosestParent;
import static org.omnifaces.util.Faces.responseComplete;
import static org.omnifaces.util.FacesLocal.getRenderKit;
import static org.omnifaces.util.FacesLocal.getRequestHeader;
import static org.omnifaces.util.FacesLocal.getRequestURI;
import static org.omnifaces.util.FacesLocal.isDevelopment;
import static org.omnifaces.util.FacesLocal.redirectPermanent;
@@ -43,7 +44,6 @@
import org.omnifaces.cdi.viewscope.ViewScopeManager;
import org.omnifaces.taghandler.EnableRestorableView;
import org.omnifaces.util.Callback;
import org.omnifaces.util.FacesLocal;
import org.omnifaces.util.Hacks;
/**
@@ -135,7 +135,7 @@ private UIViewRoot unloadView(FacesContext context, String viewId) {
context.getApplication().publishEvent(context, PreDestroyViewMapEvent.class, UIViewRoot.class, createdView);
Hacks.removeViewState(context, manager, viewId);
}
else if ("XMLHttpRequest".equals(FacesLocal.getRequestHeader(context, "X-Requested-With"))) {
else if ("XMLHttpRequest".equals(getRequestHeader(context, "X-Requested-With"))) {
redirectPermanent(context, getRequestURI(context));
}
@@ -67,15 +67,17 @@ OmniFaces.Unload = (function(Util, navigator, window, document) {
try {
var url = form.action.split(/[?#;]/)[0];
var query = "omnifaces.event=unload&id=" + id + "&" + VIEW_STATE_PARAM + "=" + encodeURIComponent(form[VIEW_STATE_PARAM].value);
var contentType = "application/x-www-form-urlencoded";
if (navigator.sendBeacon) {
// Synchronous XHR is deprecated during unload event, modern browsers offer Beacon API for this which will basically fire-and-forget the request.
navigator.sendBeacon(url, new Blob([query], {type: "application/x-www-form-urlencoded" }));
navigator.sendBeacon(url, new Blob([query], {type: contentType}));
}
else {
var xhr = new XMLHttpRequest();
xhr.open("POST", url, false);
xhr.setRequestHeader("X-Requested-With", "XMLHttpRequest");
xhr.setRequestHeader("Content-Type", contentType);
xhr.send(query);
}
}

0 comments on commit 5d47da1

Please sign in to comment.