asset reload HTTP 캐시 보강
This commit is contained in:
@@ -26,6 +26,45 @@
|
||||
return parsed.toString();
|
||||
}
|
||||
|
||||
function sameOriginUrl(url) {
|
||||
try {
|
||||
var parsed = new URL(url, window.location.href);
|
||||
return parsed.origin === window.location.origin ? parsed : null;
|
||||
} catch (e) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
function addAssetToken(element, attr, token) {
|
||||
var current = element && element.getAttribute(attr);
|
||||
var parsed = current ? sameOriginUrl(current) : null;
|
||||
if (!parsed || parsed.searchParams.get('assetReload') === token) return;
|
||||
|
||||
parsed.searchParams.set('assetReload', token);
|
||||
element.setAttribute(attr, parsed.pathname + parsed.search + parsed.hash);
|
||||
}
|
||||
|
||||
function applyAssetToken(token) {
|
||||
if (!token) return;
|
||||
|
||||
document.querySelectorAll('script[src]').forEach(function (element) {
|
||||
addAssetToken(element, 'src', token);
|
||||
});
|
||||
document.querySelectorAll('link[href]').forEach(function (element) {
|
||||
var rel = (element.getAttribute('rel') || '').toLowerCase();
|
||||
if (rel.indexOf('stylesheet') === -1 && rel.indexOf('icon') === -1 && rel.indexOf('manifest') === -1) return;
|
||||
addAssetToken(element, 'href', token);
|
||||
});
|
||||
}
|
||||
|
||||
var currentAssetToken = new URL(window.location.href).searchParams.get('assetReload') || '';
|
||||
if (currentAssetToken) {
|
||||
applyAssetToken(currentAssetToken);
|
||||
new MutationObserver(function () {
|
||||
applyAssetToken(currentAssetToken);
|
||||
}).observe(document.documentElement, { childList: true, subtree: true });
|
||||
}
|
||||
|
||||
function deleteCaches() {
|
||||
if (!window.caches || !window.caches.keys) return Promise.resolve();
|
||||
|
||||
|
||||
Reference in New Issue
Block a user