你好,
我目前正在实现一个缓慢的重定向,以避免用户必须点击初始页面上唯一的“登录”按钮。
import { apiInitializer } from "discourse/lib/api";
export default apiInitializer("1.0", (api) => {
api.onPageChange((url) => {
const user = api.getCurrentUser();
let path = url?.startsWith("http") ? new URL(url).pathname : url;
path = path?.replace(/\/+$/, "") || "/";
const allowedAnonPaths = ["/login", "/privacy", "/tos", "/pub/about-this-forum", "/safe-mode", "/pub/accessibility-statement"];
if (!user && !allowedAnonPaths.includes(path)) {
window.location.href = "/login";
}
});
});
这排除了我不希望流量被重定向的路径。
这并不是最理想的方案,但目前已在 https://physicswithethan.discourse.diy 上使用。



