refactor(frontend): 🎨 Make code look better

This commit is contained in:
Jesse Wierzbinski 2024-04-15 22:15:52 -10:00
parent 6a419a2015
commit 214f4d5a3e
No known key found for this signature in database

View file

@ -175,12 +175,12 @@ const handleDefaultRequest = async (
accessToken: string, accessToken: string,
) => { ) => {
const file = Bun.file(join(config.frontend.glitch.assets, path)); const file = Bun.file(join(config.frontend.glitch.assets, path));
const transformedText = path.endsWith(".html")
? await indexTransforms(await file.text(), accessToken, user)
: undefined;
if (await file.exists()) { if (await file.exists()) {
return returnFile( return returnFile(file, transformedText);
file,
await indexTransforms(await file.text(), accessToken, user),
);
} }
return null; return null;
@ -192,7 +192,6 @@ const indexTransforms = async (
user: UserWithRelations | null, user: UserWithRelations | null,
) => { ) => {
let newFileContents = fileContents; let newFileContents = fileContents;
for (const server of config.frontend.glitch.server) { for (const server of config.frontend.glitch.server) {
newFileContents = newFileContents.replaceAll( newFileContents = newFileContents.replaceAll(
`${new URL(server).origin}/`, `${new URL(server).origin}/`,
@ -209,14 +208,12 @@ const indexTransforms = async (
"Lysand is free and open-source software using the Glitch-Soc frontend.", "Lysand is free and open-source software using the Glitch-Soc frontend.",
); );
newFileContents = newFileContents.replaceAll("Mastodon", "Lysand"); newFileContents = newFileContents.replaceAll("Mastodon", "Lysand");
newFileContents = newFileContents.replaceAll( newFileContents = newFileContents
"Lysand is free, open-source software, and a trademark of Mastodon gGmbH.", .replaceAll(
"This is not a Mastodon instance.", "Lysand is free, open-source software, and a trademark of Mastodon gGmbH.",
); "This is not a Mastodon instance.",
newFileContents = newFileContents.replaceAll( )
"joinmastodon.org", .replaceAll("joinmastodon.org", "lysand.org");
"lysand.org",
);
// Find script id="initial-state" and replace its contents with custom json // Find script id="initial-state" and replace its contents with custom json
const rewriter = new HTMLRewriter() const rewriter = new HTMLRewriter()
@ -330,12 +327,12 @@ export const handleGlitchRequest = async (
return handleSignOutRequest(req); return handleSignOutRequest(req);
} }
// Redirect / to /index.html if (
if (path === "/" || path === "") path = "/index.html"; req.headers.get("Accept")?.includes("text/html") &&
// If path doesn't have an extension (e.g. /about), serve index.html !path.includes(".")
// Also check if Accept header contains text/html ) {
if (!path.includes(".") && req.headers.get("Accept")?.includes("text/html"))
path = "/index.html"; path = "/index.html";
}
return handleDefaultRequest(req, path, user, accessToken); return handleDefaultRequest(req, path, user, accessToken);
}; };