Skip to content

Commit 876bf65

Browse files
feat: 添加日志输出,增强页面加载过程的可追踪性
Signed-off-by: wangsimiao1 <wangsimiao1@xiaomi.com>
1 parent 2944312 commit 876bf65

7 files changed

Lines changed: 60 additions & 2 deletions

File tree

_javascript/categories.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
import { basic, initSidebar, initTopbar } from './modules/layouts';
22
import { categoryCollapse } from './modules/components';
33

4+
console.log('[Blog] categories.js loaded');
5+
46
basic();
57
initSidebar();
68
initTopbar();
79
categoryCollapse();
10+
11+
console.log('[Blog] Categories page fully initialized');

_javascript/commons.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,15 @@
11
import { basic, initSidebar, initTopbar, initPjax } from './modules/layouts';
22

3+
console.log('[Blog] commons.js loaded');
4+
35
initSidebar();
6+
console.log('[Blog] Sidebar initialized');
7+
48
initTopbar();
9+
console.log('[Blog] Topbar initialized');
10+
511
basic();
12+
console.log('[Blog] Basic modules initialized');
13+
614
initPjax();
15+
console.log('[Blog] PJAX initialized');

_javascript/home.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11
import { basic, initSidebar, initTopbar } from './modules/layouts';
22
import { initLocaleDatetime, loadImg } from './modules/components';
33

4+
console.log('[Blog] home.js loaded');
5+
46
loadImg();
57
initLocaleDatetime();
68
initSidebar();
79
initTopbar();
810
basic();
11+
12+
console.log('[Blog] Home page fully initialized');

_javascript/misc.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
import { basic, initSidebar, initTopbar } from './modules/layouts';
22
import { initLocaleDatetime } from './modules/components';
33

4+
console.log('[Blog] misc.js loaded');
5+
46
initSidebar();
57
initTopbar();
68
initLocaleDatetime();
79
basic();
10+
11+
console.log('[Blog] Misc page fully initialized');

_javascript/modules/layouts/pjax.js

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,16 @@ function updateActiveNav(path) {
3232
}
3333

