Skip to content

Commit 9130cb6

Browse files
committed
fix: light optimizations
1 parent b8877a9 commit 9130cb6

8 files changed

Lines changed: 275 additions & 235 deletions

File tree

assets/js/bibtex.js

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -247,15 +247,35 @@ function parseBibTeXEntry(entry) {
247247

248248
async function fetchBibTeXFile(filePath) {
249249
try {
250-
// Get base URL from the document
251-
const baseURL = document.documentElement.getAttribute('data-base-url') || '';
252-
// Ensure filePath starts with /
253-
const normalizedPath = filePath.startsWith('/') ? filePath : `/${filePath}`;
254-
// Combine base URL with file path
255-
const fullPath = `${baseURL}${normalizedPath}`;
250+
// Improved base URL detection
251+
const baseURL = document.documentElement.getAttribute('data-base-url') ||
252+
document.querySelector('meta[name="base-url"]')?.content ||
253+
window.location.origin;
254+
255+
// Handle relative paths more robustly
256+
let normalizedPath = filePath;
257+
if (!filePath.startsWith('http') && !filePath.startsWith('/')) {
258+
normalizedPath = `/${filePath}`;
259+
}
260+
261+
// Construct full URL, avoiding double slashes
262+
const fullPath = filePath.startsWith('http') ?
263+
filePath :
264+
`${baseURL.replace(/\/$/, '')}${normalizedPath}`;
265+
266+
console.log(`Fetching BibTeX from: ${fullPath}`);
267+
268+
const response = await fetch(fullPath, {
269+
cache: 'default',
270+
headers: {
271+
'Accept': 'text/plain,text/x-bibtex,*/*'
272+
}
273+
});
274+
275+
if (!response.ok) {
276+
throw new Error(`Failed to fetch BibTeX file: ${response.status} ${response.statusText}`);
277+
}
256278

257-
const response = await fetch(fullPath);
258-
if (!response.ok) throw new Error(`Failed to fetch BibTeX file: ${response.statusText}`);
259279
return await response.text();
260280
} catch (error) {
261281
console.error('Error loading BibTeX file:', error);

assets/js/utils.js

Lines changed: 53 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -270,13 +270,30 @@ const domUtils = {
270270
// Batch DOM reads and writes
271271
batchDOMOperations(reads = [], writes = []) {
272272
return new Promise(resolve => {
273-
requestIdleCallback(() => {
273+
// Fallback for browsers without requestIdleCallback
274+
const idleCallback = window.requestIdleCallback ||
275+
((cb) => setTimeout(cb, 16));
276+
277+
idleCallback(() => {
274278
// Batch all reads first
275-
const readResults = reads.map(read => read());
279+
const readResults = reads.map(read => {
280+
try {
281+
return read();
282+
} catch (error) {
283+
console.warn('DOM read operation failed:', error);
284+
return null;
285+
}
286+
});
276287

277288
// Then batch all writes
278289
requestAnimationFrame(() => {
279-
writes.forEach(write => write());
290+
writes.forEach(write => {
291+
try {
292+
write();
293+
} catch (error) {
294+
console.warn('DOM write operation failed:', error);
295+
}
296+
});
280297
resolve(readResults);
281298
});
282299
});
@@ -287,59 +304,39 @@ const domUtils = {
287304
isElementVisible(element, threshold = 0) {
288305
if (!element || !element.getBoundingClientRect) return false;
289306

290-
const rect = element.getBoundingClientRect();
291-
const windowHeight = window.innerHeight || document.documentElement.clientHeight;
292-
const windowWidth = window.innerWidth || document.documentElement.clientWidth;
293-
294-
return (
295-
rect.bottom >= threshold &&
296-
rect.right >= threshold &&
297-
rect.top <= windowHeight - threshold &&
298-
rect.left <= windowWidth - threshold
299-
);
307+
try {
308+
const rect = element.getBoundingClientRect();
309+
const windowHeight = window.innerHeight || document.documentElement.clientHeight;
310+
const windowWidth = window.innerWidth || document.documentElement.clientWidth;
311+
312+
return (
313+
rect.bottom >= threshold &&
314+
rect.right >= threshold &&
315+
rect.top <= windowHeight - threshold &&
316+
rect.left <= windowWidth - threshold
317+
);
318+
} catch (error) {
319+
console.warn('Visibility check failed:', error);
320+
return false;
321+
}
300322
},
301323

302-
// Intersection Observer with idle callback
303-
createIdleObserver(callback, options = {}) {
304-
const idleOptions = {
305-
threshold: options.threshold || 0.1,
306-
rootMargin: options.rootMargin || '50px'
307-
};
324+
// Intersection Observer with idle callback and fallback
325+
createIntersectionObserver(callback, options = {}) {
326+
if (!window.IntersectionObserver) {
327+
// Fallback for older browsers
328+
console.warn('IntersectionObserver not supported, using fallback');
329+
return {
330+
observe: () => {},
331+
unobserve: () => {},
332+
disconnect: () => {}
333+
};
334+
}
308335

309-
return new IntersectionObserver((entries) => {
310-
requestIdleCallback(() => {
311-
callback(entries);
312-
});
313-
}, idleOptions);
314-
},
315-
316-
// Wait for element to exist
317-
waitForElement(selector, timeout = 5000) {
318-
return new Promise((resolve, reject) => {
319-
const element = document.querySelector(selector);
320-
if (element) {
321-
resolve(element);
322-
return;
323-
}
324-
325-
const observer = new MutationObserver(() => {
326-
const element = document.querySelector(selector);
327-
if (element) {
328-
observer.disconnect();
329-
clearTimeout(timeoutId);
330-
resolve(element);
331-
}
332-
});
333-
334-
const timeoutId = setTimeout(() => {
335-
observer.disconnect();
336-
reject(new Error(`Element ${selector} not found within ${timeout}ms`));
337-
}, timeout);
338-
339-
observer.observe(document.body, {
340-
childList: true,
341-
subtree: true
342-
});
336+
return new IntersectionObserver(callback, {
337+
threshold: 0.1,
338+
rootMargin: '50px',
339+
...options
343340
});
344341
}
345342
};
@@ -474,3 +471,6 @@ if (typeof performance !== 'undefined') {
474471
if (typeof module !== 'undefined' && module.exports) {
475472
module.exports = window.PKBUtils;
476473
}
474+
if (typeof module !== 'undefined' && module.exports) {
475+
module.exports = window.PKBUtils;
476+
}

data/seo.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ meta:
3333
# Security and Privacy Headers
3434
security:
3535
# Note: Analytics domains will be dynamically resolved from site.BaseURL in templates
36-
content_security_policy_base: "default-src 'self'; script-src 'self' 'unsafe-inline' https://cdnjs.cloudflare.com https://cdn.jsdelivr.net https://d3js.org https://www.google-analytics.com http://localhost:8080; img-src 'self' data: https: http://localhost:8080; style-src 'self' 'unsafe-inline'; font-src 'self' https: https://fonts.gstatic.com; connect-src 'self' http://localhost:8080; frame-src 'self'; worker-src 'self'; media-src 'self'"
36+
content_security_policy_base: "default-src 'self'; script-src 'self' 'unsafe-inline' 'unsafe-eval' https://cdnjs.cloudflare.com https://cdn.jsdelivr.net https://d3js.org https://www.google-analytics.com http://localhost:8080; img-src 'self' data: https: http://localhost:8080; style-src 'self' 'unsafe-inline'; font-src 'self' https: https://fonts.gstatic.com; connect-src 'self' http://localhost:8080; frame-src 'self'; worker-src 'self'; media-src 'self'"
3737
# Analytics domain pattern - will be resolved dynamically
3838
analytics_subdomain: "analytics" # Will create analytics.{your-domain.com}
3939
referrer_policy: "strict-origin-when-cross-origin"

layouts/partials/head.html

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -61,11 +61,8 @@
6161
{{ end }}
6262
{{ end }}
6363

64-
{{/* Load external scripts properly without document.write */}}
65-
{{ if .Page.Params.math }}
64+
{{ partial "analytics/matomo.html" . }}
6665
<script async src="https://cdn.jsdelivr.net/npm/@tensorflow/tfjs@4.11.0/dist/tf.min.js"></script>
6766
<script async src="https://d3js.org/d3.v7.min.js"></script>
68-
{{ end }}
6967

70-
{{ partial "analytics/matomo.html" . }}
7168
</head>

layouts/partials/head/meta.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
{{ $analyticsHost = printf "https://%s.%s" $subdomain $baseURL.Host }}
1414
{{ end }}
1515

16-
{{ $cspBase := site.Data.seo.security.content_security_policy_base | default "default-src 'self'; script-src 'self' 'unsafe-inline' https://cdnjs.cloudflare.com https://cdn.jsdelivr.net https://d3js.org http://localhost:8080; style-src 'self' 'unsafe-inline'; img-src 'self' data: https: http://localhost:8080; font-src 'self' https:; connect-src 'self' http://localhost:8080; frame-src 'self'; worker-src 'self'; media-src 'self'" }}
16+
{{ $cspBase := site.Data.seo.security.content_security_policy_base | default "default-src 'self'; script-src 'self' 'unsafe-inline' 'unsafe-eval' https://cdnjs.cloudflare.com https://cdn.jsdelivr.net https://d3js.org http://localhost:8080; style-src 'self' 'unsafe-inline'; img-src 'self' data: https: http://localhost:8080; font-src 'self' https:; connect-src 'self' http://localhost:8080; frame-src 'self'; worker-src 'self'; media-src 'self'" }}
1717

1818
{{ if $analyticsHost }}
1919
{{ $cspBase = replace $cspBase "script-src 'self' 'unsafe-inline'" (printf "script-src 'self' 'unsafe-inline' %s" $analyticsHost) }}

0 commit comments

Comments
 (0)