Add pre-fetch data handling for resources, users, and comments

This commit is contained in:
2025-07-14 11:47:59 +08:00
parent 6188b88917
commit 0ef8e14587
6 changed files with 60 additions and 13 deletions

View File

@@ -54,6 +54,11 @@ export default function CommentPage() {
});
return;
}
const preFetchData = app.getPreFetchData();
if (preFetchData?.comment?.id === id) {
setComment(preFetchData.comment);
return;
}
network.getComment(id).then((res) => {
if (res.success) {
setComment(res.data!);

View File

@@ -90,14 +90,19 @@ export default function ResourcePage() {
if (location.state) {
setResource(location.state.resource);
} else {
network.getResourceDetails(id).then((res) => {
if (res.success) {
setResource(res.data!);
document.title = res.data!.title;
} else {
showToast({ message: res.message, type: "error" });
}
});
const preFetchData = app.getPreFetchData();
if (preFetchData?.resource?.id === id) {
setResource(preFetchData.resource);
} else {
network.getResourceDetails(id).then((res) => {
if (res.success) {
setResource(res.data!);
document.title = res.data!.title;
} else {
showToast({ message: res.message, type: "error" });
}
});
}
}
}
}, [id, location.state]);

View File

@@ -14,6 +14,7 @@ import {
MdOutlinePhotoAlbum,
} from "react-icons/md";
import { useTranslation } from "react-i18next";
import { app } from "../app.ts";
export default function UserPage() {
const [user, setUser] = useState<User | null>(null);
@@ -50,6 +51,11 @@ export default function UserPage() {
};
useEffect(() => {
const preFetchData = app.getPreFetchData();
if (preFetchData?.user?.username === username) {
setUser(preFetchData.user);
return;
}
network.getUserInfo(username || "").then((res) => {
if (res.success) {
setUser(res.data!);