I’m trying to setup a router for custom SEO on dynamic pages. I want my html tag to be a combination of various fields from my database. I have managed to get this working fine, as follows:
import { ok, notFound, WixRouterSitemapEntry } from "wix-router";
import wixData from "wix-data"
export function myRouter_afterRouter(request, response) {
let databaseItem = response.head.title // Get the current page title, which is used to search the database, see below
if (request.path.length < 1) {
return notFound()
}
return wixData.query("myDatabase")
.eq("name", databaseItem)
.find()
.then((queryResult) => {
if (queryResult.length > 0) {
response.head.title = "House " + queryResult.items[0].builder + "size " + queryResult.items[0].size + " feet | " + response.head.title
return response // Creates the custom title tag for SEO, works great
}
return notFound()
});
}
I use the page title to search the database (since this is the only way I could figure out to get data from the current dynamic page to the router - p.s. if there is a better way to do this please let me know, as this seems like a bit of a hack?). And that works fine, I’m then able to search my database “name” field for a matching item and the custom title tag is created fine.
But there is a problem and I get a 500 timeout error if the page/item doesn’t exist (for example if I leave the path empty or if I make up a random path). I am expecting that in the case of leaving the path empty then if (request.path.length < 1) should kick in, and if the item can’t be found in the database then if (queryResult.length > 0) should kick in…
In order to debug, I’ve commented sections out, and I’m down to just having:
export function myRouter_afterRouter(request, response) {
let databaseItem = response.head.title
}
And even that causes a 500 timeout error for pages that don’t exist…
What am I doing wrong please? (and also let me know if there is a better way to get the data of which specific database item the dynamic page is displaying, rather than using the head.title hack that I am currently doing…) Thank you.