I have been using the Wix editor dynamic page settings for SEO, which works fine. You can easily set a text-based database field for TITLE tag etc.
But I want to create a custom title tag using multiple elements from the database. I have attempted to do this using a router, but not entirely successfully…
I simply want to concatenate several fields from the database (for the specific dynamic page item). Could someone give some generic outline code on how to achieve this please? Specifically, how to tell the router which database item the current dynamic page is displaying.
Thank you.
1 Like
I would like to see an example also. Through the Setting->Page Info area of my dynamic page I have selected “Add hooks”. I selected the afterRouter option because it mentions SEO in the description and that is what i’m trying to achieve.
export function page_afterRouter(request, response) {
//TODO: write your code here…
}
The documentation for an afterRouter says: “After the data binding router completes its logic, but before the page is displayed.” I assume that would mean that my page data has already been fetched from the database. If that is correct, can you tell me how to access the data that will be used to render the page?
For example on the frontend side I reference the data as such: $w(‘#dataset1’).getCurrentItem(). Does the backend afterRouter know which database data has been fetched? Do I need to use wix-data to fetch the data record again? It would seem to me that wix is already doing that unless all data is fetched via ajax on the front end.
In your example in the api reference https://www.wix.com/corvid/reference/wix-router.html#afterRouter you give an example like this: if(response.data.picture.orientation === “vertical”)
How in your example is information about the page such as a picture being added to the data element?
How can I inspect to see what else is in the data object? The Site Events Tool only shows [object] if I do a console.log(data).
I have also noticed that if I try to set any static value on the meta tags object such as:
“metaTags”: {
“copyright”: “Wix.com”,
“Classification”: “Example”,
“og:title”: “The Rock”,
“og:image”:
It then fails to set all the other meta tags such as Title that I have pre-configured on the Settings → SEO (Google) tab for my dynamic page and I get some default wix value. Does this mean if I want to define one extra seo value in the router that I need to override all of the page meta fields?
An example of the appropriate way to access data from the database in the afterRouter function would be very helpful.
@joshmanton Manged to get it fully working 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 {
if (response.data.items[0].customMetaTitle) {
response.head.title = response.data.items[0].metaTitle
return response
} else {
return response
}
}
}
it seems that you access the database collection data using response.data.items[0].myFieldName. Using this method you can add additional text, or concatenate several fields etc. In my case above I check to see if a boolean flag (customMetaTitle) is set and if so I use a custom title from the dataset (metaTitle).
As far as I know, a good title and using the keywords is one of the possibilities to increase your website traffic and reach the Google top search. However, this technique doesn’t guarantee you that you will get a desirable result. In my opinion, to get to the top Google search, you need to know Google’s algorithm, and also apply several techniques at the same time. I’ve recently read about forum backlinks and how it works. Turns out Google loves that links to your website appear on other websites. If you use it with the keywords, that would be more than enough for you.