去掉js緩存,為引入的js添加版本號(hào),使用Thymeleaf 自定義標(biāo)簽方案
Posted on 2021-04-19 23:10 海綿谷 閱讀(799) 評(píng)論(0) 收藏 舉報(bào)作用:在html中,給引用的js文件動(dòng)態(tài)添加版本號(hào),可以去掉js緩存,保證使用的js始終是最新的js 。這次方案是使用自定義標(biāo)簽完成。
不讓頁(yè)面緩存這些文件方法其實(shí)很多,但我們經(jīng)常用的就這幾樣,這里我用到的是在資源后面加版本號(hào)來(lái)實(shí)現(xiàn)資源不緩存的功能,具體代碼如下
package com.nglee.house.config.custom;
import org.springframework.stereotype.Component;
import org.thymeleaf.dialect.AbstractProcessorDialect;
import org.thymeleaf.processor.IProcessor;
import org.thymeleaf.standard.StandardDialect;
import java.util.HashSet;
import java.util.Set;
/**
* @author ngLee
* @version 1.0
* @Desc
* @date 2021/4/19 21:32
*/
@Component
public class CustomTag extends AbstractProcessorDialect {
/**
* 定義方言名稱(chēng)
*/
private static final String NAME = "系統(tǒng)自定義標(biāo)簽";
private static final String prefix = "Ngl";
protected CustomTag() {
super(NAME, prefix, StandardDialect.PROCESSOR_PRECEDENCE);
}
@Override
public Set<IProcessor> getProcessors(final String dialectPrefix) {
final Set<IProcessor> processor = new HashSet<>();
//<Fw:select>標(biāo)簽
processor.add(new CustomTagSelect(prefix));
return processor;
}
}
package com.nglee.house.config.custom;
import org.thymeleaf.context.ITemplateContext;
import org.thymeleaf.model.IModel;
import org.thymeleaf.model.IModelFactory;
import org.thymeleaf.model.IProcessableElementTag;
import org.thymeleaf.processor.element.AbstractElementTagProcessor;
import org.thymeleaf.processor.element.IElementTagStructureHandler;
import org.thymeleaf.templatemode.TemplateMode;
import java.time.Instant;
/**
* @author ngLee
* @version 1.0
* @Desc
* @date 2021/4/19 21:40
*/
public class CustomTagSelect extends AbstractElementTagProcessor {
@Override
protected void doProcess(ITemplateContext iTemplateContext, IProcessableElementTag iProcessableElementTag, IElementTagStructureHandler iElementTagStructureHandler) {
//獲取src的引用js路徑
String src = iProcessableElementTag.getAttributeValue("src");
//如果t能加上發(fā)包版本號(hào)更完美
//拼接替換后的內(nèi)容
StringBuffer content = new StringBuffer("<script type='text/javascript' src="+src+"?t="+Instant.now().getEpochSecond() +"></script>");
IModelFactory modelFactory = iTemplateContext.getModelFactory();
IModel model = modelFactory.createModel();
model.add(modelFactory.createText(content));
iElementTagStructureHandler.replaceWith(model, false);
}
private static final String TAG_NAME="script";
private static final int PRECEDENCE = 10000;
public CustomTagSelect(String dialectPrefix) {
super(
// 模板類(lèi)型為HTML
TemplateMode.HTML,
// 標(biāo)簽方言前綴
dialectPrefix,
// 標(biāo)簽名稱(chēng)
TAG_NAME,
// 將標(biāo)簽前綴應(yīng)用于標(biāo)簽名稱(chēng)
true,
// 無(wú)屬性名稱(chēng):將通過(guò)標(biāo)簽名稱(chēng)匹配
null,
// 沒(méi)有要應(yīng)用于屬性名稱(chēng)的前綴
false,
// 優(yōu)先級(jí)
PRECEDENCE
);
}
}
具體使用:
<Ngl:script th:src="@{'/static/js/user/register.js'}" id="s"></Ngl:script>
具體引用效果為:

--本文作者:【ngLee 】
--關(guān)于博文:如果有錯(cuò)誤的地方,還請(qǐng)留言指正。如轉(zhuǎn)載請(qǐng)注明出處!如果您覺(jué)得文章對(duì)您有幫助,可以點(diǎn)擊文章右下角【推薦】一下。您的鼓勵(lì)是博主的最大動(dòng)力!
浙公網(wǎng)安備 33010602011771號(hào)