Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: first load speed optimization #28

Merged
merged 1 commit into from
Aug 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 30 additions & 22 deletions frontend/src/App.vue
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<router-view />
</template>

<script lang="ts" setup>
<script lang="ts">
import { useAppStore } from 'src/stores/app';
import { onBeforeUnmount, onMounted } from 'vue';
import { useSocketStore } from 'src/stores/websocketStore';
Expand All @@ -12,31 +12,39 @@ import { useUserStore } from 'src/stores/user';
import { useSettingStore } from 'src/stores/setting';
// import { testSatisfies } from 'src/utils/version';

const appStore = useAppStore();
const userStore = useUserStore();
const settingStore = useSettingStore();
const websocketStore = useSocketStore();

onMounted(async () => {
if (!appStore.isPublic) {
// testSatisfies();
websocketStore.start();
export default {
async preFetch() {
const appStore = useAppStore();
const userStore = useUserStore();
const settingStore = useSettingStore();
await appStore.prefetch();
userStore.init();
appStore.init();
settingStore.init();
}
},
setup() {
const appStore = useAppStore();
const websocketStore = useSocketStore();

bus.on(BUS_EVENT.APP_BACKEND_ERROR, onErrorMessage);
});
onMounted(async () => {
if (!appStore.isPublic) {
// testSatisfies();
websocketStore.start();
}

const onErrorMessage = (failureMessage: string) => {
BtNotify.show({
type: NotifyDefinedType.FAILED,
message: failureMessage
});
};
bus.on(BUS_EVENT.APP_BACKEND_ERROR, onErrorMessage);
});

onBeforeUnmount(() => {
bus.off(BUS_EVENT.APP_BACKEND_ERROR, onErrorMessage);
});
const onErrorMessage = (failureMessage: string) => {
BtNotify.show({
type: NotifyDefinedType.FAILED,
message: failureMessage
});
};

onBeforeUnmount(() => {
bus.off(BUS_EVENT.APP_BACKEND_ERROR, onErrorMessage);
});
}
};
</script>
1 change: 1 addition & 0 deletions frontend/src/components/appcard/AppCard.vue
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,7 @@ onMounted(() => {
});

function goAppDetails() {
console.log('click app');
if (props.disabled) {
return;
}
Expand Down
64 changes: 37 additions & 27 deletions frontend/src/pages/application/CategoryPage.vue
Original file line number Diff line number Diff line change
Expand Up @@ -192,43 +192,53 @@ const topLoading = ref(true);
const latestLoading = ref(true);
const { t } = useI18n();

onMounted(async () => {
onMounted(() => {
topTitle.value = `Top App in ${categoryRef.value}`;
latestTitle.value = `Latest App in ${categoryRef.value}`;
await fetchData(true);
fetchData(true);
});

async function fetchData(showLoading = false) {
function fetchData(showLoading = false) {
if (showLoading) {
pageLoading.value = true;
topLoading.value = true;
latestLoading.value = true;
}
const all = await appStore.getPageData(categoryRef.value);
if (all && all.data) {
console.log(all.data);
pageData.value = all.data;
pageLoading.value = false;
appStore
.getPageData(categoryRef.value)
.then((all) => {
if (all && all.data) {
pageData.value = all.data;
const top = pageData.value.find(
(item: any) => item.type === 'Default Topic' && item.id === 'Hottest'
);
if (top) {
getTop(categoryRef.value.toLowerCase())
.then((list) => {
topApps.value = list;
})
.finally(() => {
topLoading.value = false;
});
}

const top = pageData.value.find(
(item: any) => item.type === 'Default Topic' && item.id === 'Hottest'
);
if (top) {
topApps.value = await getTop(categoryRef.value.toLowerCase());
topLoading.value = false;
}

const latest = pageData.value.find(
(item: any) => item.type === 'Default Topic' && item.id === 'Newest'
);
if (latest) {
latestApps.value = await getLatest(categoryRef.value.toLowerCase());
latestLoading.value = false;
}
}
pageLoading.value = false;
topLoading.value = false;
latestLoading.value = false;
const latest = pageData.value.find(
(item: any) => item.type === 'Default Topic' && item.id === 'Newest'
);
if (latest) {
getLatest(categoryRef.value.toLowerCase())
.then((list) => {
latestApps.value = list;
})
.finally(() => {
latestLoading.value = false;
});
}
}
})
.finally(() => {
pageLoading.value = false;
});
}

const clickList = (type: string) => {
Expand Down
64 changes: 37 additions & 27 deletions frontend/src/pages/application/HomePage.vue
Original file line number Diff line number Diff line change
Expand Up @@ -206,42 +206,52 @@ const pageData = ref();
const appStore = useAppStore();
const { t } = useI18n();

onMounted(async () => {
await fetchData(true);
onMounted(() => {
fetchData(true);
});

async function fetchData(showLoading = false) {
function fetchData(showLoading = false) {
if (showLoading) {
pageLoading.value = true;
topLoading.value = true;
latestLoading.value = true;
}
const all = await appStore.getPageData(CATEGORIES_TYPE.LOCAL.ALL);
console.log(all);
if (all && all.data) {
console.log(all.data);
pageData.value = all.data;
pageLoading.value = false;
appStore
.getPageData(CATEGORIES_TYPE.LOCAL.ALL)
.then((all) => {
if (all && all.data) {
pageData.value = all.data;

const top = pageData.value.find(
(item: any) => item.type === 'Default Topic' && item.id === 'Hottest'
);
if (top) {
topApps.value = await getTop();
topLoading.value = false;
}
const top = pageData.value.find(
(item: any) => item.type === 'Default Topic' && item.id === 'Hottest'
);
if (top) {
getTop()
.then((list) => {
topApps.value = list;
})
.finally(() => {
topLoading.value = false;
});
}

const latest = pageData.value.find(
(item: any) => item.type === 'Default Topic' && item.id === 'Newest'
);
if (latest) {
latestApps.value = await getLatest();
latestLoading.value = false;
}
}
pageLoading.value = false;
topLoading.value = false;
latestLoading.value = false;
const latest = pageData.value.find(
(item: any) => item.type === 'Default Topic' && item.id === 'Newest'
);
if (latest) {
getLatest()
.then((list) => {
latestApps.value = list;
})
.finally(() => {
latestLoading.value = false;
});
}
}
})
.finally(() => {
pageLoading.value = false;
});
}

const clickList = (type: string) => {
Expand Down
9 changes: 3 additions & 6 deletions frontend/src/stores/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ import { CFG_TYPE } from 'src/constants/config';

export type AppState = {
tempAppMap: Record<string, AppStoreInfo>;
initialized: boolean;
pageData: [] | null;
installApps: AppStoreInfo[];
updateApps: AppStoreInfo[];
Expand All @@ -46,7 +45,6 @@ export const useAppStore = defineStore('app', {
state: () => {
return {
tempAppMap: {},
initialized: false,
installApps: [],
updateApps: [],
pageData: null,
Expand All @@ -55,11 +53,13 @@ export const useAppStore = defineStore('app', {
} as AppState;
},
actions: {
async prefetch() {
this.pageData = await getPage();
},
async init() {
console.log('app init');
await this.loadApps();
console.log('app init requests completed');
this.initialized = true;
},

async loadApps() {
Expand All @@ -68,9 +68,6 @@ export const useAppStore = defineStore('app', {
},

async getPageData(category: string): Promise<any> {
if (!this.pageData) {
this.pageData = await getPage();
}
if (!this.pageData || this.pageData.length == 0) {
return null;
}
Expand Down
Loading