How to Redirect some Dynamic Item pages using Routers

Goal: Redirect some dynamic item pages using routers
Background: A single collection’s items whose ‘status’ tag field value equals ‘Premium’ & ‘_owner’ equals ‘1a23’ need to be redirected.
I’m new to routers, checked doc & tutorials, still confused abt how to apply here

Question: For my case, do routers require querying using slugs only? Or can I query using another collection field like

await wixData.query("Properties").eq('status', ["Premium"]).find();

Non working routers.js code: (Not worked even at a basic level. No console.log shows

import wixData from 'wix-data';
import { redirect, next } from 'wix-router';

export async function events_beforeRouter(request) {
	const { path } = request;
	const slug = path[0];
	// Querying collection by slug
	const eventsQueryResult = await wixData.query("Properties").eq("link-properties-title", slug).find();

	const event = eventsQueryResult.items[0];
	console.log("eventlist", event);

    if (event.status[0] === "Premium") {  
		return redirect("/events");
    }
	return next();
}

Working in Wix Editor

Do you have a link to the site you might be able to share?

This does sound doable with Routers :slight_smile:

Not strictly, but you will need to use something within the WixRouterRequest - Wix Router Request Introduction | Velo - to link the incoming request to an item in the CMS.

Most commons use cases:

  • Protect pages according member role/permission
  • Re-route
  • Load data/additional data

In your case, you’ll likely want to use something within the URL to identify the item within the CMS it’s trying to access

Thank you for your response. I really read up on the API ref but as a rookie coder I’m quite limited in figuring out how to fix the code. I’d appreciate if anyone can suggest a change

Another question.. I own several premium Wix sites, all started on Wix classic, before Studio was launched. I’ve seen the powerful AI assistant baked into Wix Studio. I’d be using Studio but as there’s no way to convert the already much built classic sites to Studio sites, without building from scratch, I’m stuck on classic. Is there a way I can use this integrated AI assistant on my classic sites? This could solve so many of my unfixed obstacles. If you could show me a possibility I’d be grateful!

Are these text fields? Is the status field used to check if the owner is a paid subscriber? I’m asking this because you mentioned in your other post:

That you have used the 2nd approach, which does not require items to have a status field.

Check out this tutorial by @thewixwiz on routers and try coding your logic by following along the steps. Should be easy enough to understand by someone who’s just getting started with code.

2 Likes

Many thanks for your response:)
‘status’ is a tag field, as mentioned above & 'owner is a system text field. Prior to making this post, I followed all @thewixwiz 's tutorials on routers & hooks incl the above which is what my above code is based on.

Not quite, it’s just one label for filtering purposes

With my rookie coding, I had console.log s & try catch blocks in that routers.js file, but none of them show up on the console. I tested just at the simplest levels, & none work. So my 2 main questions re this;

  1. For my use case, with routers, is it possible to query without slugs? (using other fields)
  2. If you can spot why my code doesn’t work or if you have some code that’s worked for you, I’d be grateful.

That depends on what you are trying to achieve. I haven’t used routers a lot, but as seen in the video, if you want fetch the complete item object of the particular dynamic item page, then the queryBySlug() method does the job correctly.

You’ve copied the exact code as shown in the video, including this line:

But if you would have read the documentation that was being pointed to in the video, you would have found out that:

So you need to replace events with the prefix of your dynamic page.


This is exactly why developers charge high fees for the code they write, because coding is not a mere copy-paste job, it involves digging deep into the core logic and correctly linking all the elements in just the right way to make them all work in perfect sync.

So for a rookie coder, I would highly advise studying the documentation in detail, and also to first work on test enviroments - for example creating a similar Events site as was shownin the video and first implementing your code there, trying out new things and see what works and what dosen’t, and then implement what you have learnt on your actual live project that you are working on.

That’s how I learnt it, and I hope this approach will help you too! (:

Happy coding! :raising_hands:t2:

Thank you for this. I’ll have to go ahead with the slug method then.
Re that comment, I know what you mean, that misapprehension people have about devs. That ain me:) I do understand the time, effort & difficulty of these. So given that, those of you that just help out with your knowledge have my respect! Current limitations in my resources & capacity has me stuck building these alone with some community help, just the essentials at this stage. I am working towards being able to pay devs well hopefully in the near future.

1 Like