3434
async function navigate(url, pushState = true) {
35-
if (isNavigating) return;
35+
if (isNavigating) {
36+
console.log('[PJAX] Navigation skipped — already navigating');
37+
return;
38+
}
3639
isNavigating = true;
40+
console.log(`[PJAX] Navigating to: ${url}`);
3741

3842
const container = document.getElementById(CONTAINER_ID);
3943
if (!container) {
44+
console.warn('[PJAX] #swup container not found, falling back to full page load');
4045
window.location.href = url;
4146
return;
4247
}
@@ -46,6 +51,7 @@ async function navigate(url, pushState = true) {
4651

4752
try {
4853
// Fade out main content only
54+
console.log('[PJAX] Fading out main content');
4955
if (mainContent) {
5056
mainContent.style.transition = `opacity ${TRANSITION_DURATION}ms ease-in-out`;
5157
mainContent.style.opacity = '0';
@@ -55,7 +61,9 @@ async function navigate(url, pushState = true) {
5561

5662
// Fetch new page
5763
const response = await fetch(url);
64+
console.log(`[PJAX] Fetch response: ${response.status}`);
5865
if (!response.ok) {
66+
console.warn(`[PJAX] Fetch failed (${response.status}), falling back`);
5967
window.location.href = url;
6068
return;
6169
}
@@ -66,31 +74,42 @@ async function navigate(url, pushState = true) {
6674

6775
const newContainer = doc.getElementById(CONTAINER_ID);
6876
if (!newContainer) {
77+
console.warn('[PJAX] New page has no #swup container, falling back');
6978
window.location.href = url;
7079
return;
7180
}
7281

7382
// Wait for fade out to finish
7483
await new Promise((r) => setTimeout(r, TRANSITION_DURATION));
84+
console.log('[PJAX] Fade out complete, replacing DOM');
7585

7686
// Replace main content
7787
const newMain = newContainer.querySelector('main');
7888
if (mainContent && newMain) {
7989
mainContent.innerHTML = newMain.innerHTML;
90+
console.log('[PJAX] ✓ Main content replaced');
91+
} else {
92+
console.warn('[PJAX] ✗ Main content not found', { mainContent: !!mainContent, newMain: !!newMain });
8093
}
8194

8295
// Silently replace sidebar (no animation)
8396
const panel = container.querySelector('#panel-wrapper');
8497
const newPanel = newContainer.querySelector('#panel-wrapper');
8598
if (panel && newPanel) {
8699
panel.innerHTML = newPanel.innerHTML;
100+
console.log('[PJAX] ✓ Panel replaced (silent)');
101+
} else {
102+
console.warn('[PJAX] ✗ Panel not found', { panel: !!panel, newPanel: !!newPanel });
87103
}
88104

89105
// Replace tail/footer
90106
const tail = container.querySelector('#tail-wrapper');
91107
const newTail = newContainer.querySelector('#tail-wrapper');
92108
if (tail && newTail) {
93109
tail.innerHTML = newTail.innerHTML;
110+
console.log('[PJAX] ✓ Tail/footer replaced (silent)');
111+
} else {
112+
console.warn('[PJAX] ✗ Tail not found', { tail: !!tail, newTail: !!newTail });
94113
}
95114

96115
// Update title
@@ -103,6 +122,7 @@ async function navigate(url, pushState = true) {
103122

104123
// Update sidebar active state
105124
updateActiveNav(new URL(url, window.location.origin).pathname);
125+
console.log('[PJAX] Sidebar nav updated');
106126

107127
// Scroll to top
108128
window.scrollTo({ top: 0 });
@@ -113,14 +133,18 @@ async function navigate(url, pushState = true) {
113133
} else {
114134
container.style.opacity = '1';
115135
}
136+
console.log(`[PJAX] ✓ Navigation complete: ${url}`);
116137
} catch (e) {
138+
console.error('[PJAX] Navigation error:', e);
117139
window.location.href = url;
118140
} finally {
119141
isNavigating = false;
120142
}
121143
}
122144

123145
export function initPjax() {
146+
console.log('[PJAX] Setting up event listeners');
147+
124148
// Intercept clicks on internal links
125149
document.addEventListener('click', (e) => {
126150
const link = e.target.closest('a');
@@ -129,13 +153,18 @@ export function initPjax() {
129153
e.preventDefault();
130154

131155
// Skip if same page
132-
if (link.href === window.location.href) return;
156+
if (link.href === window.location.href) {
157+
console.log('[PJAX] Same page click ignored:', link.href);
158+
return;
159+
}
133160

161+
console.log('[PJAX] Link intercepted:', link.href);
134162
navigate(link.href);
135163
});
136164

137165
// Handle browser back/forward
138166
window.addEventListener('popstate', () => {
167+
console.log('[PJAX] Popstate (back/forward):', window.location.href);
139168
navigate(window.location.href, false);
140169
});
141170
}

_javascript/page.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,14 @@ import {
66
loadMermaid
77
} from './modules/components';
88

9+
console.log('[Blog] page.js loaded');
10+
911
loadImg();
1012
imgPopup();
1113
initSidebar();
1214
initTopbar();
1315
initClipboard();
1416
loadMermaid();
1517
basic();
18+
19+
console.log('[Blog] Page fully initialized');

_javascript/post.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ import {
99
loadMermaid
1010
} from './modules/components';
1111

12+
console.log('[Blog] post.js loaded');
13+
1214
loadImg();
1315
initToc();
1416
imgPopup();
@@ -18,3 +20,5 @@ initClipboard();
1820
initTopbar();
1921
loadMermaid();
2022
basic();
23+
24+
console.log('[Blog] Post page fully initialized');

0 commit comments

Comments
 (0)