Overriding the home page route on a NodeBB
Difficulty: Moderate-Advanced (some theming and plugin writing experience required)
The following guide teaches you how to override the index route of a NodeBB so that it points to another page, such as /recent
. This is intended to be done in a custom or forked theme.
This guide specifically will overwrite the home route to load the "Recent Topics" page.
Rationale
Not everybody wants a category listing, especially smaller boards with only one "category".
Use cases
- Show the "Recent Topics" page as the home page
- On a forum with only one category, load just that category at start
- If using the static page plugin, load that page first.
Prerequisites
NodeBB v0.5.0, or a prerelease version with commits after (or equal to) this hash: https://github.com/NodeBB/NodeBB/commit/667a78902e7c9218eb4b66bb97ebbd3e423630cb
Steps
- Make a copy of the theme you'd like to work on. For compatibility reasons, you should probably not modify the vanilla or lavender theme files, so copying them to another folder is best
- Rename all references of the theme id to another id of your choosing.
- If the
templates/
folder does not contain aconfig.json
file, copy one over fromnodebb-theme-vanilla
. Edit this file. - The first line in the
custom_mapping
block is"^\/?$": "home",
, changehome
torecent
- Add a new plugin hook to your
plugin.json
file:{ "hook": "action:app.load", "method": "init" }
- Edit your theme's main library file (that you specified in
plugin.json
, right?), and create a newinit
method (attachment 1). - Save the files, select your theme, restart your NodeBB.
Attachments
Attachment 1 library.js
"use strict";
var Theme = {};
Theme.init = function(app, middleware, controllers) {
// Overwrite the existing `/` route handler to load the recent topics (in a given month) page
app.get('/', middleware.buildHeader, function(req, res, next) {
req.params.term = 'month';
controllers.categories.recent.apply(controllers.categories.recent, arguments);
});
app.get('/api/home', function(req, res, next) {
req.params.term = 'month';
controllers.categories.recent.apply(controllers.categories.recent, arguments);
});
};
module.exports = Theme;