refactor: Improve SSR logic for homepage SEO

This commit is contained in:
2025-10-13 19:00:06 +02:00
parent 071f0d5ea1
commit 1bc9332f1c
2 changed files with 17 additions and 25 deletions

View File

@@ -83,29 +83,28 @@ app.get('/:id', (req, res) => {
fileStorage.get(key, (data) => { fileStorage.get(key, (data) => {
if (data) { if (data) {
// Paste found, render it with dynamic SEO tags // Paste found, render it with dynamic SEO tags
const title = `Paste ${key} - unknownBIN`;
const description = data.substring(0, 160).replace(/\n/g, ' ');
res.render('index', { res.render('index', {
paste: { title: `Paste ${key} - unknownBIN`,
key: key, description: data.substring(0, 160).replace(/\n/g, ' ').trim(),
data: data, content: data
title: title,
description: description
}
}); });
} else { } else {
// Paste not found, render the homepage // Paste not found, render the homepage with 404 status
res.render('index', { res.status(404).render('index', {
paste: null title: '404 Not Found - unknownBIN',
description: 'The paste you were looking for could not be found.',
content: null
}); });
} }
}); });
}); });
app.get('/', (req, res) => { app.get('/', (req, res) => {
// Render the homepage // Render the homepage with static SEO tags
res.render('index', { res.render('index', {
paste: null title: 'unknownBIN - A Secure & Modern Pastebin',
description: 'unknownBIN is a secure and modern open-source Pastebin software written in Node.js. Easily share code, logs, and text snippets.',
content: null
}); });
}); });

View File

@@ -2,17 +2,10 @@
<html lang="en"> <html lang="en">
<head> <head>
<meta charset="UTF-8"> <meta charset="UTF-8">
<% if (paste) { %> <title><%= title %></title>
<title><%= paste.title %></title> <meta name="description" content="<%= description %>">
<meta name="description" content="<%= paste.description %>"> <meta property="og:title" content="<%= title %>">
<meta property="og:title" content="<%= paste.title %>"> <meta property="og:description" content="<%= description %>">
<meta property="og:description" content="<%= paste.description %>">
<% } else { %>
<title>unknownBIN - A Secure Pastebin</title>
<meta name="description" content="unknownBIN is a secure and modern open-source Pastebin software written in Node.js.">
<meta property="og:title" content="unknownBIN - A Secure Pastebin">
<meta property="og:description" content="unknownBIN is a secure and modern open-source Pastebin software written in Node.js.">
<% } %>
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no"> <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">
<link rel="stylesheet" type="text/css" href="/application.min.css"/> <link rel="stylesheet" type="text/css" href="/application.min.css"/>
<link rel="stylesheet" type="text/css" href="/highlight.min.css"/> <link rel="stylesheet" type="text/css" href="/highlight.min.css"/>
@@ -29,7 +22,7 @@
<div class="raw function" title="Raw [Ctrl + Shift + R]">Raw</div> <div class="raw function" title="Raw [Ctrl + Shift + R]">Raw</div>
</div> </div>
<div id="content"> <div id="content">
<pre id="code" style="display:none;" tabindex="0"><code <% if (paste) { %>id="preloaded-data"<% } %>><% if (paste) { %><%- paste.data %><% } %></code></pre> <pre id="code" style="display:none;" tabindex="0"><code <% if (content) { %>id="preloaded-data"<% } %>><% if (content) { %><%- content %><% } %></code></pre>
<textarea spellcheck="false" style="display:none;"></textarea> <textarea spellcheck="false" style="display:none;"></textarea>
</div> </div>
</body> </body>