Handler that matches all links that contain certain URL query string

Hello,

so we are doing some custom pagination as we would prefer to use query strings rather than the default frontity approach with /some-slug/page/2. Instead I would like to have my urls like this /some-slug/?page=2. I already have my pagination working but I want to make it better, by updating the current link data.page based on those params.
Right now if for example i go to /some-slug/?page=2, then data.page === 1.

So I want to do a handler that matches all urls that include page=somevalue and then update data.page to the value of ?page.

I have written some regex but when I place it in my handler it crashes the app, saying its invalid regex.

I know question is very basic but I am struggling to make this handler to match my URLS. Here is what I have so far:

  {
    name: "Set page in data based on query strings",
    pattern: "(?=.page=.).*",
    func: ({ state, link }) => {
      console.log("this handler matches everything with 'page=' as a param")
    }
  }

The error i get is: Invalid regular expression: /^\/(?\=.page\=.)\.\*\/(?:\/)?$/: Invalid group.
Any idea what I am doing wrong?

Thanks in advance.

1 Like

Hi @ni.bonev

You can use a regexp tester such as this one or this one to ensure that your regular expression is formatted correctly and that it will match the strings that you want it to.

Hey Michael,

thanks for the response. I already did some testing of the regex and unfortunately I don’t think that is the issue. I already tested it and saved the result. You can see the pattern works.

Actually last week, after I made this question I started digging further and I was not even sure if the pattern accepts RegExp, as anything I would put would just crash my frontity.

Today I decided to dig further and looked at some frontity sites, inspected their handlers and saw something that was not clear to me from the docs, but solved the issue. The way to handle pattern in handlers if its only regex(without any actual path) you need to do it this way:

{
 name: "Set page in data based on params",
 pattern: "RegExp:(?=.page=.).*",
 func: ({ state, link }) => {
   //Do your thing
 }
}

Hopefully that also helps someone in the future.

1 Like

Hi @ni.bonev

Great that you found a solution :+1:. You’re right, this is not clear from the documentation. Please feel free to make a PR to either our docs repo or our api repo with any suggested changes to make this clearer. Thanks.

Exactly my plan. PR is already made.
Thanks again for following up.

Greetings and Happy coding.

1 Like

Thanks @ni.bonev, I’ve merged your changes. :+1:

1 Like