Support for uncached data loading

So you don't have to restart the server, you can add "cache_data": false option to config.json to not cache data.
Documented in CONFIG.md
I added a require_module function that either does or does not cache the data based on this configuration option.

Signed-off-by: deadvey <deadvey@deadvey.com>
This commit is contained in:
2025-09-24 20:37:35 +01:00
parent 3f173fc2e3
commit 35e6b94ba1
4 changed files with 23 additions and 4 deletions

View File

@@ -14,7 +14,8 @@ All options show an example configuartion value and the variable type + an expla
| enable_hitcount | true | Boolean | Enabling the hitcount (a number that represents the amount of front page loads (stored in hitcount.txt)) can slightly slow down loading of the front page. |
| charset | "UTF-8" | String | This is the value in the <meta charset=""> tag in the html of all pages, you should not change this unless you know why. |
| root_path | "/path/to/root/of/website" | String | Anything in this directory will be in the webroot, so put favicon.ico and anything else here. |
| data_storage | "json" | String | JSON is currently the only supported format, but SQL is going to be added/is a work in progress |
| cache_data | true | String | Not caching data means you can edit the posts, users, comments etc, maunally and not have to restart the server, however, for large instances this is not reccomended as it takes longer to load the required data. Note: config.json always needs a restart |
## Basic Customisation
| name | example value | variable type | explanation |
|-----------------|----------------------------|---------------|------------------------------------------------------------------------------------------------------------------------------------------------------------|

View File

@@ -5,6 +5,7 @@
"site_url": "https://example.com",
"language": "en-US",
"port": 8080,
"cache_data": false,
"allow_signup": true,
"site_description": "Read my blogs!",
"timeline_length": 20,

View File

@@ -1,6 +1,7 @@
import { createRequire } from "module";
const require = createRequire(import.meta.url);
const func = require('./functions.js')
const config = require("../config.json")
const fs = require("fs")
@@ -8,14 +9,14 @@ export function getdata(data, index=-1) {
if (config["data_storage"] == "json") {
if (data == "posts" || data == 'users' || data == 'comments') {
let result = require(`../data/${data}.json`)
let result = func.require_module(`../data/${data}.json`)
if (index != -1) {
return result[index]
}
return result
}
else if (data == "hitcount") {
let result = require('../data/data.json') // This file is actually called data.json
let result = func.require_module('../data/data.json') // This file is actually called data.json
return result["hitcount"]
}
else {

View File

@@ -4,6 +4,19 @@ const config = require("../config.json")
const fs = require('fs')
const locale = require(`../locales/${config.locale}.json`)
// This function requires a module without caching it
// So the server doesn't need to be restarted, though this can slow it down a bit.
// https://stackoverflow.com/a/16060619
export function require_module(module) {
if (config.cache_data == false) {
delete require.cache[require.resolve(module)];
return require(module);
}
else {
return require(module);
}
}
// The configuration defines a date format using the date-fns (a datetime library) syntax
// eg "yyyy-MM-dd"
// this converts unix time (an integer) into a string that is formatted according to config.js
@@ -86,6 +99,7 @@ export function escape_input(input) {
}
// Render comment content by replacing the >> int with a url link to that comment
// Syntax: ">> postID-commentID"
export function render_comment(comment_content) {
return comment_content
.replaceAll(/>> ([0-9]*)-([0-9]*)/g, "<a href='/comment/$1-$2'>>> $1-$2</a>")
@@ -95,9 +109,11 @@ export function render_comment(comment_content) {
.replaceAll("\n", "<br/>")
};
// Renders a string into markdown using markdown-it library
export function render_md(content) {
const markdownit = require("markdown-it")
const md = markdownit({
const md = markdownit({ // this is just defining some options for markdown-it, should I add this to config.json?
html: false,
xhtmlOut: false,
breaks: true,