This is an automated email from the ASF dual-hosted git repository. svenmeier pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/wicket.git
The following commit(s) were added to refs/heads/master by this push: new c6fde82 WICKET-6953 defer non-ajax only c6fde82 is described below commit c6fde82ce9637c43753b59620b2f5551ad7f20fe Author: Sven Meier <svenme...@apache.org> AuthorDate: Wed Feb 2 22:01:55 2022 +0100 WICKET-6953 defer non-ajax only --- .../head/filter/JavaScriptDeferHeaderResponse.java | 33 ++++++++++------------ .../wicket/markup/head/filter/DeferredPage.html | 1 + .../wicket/markup/head/filter/DeferredPage.java | 9 +++++- .../markup/head/filter/DeferredPageExpected.html | 9 +++--- .../head/filter/DeferredPage_AjaxExpected.html | 18 ++++++++++++ .../head/filter/FilteringHeaderResponseTest.java | 6 +++- 6 files changed, 52 insertions(+), 24 deletions(-) diff --git a/wicket-core/src/main/java/org/apache/wicket/markup/head/filter/JavaScriptDeferHeaderResponse.java b/wicket-core/src/main/java/org/apache/wicket/markup/head/filter/JavaScriptDeferHeaderResponse.java index c00519f..e0cdacc 100644 --- a/wicket-core/src/main/java/org/apache/wicket/markup/head/filter/JavaScriptDeferHeaderResponse.java +++ b/wicket-core/src/main/java/org/apache/wicket/markup/head/filter/JavaScriptDeferHeaderResponse.java @@ -16,6 +16,7 @@ */ package org.apache.wicket.markup.head.filter; +import org.apache.wicket.ajax.AjaxRequestTarget; import org.apache.wicket.core.util.string.JavaScriptUtils; import org.apache.wicket.markup.head.AbstractJavaScriptReferenceHeaderItem; import org.apache.wicket.markup.head.HeaderItem; @@ -26,8 +27,8 @@ import org.apache.wicket.markup.head.JavaScriptHeaderItem; import org.apache.wicket.markup.head.OnDomReadyHeaderItem; import org.apache.wicket.markup.head.OnLoadHeaderItem; import org.apache.wicket.markup.html.DecoratingHeaderResponse; -import org.apache.wicket.page.PartialPageUpdate; import org.apache.wicket.request.Response; +import org.apache.wicket.request.cycle.RequestCycle; import org.apache.wicket.util.string.Strings; /** @@ -60,18 +61,20 @@ public class JavaScriptDeferHeaderResponse extends DecoratingHeaderResponse @Override public void render(HeaderItem item) { - while (item instanceof IWrappedHeaderItem) { - item = ((IWrappedHeaderItem)item).getWrapped(); - } + if (RequestCycle.get().find(AjaxRequestTarget.class).isEmpty()) { + while (item instanceof IWrappedHeaderItem) { + item = ((IWrappedHeaderItem)item).getWrapped(); + } - if (item instanceof AbstractJavaScriptReferenceHeaderItem) { - ((AbstractJavaScriptReferenceHeaderItem)item).setDefer(true); - } else if (item instanceof JavaScriptContentHeaderItem) { - item = new NativeOnDomContentLoadedHeaderItem(((JavaScriptContentHeaderItem)item).getJavaScript()); - } else if (item instanceof OnDomReadyHeaderItem) { - item = new NativeOnDomContentLoadedHeaderItem(((OnDomReadyHeaderItem)item).getJavaScript()); - } else if (item instanceof OnLoadHeaderItem) { - item = new NativeOnLoadHeaderItem(((OnLoadHeaderItem)item).getJavaScript()); + if (item instanceof AbstractJavaScriptReferenceHeaderItem) { + ((AbstractJavaScriptReferenceHeaderItem)item).setDefer(true); + } else if (item instanceof JavaScriptContentHeaderItem) { + item = new NativeOnDomContentLoadedHeaderItem(((JavaScriptContentHeaderItem)item).getJavaScript()); + } else if (item instanceof OnDomReadyHeaderItem) { + item = new NativeOnDomContentLoadedHeaderItem(((OnDomReadyHeaderItem)item).getJavaScript()); + } else if (item instanceof OnLoadHeaderItem) { + item = new NativeOnLoadHeaderItem(((OnLoadHeaderItem)item).getJavaScript()); + } } super.render(item); @@ -79,9 +82,6 @@ public class JavaScriptDeferHeaderResponse extends DecoratingHeaderResponse /** * A specialization that uses native "DOMContentLoaded" events without dependency to external JavaScript. - * <p> - * For Ajax requests we utilize the fact, that {@link PartialPageUpdate} renders {@link #getJavaScript()} only, - * thus executing the JavaScript directly without any event registration. */ private static class NativeOnDomContentLoadedHeaderItem extends OnDomReadyHeaderItem { @@ -113,9 +113,6 @@ public class JavaScriptDeferHeaderResponse extends DecoratingHeaderResponse /** * A specialization that uses native "load" events without dependency to external JavaScript - * <p> - * For Ajax requests we utilize the fact, that {@link PartialPageUpdate} renders {@link #getJavaScript()} only, - * thus executing the JavaScript directly without any event registration. */ private static class NativeOnLoadHeaderItem extends OnLoadHeaderItem { diff --git a/wicket-core/src/test/java/org/apache/wicket/markup/head/filter/DeferredPage.html b/wicket-core/src/test/java/org/apache/wicket/markup/head/filter/DeferredPage.html index ee4a485..17099a5 100644 --- a/wicket-core/src/test/java/org/apache/wicket/markup/head/filter/DeferredPage.html +++ b/wicket-core/src/test/java/org/apache/wicket/markup/head/filter/DeferredPage.html @@ -1,4 +1,5 @@ <html xmlns:wicket="http://wicket.apache.org/dtds.data/wicket-xhtml1.4-strict.dtd" > <body> + <div wicket:id="c"></div> </body> </html> diff --git a/wicket-core/src/test/java/org/apache/wicket/markup/head/filter/DeferredPage.java b/wicket-core/src/test/java/org/apache/wicket/markup/head/filter/DeferredPage.java index b5e3850..ac8c4fe 100644 --- a/wicket-core/src/test/java/org/apache/wicket/markup/head/filter/DeferredPage.java +++ b/wicket-core/src/test/java/org/apache/wicket/markup/head/filter/DeferredPage.java @@ -18,6 +18,7 @@ package org.apache.wicket.markup.head.filter; import org.apache.wicket.ajax.AjaxEventBehavior; import org.apache.wicket.ajax.AjaxRequestTarget; +import org.apache.wicket.markup.html.WebMarkupContainer; import org.apache.wicket.markup.html.WebPage; /** @@ -27,6 +28,8 @@ public class DeferredPage extends WebPage { private static final long serialVersionUID = 1L; + public AjaxEventBehavior behavior; + /** * Construct. * @@ -34,12 +37,16 @@ public class DeferredPage extends WebPage */ public DeferredPage() { - add(new AjaxEventBehavior("click") { + WebMarkupContainer c = new WebMarkupContainer("c"); + add(c); + + c.add(behavior = new AjaxEventBehavior("click") { private static final long serialVersionUID = 1L; @Override protected void onEvent(AjaxRequestTarget target) { + target.add(c); } }); } diff --git a/wicket-core/src/test/java/org/apache/wicket/markup/head/filter/DeferredPageExpected.html b/wicket-core/src/test/java/org/apache/wicket/markup/head/filter/DeferredPageExpected.html index e4305c5..f39ee24 100644 --- a/wicket-core/src/test/java/org/apache/wicket/markup/head/filter/DeferredPageExpected.html +++ b/wicket-core/src/test/java/org/apache/wicket/markup/head/filter/DeferredPageExpected.html @@ -1,6 +1,6 @@ <html xmlns:wicket="http://wicket.apache.org/dtds.data/wicket-xhtml1.4-strict.dtd" > - <head><script type="text/javascript" defer="defer" src="../resource/org.apache.wicket.resource.JQueryResourceReference/jquery/jquery-3.6.0.js"></script> -<script type="text/javascript" defer="defer" src="../resource/org.apache.wicket.ajax.AbstractDefaultAjaxBehavior/res/js/wicket-ajax-jquery.js"></script> + <head><script type="text/javascript" defer="defer" src="./resource/org.apache.wicket.resource.JQueryResourceReference/jquery/jquery-3.6.0.js"></script> +<script type="text/javascript" defer="defer" src="./resource/org.apache.wicket.ajax.AbstractDefaultAjaxBehavior/res/js/wicket-ajax-jquery.js"></script> <script type="text/javascript"> /*<![CDATA[*/ document.addEventListener('DOMContentLoaded', function() { Wicket.Log.enabled=true;; }); @@ -8,17 +8,18 @@ document.addEventListener('DOMContentLoaded', function() { Wicket.Log.enabled=tr </script> <script type="text/javascript"> /*<![CDATA[*/ -document.addEventListener('DOMContentLoaded', function() { Wicket.Ajax.baseUrl="wicket/bookmarkable/org.apache.wicket.markup.head.filter.DeferredPage?0";; }); +document.addEventListener('DOMContentLoaded', function() { Wicket.Ajax.baseUrl="wicket/page?0";; }); /*]]>*/ </script> <script type="text/javascript"> /*<![CDATA[*/ document.addEventListener('DOMContentLoaded', function() { -Wicket.Ajax.ajax({"u":"./org.apache.wicket.markup.head.filter.DeferredPage?0-1.0-","e":"click"});; +Wicket.Ajax.ajax({"u":"./page?0-1.0-c","c":"c1","e":"click"});; Wicket.Event.publish(Wicket.Event.Topic.AJAX_HANDLERS_BOUND); ; }); /*]]>*/ </script> </head><body> + <div wicket:id="c" id="c1"></div> </body> </html> diff --git a/wicket-core/src/test/java/org/apache/wicket/markup/head/filter/DeferredPage_AjaxExpected.html b/wicket-core/src/test/java/org/apache/wicket/markup/head/filter/DeferredPage_AjaxExpected.html new file mode 100644 index 0000000..db06f8d --- /dev/null +++ b/wicket-core/src/test/java/org/apache/wicket/markup/head/filter/DeferredPage_AjaxExpected.html @@ -0,0 +1,18 @@ +<?xml version="1.0" encoding="UTF-8"?><ajax-response><component id="c1" ><![CDATA[<div wicket:id="c" id="c1"></div>]]></component><header-contribution><![CDATA[<head xmlns:wicket="http://wicket.apache.org"><script type="text/javascript" src="./resource/org.apache.wicket.resource.JQueryResourceReference/jquery/jquery-3.6.0.js"></script> +<script type="text/javascript" src="./resource/org.apache.wicket.ajax.AbstractDefaultAjaxBehavior/res/js/wicket-ajax-jquery.js"></script> +<script type="text/javascript" id="wicket-ajax-debug-enable"> +/*<![CDATA[*/ +Wicket.Log.enabled=true; +/*]]]]><![CDATA[>*/ +</script> +<script type="text/javascript" id="wicket-ajax-base-url"> +/*<![CDATA[*/ +Wicket.Ajax.baseUrl="wicket/page?0-1.0-c"; +/*]]]]><![CDATA[>*/ +</script> +</head>]]></header-contribution><evaluate><![CDATA[<head xmlns:wicket="http://wicket.apache.org"><script type="text/javascript"> +/*<![CDATA[*/ +(function(){Wicket.Ajax.ajax({"u":"./page?0-1.0-c","c":"c1","e":"click"});})(); +/*]]]]><![CDATA[>*/ +</script> +</head>]]></evaluate></ajax-response> \ No newline at end of file diff --git a/wicket-core/src/test/java/org/apache/wicket/markup/head/filter/FilteringHeaderResponseTest.java b/wicket-core/src/test/java/org/apache/wicket/markup/head/filter/FilteringHeaderResponseTest.java index 610abbc..dcbdbb7 100644 --- a/wicket-core/src/test/java/org/apache/wicket/markup/head/filter/FilteringHeaderResponseTest.java +++ b/wicket-core/src/test/java/org/apache/wicket/markup/head/filter/FilteringHeaderResponseTest.java @@ -113,7 +113,11 @@ class FilteringHeaderResponseTest extends WicketTestCase { tester.getApplication().getHeaderResponseDecorators() .add(response -> new JavaScriptDeferHeaderResponse(response)); - executeTest(DeferredPage.class, "DeferredPageExpected.html"); + + DeferredPage page = new DeferredPage(); + executeTest(page, "DeferredPageExpected.html"); + + executeBehavior(page.behavior, "DeferredPage_AjaxExpected.html"); } /**