Skip to content

Commit

Permalink
fix(web): 修复CI;修复进度未同步的问题;优化数据加载
Browse files Browse the repository at this point in the history
  • Loading branch information
Xwite committed Oct 15, 2024
1 parent 123d9b4 commit 3132172
Show file tree
Hide file tree
Showing 7 changed files with 11 additions and 33 deletions.
2 changes: 1 addition & 1 deletion modules/web/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
},
"scripts": {
"dev": "vite",
"build": "run-p type-check \"build-only {@}\" --",
"build": "run-p type-check \"build-only {@}\" -- && node ./scripts/sync.js",
"preview": "vite preview",
"build-only": "vite build",
"type-check": "vue-tsc --build --force",
Expand Down
2 changes: 2 additions & 0 deletions modules/web/src/api/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,11 @@ const getReadConfig = async (http_url = legado_http_entry_point) => {
const saveReadConfig = (config: webReadConfig) =>
ajax.post<LeagdoApiResponse<string>>('/saveReadConfig', config)

/** @deprecated: 使用`API.saveBookProgressWithBeacon`以确保在页面或者直接关闭的情况下保存进度 */
const saveBookProgress = (bookProgress: BookProgress) =>
ajax.post('/saveBookProgress', bookProgress)

/**主要在直接关闭浏览器情况下可靠发送书籍进度 */
const saveBookProgressWithBeacon = (bookProgress: BookProgress) => {
if (!bookProgress) return
// 常规请求可能会被取消 使用Fetch keep-alive 或者 navigator.sendBeacon
Expand Down
14 changes: 0 additions & 14 deletions modules/web/src/router/bookRouter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,4 @@ const router = createRouter({
routes: bookRoutes,
})

import { createApp } from 'vue'
import App from '@/App.vue'
import store, { useBookStore } from '@/store'

// init pinia instance
createApp(App).use(store)
router.beforeEach((to, from, next) => {
// 自动加载阅读配置
useBookStore()
.loadWebConfig()
.then(() => next())
.catch(() => next())
})

export default router
14 changes: 0 additions & 14 deletions modules/web/src/router/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,4 @@ router.afterEach(to => {
if (to.name == 'shelf') document.title = '书架'
})

import { createApp } from 'vue'
import App from '@/App.vue'
import store, { useBookStore } from '@/store'

// init pinia instance
createApp(App).use(store)
router.beforeEach((to, from, next) => {
// 自动加载阅读配置
useBookStore()
.loadWebConfig()
.then(() => next())
.catch(() => next())
})

export default router
4 changes: 3 additions & 1 deletion modules/web/src/store/bookStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,9 @@ export const useBookStore = defineStore('book', {
this.bookProgress,
)
}
return API.saveBookProgress(this.bookProgress)
// 直接关闭浏览器时 http请求可能被取消
// return API.saveBookProgress(this.bookProgress)
return API.saveBookProgressWithBeacon(this.bookProgress)
},
},
})
6 changes: 4 additions & 2 deletions modules/web/src/views/BookChapter.vue
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,7 @@ watch(
sessionStorage.setItem('chapterIndex', book.chapterIndex.toString())
sessionStorage.setItem('chapterPos', book.chapterPos.toString())
},
{ deep: 1 },
)
// 无限滚动
Expand Down Expand Up @@ -371,7 +372,7 @@ const saveReadingBookProgressToBrowser = (index: number, pos: number) => {
const onVisibilityChange = () => {
const _bookProgress = bookProgress.value
if (document.visibilityState == 'hidden' && _bookProgress) {
API.saveBookProgressWithBeacon(_bookProgress)
store.saveBookProgress()
}
}
// 定时同步
Expand Down Expand Up @@ -472,7 +473,8 @@ const ignoreKeyPress = (event: {
}
}
onMounted(() => {
onMounted(async () => {
await store.loadWebConfig()
//获取书籍数据
const bookUrl = sessionStorage.getItem('bookUrl')
const name = sessionStorage.getItem('bookName')
Expand Down
2 changes: 1 addition & 1 deletion modules/web/src/views/BookShelf.vue
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,7 @@ const toDetail = (
const loadShelf = async () => {
try {
//await store.loadWebConfig() called in router.beforeEach
await store.loadWebConfig()
await store.saveBookProgress()
//确保各种网络情况下同步请求先完成
await fetchBookShelfData()
Expand Down

0 comments on commit 3132172

Please sign in to comment.