mirror of
https://github.com/wgh136/nysoure.git
synced 2025-09-27 04:17:23 +00:00
Add pre-fetch data handling for resources, users, and comments
This commit is contained in:
@@ -39,6 +39,7 @@
|
|||||||
window.uploadPrompt = `{{UploadPrompt}}`;
|
window.uploadPrompt = `{{UploadPrompt}}`;
|
||||||
window.allowNormalUserUpload = `{{AllowNormalUserUpload}}`;
|
window.allowNormalUserUpload = `{{AllowNormalUserUpload}}`;
|
||||||
</script>
|
</script>
|
||||||
|
<script id="pre_fetch_data"></script>
|
||||||
<div id="root"></div>
|
<div id="root"></div>
|
||||||
<script type="module" src="/src/main.tsx"></script>
|
<script type="module" src="/src/main.tsx"></script>
|
||||||
</body>
|
</body>
|
||||||
|
@@ -63,6 +63,21 @@ class App {
|
|||||||
canUpload() {
|
canUpload() {
|
||||||
return this.isLoggedIn() && (this.user?.can_upload || this.isAdmin());
|
return this.isLoggedIn() && (this.user?.can_upload || this.isAdmin());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
getPreFetchData() {
|
||||||
|
const preFetchDataElement = document.getElementById("pre_fetch_data");
|
||||||
|
if (preFetchDataElement) {
|
||||||
|
let content = preFetchDataElement.textContent
|
||||||
|
if (!content) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
content = decodeURIComponent(content);
|
||||||
|
const res = JSON.parse(content);
|
||||||
|
preFetchDataElement.remove();
|
||||||
|
return res;
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export const app = new App();
|
export const app = new App();
|
||||||
|
@@ -54,6 +54,11 @@ export default function CommentPage() {
|
|||||||
});
|
});
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
const preFetchData = app.getPreFetchData();
|
||||||
|
if (preFetchData?.comment?.id === id) {
|
||||||
|
setComment(preFetchData.comment);
|
||||||
|
return;
|
||||||
|
}
|
||||||
network.getComment(id).then((res) => {
|
network.getComment(id).then((res) => {
|
||||||
if (res.success) {
|
if (res.success) {
|
||||||
setComment(res.data!);
|
setComment(res.data!);
|
||||||
|
@@ -90,14 +90,19 @@ export default function ResourcePage() {
|
|||||||
if (location.state) {
|
if (location.state) {
|
||||||
setResource(location.state.resource);
|
setResource(location.state.resource);
|
||||||
} else {
|
} else {
|
||||||
network.getResourceDetails(id).then((res) => {
|
const preFetchData = app.getPreFetchData();
|
||||||
if (res.success) {
|
if (preFetchData?.resource?.id === id) {
|
||||||
setResource(res.data!);
|
setResource(preFetchData.resource);
|
||||||
document.title = res.data!.title;
|
} else {
|
||||||
} else {
|
network.getResourceDetails(id).then((res) => {
|
||||||
showToast({ message: res.message, type: "error" });
|
if (res.success) {
|
||||||
}
|
setResource(res.data!);
|
||||||
});
|
document.title = res.data!.title;
|
||||||
|
} else {
|
||||||
|
showToast({ message: res.message, type: "error" });
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}, [id, location.state]);
|
}, [id, location.state]);
|
||||||
|
@@ -14,6 +14,7 @@ import {
|
|||||||
MdOutlinePhotoAlbum,
|
MdOutlinePhotoAlbum,
|
||||||
} from "react-icons/md";
|
} from "react-icons/md";
|
||||||
import { useTranslation } from "react-i18next";
|
import { useTranslation } from "react-i18next";
|
||||||
|
import { app } from "../app.ts";
|
||||||
|
|
||||||
export default function UserPage() {
|
export default function UserPage() {
|
||||||
const [user, setUser] = useState<User | null>(null);
|
const [user, setUser] = useState<User | null>(null);
|
||||||
@@ -50,6 +51,11 @@ export default function UserPage() {
|
|||||||
};
|
};
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
|
const preFetchData = app.getPreFetchData();
|
||||||
|
if (preFetchData?.user?.username === username) {
|
||||||
|
setUser(preFetchData.user);
|
||||||
|
return;
|
||||||
|
}
|
||||||
network.getUserInfo(username || "").then((res) => {
|
network.getUserInfo(username || "").then((res) => {
|
||||||
if (res.success) {
|
if (res.success) {
|
||||||
setUser(res.data!);
|
setUser(res.data!);
|
||||||
|
@@ -1,8 +1,9 @@
|
|||||||
package middleware
|
package middleware
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
url2 "net/url"
|
"net/url"
|
||||||
"nysoure/server/config"
|
"nysoure/server/config"
|
||||||
"nysoure/server/service"
|
"nysoure/server/service"
|
||||||
"nysoure/server/utils"
|
"nysoure/server/utils"
|
||||||
@@ -68,22 +69,27 @@ func serveIndexHtml(c fiber.Ctx) error {
|
|||||||
description := config.ServerDescription()
|
description := config.ServerDescription()
|
||||||
preview := serverBaseURL + "/icon-192.png"
|
preview := serverBaseURL + "/icon-192.png"
|
||||||
title := siteName
|
title := siteName
|
||||||
url := serverBaseURL + c.Path()
|
htmlUrl := serverBaseURL + c.Path()
|
||||||
cfTurnstileSiteKey := config.CloudflareTurnstileSiteKey()
|
cfTurnstileSiteKey := config.CloudflareTurnstileSiteKey()
|
||||||
siteInfo := config.SiteInfo()
|
siteInfo := config.SiteInfo()
|
||||||
path := c.Path()
|
path := c.Path()
|
||||||
|
preFetchData := ""
|
||||||
|
|
||||||
if strings.HasPrefix(path, "/resources/") {
|
if strings.HasPrefix(path, "/resources/") {
|
||||||
idStr := strings.TrimPrefix(path, "/resources/")
|
idStr := strings.TrimPrefix(path, "/resources/")
|
||||||
id, err := strconv.Atoi(idStr)
|
id, err := strconv.Atoi(idStr)
|
||||||
if err == nil {
|
if err == nil {
|
||||||
r, err := service.GetResource(uint(id), "")
|
r, err := service.GetResource(uint(id), c.Hostname())
|
||||||
if err == nil {
|
if err == nil {
|
||||||
if len(r.Images) > 0 {
|
if len(r.Images) > 0 {
|
||||||
preview = fmt.Sprintf("%s/api/image/%d", serverBaseURL, r.Images[0].ID)
|
preview = fmt.Sprintf("%s/api/image/%d", serverBaseURL, r.Images[0].ID)
|
||||||
}
|
}
|
||||||
title = r.Title
|
title = r.Title
|
||||||
description = utils.ArticleToDescription(r.Article, 200)
|
description = utils.ArticleToDescription(r.Article, 200)
|
||||||
|
preFetchDataJson, _ := json.Marshal(map[string]interface{}{
|
||||||
|
"resource": r,
|
||||||
|
})
|
||||||
|
preFetchData = url.QueryEscape(string(preFetchDataJson))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else if strings.HasPrefix(path, "/user/") {
|
} else if strings.HasPrefix(path, "/user/") {
|
||||||
@@ -93,10 +99,14 @@ func serveIndexHtml(c fiber.Ctx) error {
|
|||||||
preview = fmt.Sprintf("%s/api/avatar/%d", serverBaseURL, u.ID)
|
preview = fmt.Sprintf("%s/api/avatar/%d", serverBaseURL, u.ID)
|
||||||
title = u.Username
|
title = u.Username
|
||||||
description = "User " + u.Username + "'s profile"
|
description = "User " + u.Username + "'s profile"
|
||||||
|
preFetchDataJson, _ := json.Marshal(map[string]interface{}{
|
||||||
|
"user": u,
|
||||||
|
})
|
||||||
|
preFetchData = url.QueryEscape(string(preFetchDataJson))
|
||||||
}
|
}
|
||||||
} else if strings.HasPrefix(path, "/tag/") {
|
} else if strings.HasPrefix(path, "/tag/") {
|
||||||
tagName := strings.TrimPrefix(path, "/tag/")
|
tagName := strings.TrimPrefix(path, "/tag/")
|
||||||
tagName, err := url2.PathUnescape(tagName)
|
tagName, err := url.PathUnescape(tagName)
|
||||||
tagName = strings.TrimSpace(tagName)
|
tagName = strings.TrimSpace(tagName)
|
||||||
if err == nil {
|
if err == nil {
|
||||||
t, err := service.GetTagByName(tagName)
|
t, err := service.GetTagByName(tagName)
|
||||||
@@ -117,6 +127,10 @@ func serveIndexHtml(c fiber.Ctx) error {
|
|||||||
if len(cmt.Images) > 0 {
|
if len(cmt.Images) > 0 {
|
||||||
preview = fmt.Sprintf("%s/api/image/%d", serverBaseURL, cmt.Images[0].ID)
|
preview = fmt.Sprintf("%s/api/image/%d", serverBaseURL, cmt.Images[0].ID)
|
||||||
}
|
}
|
||||||
|
preFetchDataJson, _ := json.Marshal(map[string]interface{}{
|
||||||
|
"comment": cmt,
|
||||||
|
})
|
||||||
|
preFetchData = url.QueryEscape(string(preFetchDataJson))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -125,11 +139,12 @@ func serveIndexHtml(c fiber.Ctx) error {
|
|||||||
content = strings.ReplaceAll(content, "{{Description}}", description)
|
content = strings.ReplaceAll(content, "{{Description}}", description)
|
||||||
content = strings.ReplaceAll(content, "{{Preview}}", preview)
|
content = strings.ReplaceAll(content, "{{Preview}}", preview)
|
||||||
content = strings.ReplaceAll(content, "{{Title}}", title)
|
content = strings.ReplaceAll(content, "{{Title}}", title)
|
||||||
content = strings.ReplaceAll(content, "{{Url}}", url)
|
content = strings.ReplaceAll(content, "{{Url}}", htmlUrl)
|
||||||
content = strings.ReplaceAll(content, "{{CFTurnstileSiteKey}}", cfTurnstileSiteKey)
|
content = strings.ReplaceAll(content, "{{CFTurnstileSiteKey}}", cfTurnstileSiteKey)
|
||||||
content = strings.ReplaceAll(content, "{{SiteInfo}}", siteInfo)
|
content = strings.ReplaceAll(content, "{{SiteInfo}}", siteInfo)
|
||||||
content = strings.ReplaceAll(content, "{{UploadPrompt}}", config.UploadPrompt())
|
content = strings.ReplaceAll(content, "{{UploadPrompt}}", config.UploadPrompt())
|
||||||
content = strings.ReplaceAll(content, "{{AllowNormalUserUpload}}", strconv.FormatBool(config.AllowNormalUserUpload()))
|
content = strings.ReplaceAll(content, "{{AllowNormalUserUpload}}", strconv.FormatBool(config.AllowNormalUserUpload()))
|
||||||
|
content = strings.ReplaceAll(content, "<script id=\"pre_fetch_data\"></script>", fmt.Sprintf("<script type=\"application/json\" id=\"pre_fetch_data\">%s</script>", preFetchData))
|
||||||
|
|
||||||
c.Set("Content-Type", "text/html; charset=utf-8")
|
c.Set("Content-Type", "text/html; charset=utf-8")
|
||||||
return c.SendString(content)
|
return c.SendString(content)
|
||||||
|
Reference in New Issue
Block a user