From 47951152d1a3da4d49d074d948630fc0486f6909 Mon Sep 17 00:00:00 2001
From: radishzzz
Date: Mon, 17 Feb 2025 16:06:00 +0000
Subject: [PATCH] refactor: optimize website performance and layout
- Replace ScrollButton with BackToTop component
- Improve theme toggle and scrollbar event handling
- Update layout and index page styling
- Optimize client:load and client:idle directives
- Remove deprecated Heti addon plugin
---
pnpm-lock.yaml | 16 +-
src/components/BackToTop.astro | 77 ++++++
src/components/PhotoSwipe.astro | 2 +
src/components/ScrollButton.astro | 91 -------
src/components/Scrollbar.astro | 1 -
src/components/ThemeToggle.astro | 81 +++---
src/config.ts | 2 +-
src/layouts/Head.astro | 8 +-
src/layouts/Layout.astro | 14 +-
src/pages/[lang]/index.astro | 2 +-
src/pages/index.astro | 16 +-
src/plugins/heti-addon.js | 412 ------------------------------
src/styles/heti.css | 1 -
13 files changed, 164 insertions(+), 559 deletions(-)
create mode 100644 src/components/BackToTop.astro
delete mode 100644 src/components/ScrollButton.astro
delete mode 100644 src/plugins/heti-addon.js
diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml
index 8732744..d9c91cc 100644
--- a/pnpm-lock.yaml
+++ b/pnpm-lock.yaml
@@ -1175,8 +1175,8 @@ packages:
resolution: {integrity: sha512-bN798gFfQX+viw3R7yrGWRqnrN2oRkEkUjjl4JNn4E8GxxbjtG3FbrEIIY3l8/hrwUwIeCZvi4QuOTP4MErVug==}
engines: {node: '>=12'}
- ansis@3.14.0:
- resolution: {integrity: sha512-R1LnSpYZWMDEFoAyCrfgToVz4ES25luDpjlZsUlD5GXdPWb91U+TZGkxWAOvt+7zWRY/ctOxhtTx5HUtL3qmbA==}
+ ansis@3.15.0:
+ resolution: {integrity: sha512-zIcWDJ+Kwqxfdnogx66Gxzr0kVmCcRAdat9nlY2IHsshqTN4fBH6tMeRMPA/2w0rpBayIJvjQAaa2/4RDrNqwg==}
engines: {node: '>=14'}
anymatch@3.1.3:
@@ -1289,8 +1289,8 @@ packages:
resolution: {integrity: sha512-8WB3Jcas3swSvjIeA2yvCJ+Miyz5l1ZmB6HFb9R1317dt9LCQoswg/BGrmAmkWVEszSrrg4RwmO46qIm2OEnSA==}
engines: {node: '>=16'}
- caniuse-lite@1.0.30001699:
- resolution: {integrity: sha512-b+uH5BakXZ9Do9iK+CkDmctUSEqZl+SP056vc5usa0PL+ev5OHw003rZXcnjNDv3L8P5j6rwT6C0BPKSikW08w==}
+ caniuse-lite@1.0.30001700:
+ resolution: {integrity: sha512-2S6XIXwaE7K7erT8dY+kLQcpa5ms63XlRkMkReXjle+kf6c5g38vyMl+Z5y8dSxOFDhcFe+nxnn261PLxBSQsQ==}
ccount@2.0.1:
resolution: {integrity: sha512-eyrF0jiFpY+3drT6383f1qhkbGsLSifNAjA61IUjZjmLCWjItY6LB9ft9YhoDgwfmclB2zhu51Lc7+95b8NRAg==}
@@ -3767,7 +3767,7 @@ snapshots:
'@typescript-eslint/eslint-plugin': 8.24.0(@typescript-eslint/parser@8.24.0(eslint@9.20.1(jiti@2.4.2))(typescript@5.7.3))(eslint@9.20.1(jiti@2.4.2))(typescript@5.7.3)
'@typescript-eslint/parser': 8.24.0(eslint@9.20.1(jiti@2.4.2))(typescript@5.7.3)
'@vitest/eslint-plugin': 1.1.31(@typescript-eslint/utils@8.24.0(eslint@9.20.1(jiti@2.4.2))(typescript@5.7.3))(eslint@9.20.1(jiti@2.4.2))(typescript@5.7.3)
- ansis: 3.14.0
+ ansis: 3.15.0
eslint: 9.20.1(jiti@2.4.2)
eslint-config-flat-gitignore: 2.0.0(eslint@9.20.1(jiti@2.4.2))
eslint-flat-config-utils: 2.0.1
@@ -4919,7 +4919,7 @@ snapshots:
ansi-styles@6.2.1: {}
- ansis@3.14.0: {}
+ ansis@3.15.0: {}
anymatch@3.1.3:
dependencies:
@@ -5152,7 +5152,7 @@ snapshots:
browserslist@4.24.4:
dependencies:
- caniuse-lite: 1.0.30001699
+ caniuse-lite: 1.0.30001700
electron-to-chromium: 1.5.101
node-releases: 2.0.19
update-browserslist-db: 1.1.2(browserslist@4.24.4)
@@ -5172,7 +5172,7 @@ snapshots:
camelcase@8.0.0: {}
- caniuse-lite@1.0.30001699: {}
+ caniuse-lite@1.0.30001700: {}
ccount@2.0.1: {}
diff --git a/src/components/BackToTop.astro b/src/components/BackToTop.astro
new file mode 100644
index 0000000..0076fe8
--- /dev/null
+++ b/src/components/BackToTop.astro
@@ -0,0 +1,77 @@
+
+
+
+
+
+
diff --git a/src/components/PhotoSwipe.astro b/src/components/PhotoSwipe.astro
index 03b300e..0bd4215 100644
--- a/src/components/PhotoSwipe.astro
+++ b/src/components/PhotoSwipe.astro
@@ -10,6 +10,8 @@
lightbox.destroy()
lightbox = null
}
+ document.removeEventListener('astro:page-load', createPhotoSwipe)
+ document.removeEventListener('astro:before-swap', cleanup)
}
function createPhotoSwipe() {
diff --git a/src/components/ScrollButton.astro b/src/components/ScrollButton.astro
deleted file mode 100644
index 9e73fac..0000000
--- a/src/components/ScrollButton.astro
+++ /dev/null
@@ -1,91 +0,0 @@
-
-
-
diff --git a/src/components/Scrollbar.astro b/src/components/Scrollbar.astro
index 8f38683..79704d1 100644
--- a/src/components/Scrollbar.astro
+++ b/src/components/Scrollbar.astro
@@ -41,7 +41,6 @@ document.addEventListener('astro:before-swap', () => {
scrollbarsInstance = null
})
-document.addEventListener('DOMContentLoaded', initScrollbar)
document.addEventListener('astro:page-load', initScrollbar)
diff --git a/src/components/ThemeToggle.astro b/src/components/ThemeToggle.astro
index af1b001..3e2f058 100644
--- a/src/components/ThemeToggle.astro
+++ b/src/components/ThemeToggle.astro
@@ -1,12 +1,29 @@
+
+
+}
-
+// 集中管理所有事件监听器
+function initThemeEvents() {
+ const themeToggle = document.querySelector('button[aria-pressed]') as HTMLButtonElement
+ if (themeToggle) {
+ themeToggle.addEventListener('click', toggleTheme)
+ }
+
+ document.addEventListener('astro:after-swap', handleAfterSwap)
+ window.addEventListener('popstate', syncTheme)
+ window.addEventListener('pageshow', handlePageShow)
+}
+
+// 初始化
+initThemeEvents()
+
diff --git a/src/config.ts b/src/config.ts
index cee5a04..d4a2e06 100644
--- a/src/config.ts
+++ b/src/config.ts
@@ -34,7 +34,7 @@ export const themeConfig: ThemeConfig = {
global: {
locale: 'zh', // support 'zh', 'zh-tw', 'ja', 'en', 'es', 'ru'. Default language setting
moreLocale: ['zh-tw', 'ja', 'en', 'es', 'ru'], // ['zh', 'zh-tw', 'ja', 'en', 'es', 'ru']. Not fill in the default locale code again
- fontStyle: 'serif', // sans, serif. Font styles for post content
+ fontStyle: 'sans', // sans, serif. Font styles for post content
titleSpace: 3, // 1, 2, 3. Space between title and subtitle
},
// GLOBAL SETTINGS >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> END
diff --git a/src/layouts/Head.astro b/src/layouts/Head.astro
index 26ece36..32651cd 100644
--- a/src/layouts/Head.astro
+++ b/src/layouts/Head.astro
@@ -80,7 +80,7 @@ function getCurrentTheme() {
return document.documentElement.classList.contains('dark')
}
-function updateThemeColor(isDark) {
+function updateMetaTheme(isDark) {
const metaThemeColor = document.querySelector('meta[name="theme-color"]')
if (metaThemeColor) {
metaThemeColor.setAttribute('content', isDark ? darkMode : lightMode)
@@ -89,7 +89,7 @@ function updateThemeColor(isDark) {
function syncTheme() {
const isDark = getCurrentTheme()
- updateThemeColor(isDark)
+ updateMetaTheme(isDark)
}
function initTheme() {
@@ -105,7 +105,7 @@ function initTheme() {
const isDark = theme === 'dark'
document.documentElement.classList.toggle('dark', isDark)
- updateThemeColor(isDark)
+ updateMetaTheme(isDark)
}
initTheme()
@@ -113,7 +113,7 @@ initTheme()
window.matchMedia('(prefers-color-scheme: dark)').addEventListener('change', (e) => {
if (!localStorage.getItem('theme')) {
document.documentElement.classList.toggle('dark', e.matches)
- updateThemeColor(e.matches)
+ updateMetaTheme(e.matches)
}
})
diff --git a/src/layouts/Layout.astro b/src/layouts/Layout.astro
index ae0f836..2de9884 100644
--- a/src/layouts/Layout.astro
+++ b/src/layouts/Layout.astro
@@ -1,4 +1,5 @@
---
+import BackToTop from '@/components/BackToTop.astro'
import Footer from '@/components/Footer.astro'
import LanguageSwitcher from '@/components/LanguageSwitcher.astro'
import MainHeader from '@/components/MainHeader.astro'
@@ -6,7 +7,6 @@ import MobileHeader from '@/components/MobileHeader.astro'
import Navigation from '@/components/Navbar.astro'
import PhotoSwipe from '@/components/PhotoSwipe.astro'
import Scrollbar from '@/components/Scrollbar.astro'
-import ScrollButton from '@/components/ScrollButton.astro'
import ThemeToggle from '@/components/ThemeToggle.astro'
import themeConfig from '@/config'
import Head from '@/layouts/Head.astro'
@@ -35,8 +35,8 @@ const fontStyle = themeConfig.global.fontStyle === 'serif' ? 'font-serif' : 'fon
@@ -51,10 +51,10 @@ const fontStyle = themeConfig.global.fontStyle === 'serif' ? 'font-serif' : 'fon
-
+
+
-
-
-
+
+
diff --git a/src/pages/[lang]/index.astro b/src/pages/[lang]/index.astro
index 74daee4..744c093 100644
--- a/src/pages/[lang]/index.astro
+++ b/src/pages/[lang]/index.astro
@@ -47,7 +47,7 @@ const postsByYear = await getPostsByYear(lang)
{/* Post Title */}
{post.data.title}
{/* Post Date */}
-