首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
社区首页 >问答首页 >Primefaces javascript延迟解析

Primefaces javascript延迟解析
EN

Stack Overflow用户
提问于 2013-12-11 09:13:21
回答 1查看 947关注 0票数 1

从PageSpeed洞察可以看出,Primefaces 4.0在页面加载过程中产生了大量开销:

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
**605.3KiB of JavaScript is parsed during initial page load. Defer parsing JavaScript to reduce blocking of page rendering.**
http://localhost:8888/.../primefaces.js.xhtml?... (219.5KiB)
http://localhost:8888/.../jquery-plugins.js.xhtml?... (191.8KiB)
http://localhost:8888/.../jquery.js.xhtml?... (95.3KiB)
http://localhost:8888/.../tooltip.js.xhtml?... (34.5KiB)
http://localhost:8888/.../jsf.js.xhtml?... (25.4KiB)
http://localhost:8888/.../primefaces-extensions.js.xhtml?... (19.7KiB)
http://localhost:8888/.../watermark.js.xhtml?... (4.7KiB)
http://localhost:8888/.../hotkey.js.xhtml?... (1.2KiB)

你知道如何将这些第三方javascript文件设置在body部分的底部,而不是head或使用defer/async参数吗?Javascript加载器在这种情况下没有帮助,因为这些加载器来自JSF渲染器。此外,我还尝试为PreRenderView (Best way for JSF to defer parsing of JavaScript?)创建一个监听器,但没有成功。有没有其他方法可以解决这个问题?谢谢你的帮忙!

EN

回答 1

Stack Overflow用户

发布于 2013-12-11 11:38:26

我将脚本移动到以下代码片段中:

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
public class ScriptValidateListener implements SystemEventListener {

    @Override
    public void processEvent(SystemEvent event) throws AbortProcessingException {
        UIViewRoot root = (UIViewRoot) event.getSource();
        FacesContext ctx = FacesContext.getCurrentInstance();
        List<UIComponent> resources = root.getComponentResources(ctx, "HEAD");
        for (UIComponent r : resources) {
            String name = (String) r.getAttributes().get("name");
            if (name == null) {
                continue;
            }

            if (name.contains(".js")) {
                root.removeComponentResource(ctx, r, "HEAD");
                root.addComponentResource(ctx, r, "BODY");
            }
        }
    }

    @Override
    public boolean isListenerForSource(Object source) {
        return (source instanceof UIViewRoot);
    }
}

这会将所有javascripts从头移动到主体的末尾。但。Primefaces存在这样的问题,即呈现的组件将尝试访问JQuery ($)。或者PrimeFaces javascript函数,这将破坏页面上的所有ajax功能。我可能需要决定移动哪些脚本,不移动哪些脚本。另外,作为侦听器的一部分,我需要在faces-config.xml中定义以下内容才能使其工作:

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
<application>
    <system-event-listener>
        <system-event-listener-class>com.example.listener.ScriptValidateListener</system-event-listener-class>
        <system-event-class>javax.faces.event.PreRenderViewEvent</system-event-class>
    </system-event-listener>
</application>
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/20514987

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档
查看详情【社区公告】 技术创作特训营有奖征文