This commit is contained in:
1 parent
ac7991a37c
commit
351bc8a226
4 files changed
+65
-7
No files matched your search
@@ -5,6 +5,7 @@
|
||||
"scripts": {
|
||||
"prebuild": "node scripts/generate-abbrlinks.mjs",
|
||||
"build": "astro build",
|
||||
"postbuild": "node scripts/version-service-worker.mjs",
|
||||
"clean": "astro sync",
|
||||
"dev": "astro dev",
|
||||
"preview": "astro preview",
|
||||
|
||||
@@ -5,6 +5,28 @@
|
||||
// 配置区域 - 请根据实际情况修改
|
||||
// ============================================================================
|
||||
const CONFIG = window.SITE_CONFIG?.search;
|
||||
const assets = document.currentScript?.dataset || {};
|
||||
let dependenciesPromise;
|
||||
function loadDependencies() {
|
||||
if (dependenciesPromise) return dependenciesPromise;
|
||||
const css = document.createElement('link');
|
||||
css.rel = 'stylesheet';
|
||||
css.href = assets.searchCss;
|
||||
document.head.appendChild(css);
|
||||
const loadScript = (src) => new Promise((resolve, reject) => {
|
||||
const script = document.createElement('script');
|
||||
script.src = src;
|
||||
script.onload = resolve;
|
||||
script.onerror = () => reject(new Error('搜索依赖加载失败'));
|
||||
document.head.appendChild(script);
|
||||
});
|
||||
dependenciesPromise = Promise.all([loadScript(assets.searchJs), loadScript(assets.adapterJs)]).catch((error) => {
|
||||
dependenciesPromise = null;
|
||||
css.remove();
|
||||
throw error;
|
||||
});
|
||||
return dependenciesPromise;
|
||||
}
|
||||
if (!CONFIG) {
|
||||
console.warn('Typesense 搜索配置未加载');
|
||||
return;
|
||||
@@ -297,7 +319,13 @@
|
||||
document.body.style.overflow = 'hidden';
|
||||
|
||||
if (!isInitialized) {
|
||||
initTypesense();
|
||||
loadDependencies().then(() => {
|
||||
initTypesense();
|
||||
if (isSearchOpen) focusSearchInput();
|
||||
}).catch((error) => {
|
||||
console.error(error);
|
||||
showErrorMessage();
|
||||
});
|
||||
}
|
||||
focusSearchInput();
|
||||
}
|
||||
|
||||
@@ -0,0 +1,31 @@
|
||||
import { createHash } from 'node:crypto';
|
||||
import { readdir, readFile, writeFile } from 'node:fs/promises';
|
||||
import { join, relative, resolve } from 'node:path';
|
||||
|
||||
const root = resolve('dist');
|
||||
const worker = join(root, 'sw.js');
|
||||
const hash = createHash('sha256');
|
||||
const source = await readFile(resolve('public/sw.js'), 'utf8');
|
||||
hash.update(source);
|
||||
hash.update('\0');
|
||||
|
||||
async function collect(directory) {
|
||||
const entries = await readdir(directory, { withFileTypes: true });
|
||||
const files = await Promise.all(entries.map((entry) => {
|
||||
const path = join(directory, entry.name);
|
||||
return entry.isDirectory() ? collect(path) : [path];
|
||||
}));
|
||||
return files.flat();
|
||||
}
|
||||
|
||||
const files = (await collect(root)).filter((path) => path !== worker).sort();
|
||||
for (const path of files) {
|
||||
hash.update(relative(root, path).replaceAll('\\', '/'));
|
||||
hash.update('\0');
|
||||
hash.update(await readFile(path));
|
||||
hash.update('\0');
|
||||
}
|
||||
|
||||
const versioned = source.replace(/^const CACHE_VERSION = '[^']+';/m, `const CACHE_VERSION = '${hash.digest('hex').slice(0, 16)}';`);
|
||||
if (versioned === source) throw new Error('Service worker cache version was not found');
|
||||
await writeFile(worker, versioned);
|
||||
Loaded 3 of 4 files, more files were not shown because too many files have changed in this diff.
Show more
Reference in new issue
Block a user