Using router for SEO...500 timeout error

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.

?

?

Have you looked on the site monitoring tab to see if there are any errors generated? (You’ll have to reload the relevant address - it doesn’t keep history.)

I’ve actually got it working today as follows:

import { ok, notFound, WixRouterSitemapEntry } from "wix-router";
import wixData from "wix-data"

export function yacht_charter_afterRouter(request, response) {

 if (response.status === 404) {
 return notFound()
    } else {
 let yachtName = response.head.title
 return wixData.query("yachts")
            .eq("name", yachtName)
            .find()
            .then((queryResult) => {
                response.head.title = "Yacht " + yachtName + " for Charter, " + queryResult.items[0].length + "m " + queryResult.items[0].builder
            })
    }
}

I’m not sure if this code is the correct way of doing things…but it certainly works and creates me an “on the fly” title for SEO purposes. The reason I was getting a 500 timeout error was because I was referencing

response.head.title

on a page that didn’t exist…now I’ve got this:

 if (response.status === 404) {  return notFound()     }  

which stops it crashing, as it returns before attempting to read “response”

As I say, use my working code at your own risk, as I’m not 100% sure it’s right, but just that it works for me. I’m using the “response.head.title” of the page in order to search the database to match the record, which works be seems like a hack. I’m also returning “response.head.title” whereas the API says to return just “response” but this is the only way I can get it to work…

I did also have a look at customizeQuery() but couldn’t get this to work.

Link to the guide for all of this here Velo: Creating Data Hooks for Dynamic Pages | Help Center | Wix.com

P.S. Like the site monitoring tip, didn’t know that existed and will certainly be useful for future.