wixLocation.query - dynamic page vs static page

From some reason the behavior is different on static pages and dynamic pages and I’m wondering why.

For example:

/*
page url examples:
//domain.com/abc?q=1 OR //domain.com/abc/a?q=1
*/

import wixLocation from 'wix-location';
let q = Number(wixLocation.query.q);
console.log(q); //<<<< HERE IS THE DIFFERENCE
$w.onReady(function () {
    $w("#button1").onClick((event) => {
        q++;
        wixLocation.to(/*prefix + path in case of dynamic OR path in case of static*/ + q);
})

In both cases when I click the button, the url changes in the browser address line as expected.
But here comes the difference:
Dynamic page: every time the query changes it logs the new query to the console.
Static page: It doesn’t log a new query.

So why is that?

wix-location has a built in optimization to not actually perform a navigation in case it’s to the same page you’re on. This optimization only effects static pages and not dynamic pages because it assumes that dynamic pages are, well, dynamic, so a navigation occurs even if it’s to the current url.

Thank you.

I have a static page with dynamic content (i.e. the user clicks NEXT and the content changes).
The problem is that users tend to click the browser back in order to get the previous contents of the same page.
So to solve that I thought to change the parameters when the user clicks Next (and then when they click back, the browser will stay on the same page with the previous parameters.
However, there’s no way to detect the parameter change if the user clicked Back, unless I make this page dynamic.
So I’ll make this specific page dynamic even though it’s the only page with this prefix.

Or maybe you have a better idea?

Most likely wix-location.onChange() will do the trick for you and will fire on any change to the url. I haven’t tested it with query params yet, please share your outcome.

Already tested and it didn’t work.

gotcha. so dynamic would be a better solution here - as you pretty much implemented a dynamic page within a static one. A dynamic page also adds the advantage of SEO as all the pages are listed in the sitemap.
bottom line - yes, i think you should change the page to a dynamic page.

Thanks!