前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >合并压缩js,css利器,珞樱http concat开源

合并压缩js,css利器,珞樱http concat开源

作者头像
Zeal
发布2020-11-11 16:05:14
1.2K0
发布2020-11-11 16:05:14
举报
文章被收录于专栏:Hyperledger实践Hyperledger实践

Luoying web framework

Luoying web framework contains a bundle of components to accelerate J2EE development.

Github地址

https://github.com/zealzeng/luoying-web

Maven地址

代码语言:javascript
复制
<dependency>
  <groupId>com.whlylc</groupId>
  <artifactId>luoying-web</artifactId>
  <version>0.1.6</version>

</dependency>

http concat组件

主流的http1.1算是短连接,合并js,css为单个文件,使用浏览器缓存,gzip传输等手段能有效的减少浏览器和服务器的交互次数和减小传输数据,在有限的资源下可以提高一下网站响应速度和负载。

小开发团队折腾不起前后端分离,享受不到大前端webpack,gulp带来的福利。

阿里的nginx concat可惜只支持本地文件,做反向代理时无能为力。

MVC后端的写页面不讲究,是有一些开源的组件是可以合并js和css,但不尽人意,多个css的url语法的相对路径没处理,不支持ETag缓存,gzip, 没考虑热更新等。

所以造了luoying concat这个轮子,团队内部用得还行。

Servlet使用例子

代码语言:javascript
复制
package com.whlylc.web.concat;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.nio.charset.StandardCharsets;

/**
 * Created by Zeal on 2019/4/9 0009.
 */
public class ConcatServlet extends HttpServlet {

    private ResourceConcat resourceConcat = null;

    @Override
    public void init() throws ServletException {
        ServletContext servletContext = this.getServletContext();
        resourceConcat = new ResourceConcat();
        //默认不开启gzip,资源文件超过最小值(这里设置10K)才生效
        resourceConcat.setCompressionMinSize(10 * 1024);
        //资源文件最好能统一个编码,建议为默认的UTF-8
        resourceConcat.setResourceEncoding(StandardCharsets.UTF_8);
        //开启gzip时使用的Transfer-Encoding为chunked,无gzip时Content-Length才设置,这个参数通常不用设置
        //resourceConcat.setResponseBufferSize(10 * 1024);
        try {
            //开启资源文件监控,使用NIO WatchService,可实时更新内存中的缓存的资源文件内容,默认采访过的js,css加载到内存.
            //watchResources()方法不指定特定目录则监控除去WEB-INF外的所有文件夹
            //部署后基本不会变化的不开启省些资源
            resourceConcat.watchResources(this.getServletContext(), "/js", "/css");
        } catch (IOException e) {
            throw new ServletException(e);
        }
    }

    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resourceConcat.concat(req, resp);
    }

}


Spring MVC使用例子,用ResourceConcatFactoryBean注入,ConcatCtrl中使用

代码语言:javascript
复制
package com.whlylc.web.concat;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.FactoryBean;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.web.context.WebApplicationContext;

import java.nio.charset.Charset;

/**
 * Resource concat spring factory bean
 * Created by Zeal on 2019/4/10 0010.
 */
public class ResourceConcatFactoryBean implements FactoryBean<ResourceConcat>,ApplicationContextAware,InitializingBean,DisposableBean {

    private ResourceConcat resourceConcat = new ResourceConcat();

    private String[] resourcePaths = null;

    private WebApplicationContext webApplicationContext = null;

    public ResourceConcatFactoryBean setCompressionMinSize(int compressionMinSize) {
        resourceConcat.setCompressionMinSize(compressionMinSize);
        return this;
    }

    public ResourceConcatFactoryBean setResourceEncoding(Charset resourceEncoding) {
        this.resourceConcat.setResourceEncoding(resourceEncoding);
        return this;
    }

    public ResourceConcatFactoryBean setResponseBufferSize(int size) {
        this.resourceConcat.setResponseBufferSize(size);
        return this;
    }

    public ResourceConcatFactoryBean setWatchResourcePaths(String... resourcePaths) {
        this.resourcePaths = resourcePaths;
        return this;
    }

    @Override
    public ResourceConcat getObject() throws Exception {
        return this.resourceConcat;
    }

    @Override
    public Class<?> getObjectType() {
        return ResourceConcat.class;
    }

    @Override
    public boolean isSingleton() {
        return true;
    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        if (applicationContext instanceof WebApplicationContext) {
            this.webApplicationContext = (WebApplicationContext) applicationContext;
        }
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        if (this.resourcePaths != null && this.resourcePaths.length > 0) {
            this.resourceConcat.watchResources(this.webApplicationContext.getServletContext(), this.resourcePaths);
        }
    }

    @Override
    public void destroy() throws Exception {
        this.resourceConcat.destroy();
    }
}


代码语言:javascript
复制
@Controller
public class ConcatCtrl {

    @Autowired
    private ResourceConcat resourceOptimizer = null;

    @RequestMapping("/jscss")
    @ResponseBody
    public void jscss(HttpServletRequest request, HttpServletResponse response) throws IOException {
        this.resourceOptimizer.concat(request, response);
    }
}

Enjoy it!

本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2019-04-13,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 Hyperledger实践 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • Luoying web framework
    • Github地址
      • Maven地址
        • http concat组件
        领券
        问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档