On my site I'm adding query params to the URL as such:
wixLocation.queryParams.add({"category": "tag"});
The maximum number of additional params I add is two.
The issue appears when I try to remove multiple params at once. The user is able to click a "clear" button that, among other things, should clear all params from the URL. When there is just one param, it removes it with no issue. But if there are two params, it will either clear just one (if I am not adding another param in the same function) or clear none (if I am adding another param in the same function). An example of code is below:
export function searchbutton_click(event) {
// Update the URL to remove filter specifications and add search specification
if (wixLocation.query["author"]) {
wixLocation.queryParams.remove(["author"]);
}
if (wixLocation.query["profession"]) {
wixLocation.queryParams.remove(["profession"]);
}
if (wixLocation.query["tag"]) {
wixLocation.queryParams.remove(["tag"]);
}
if (wixLocation.query["category"]) {
wixLocation.queryParams.remove(["category"]);
}
wixLocation.queryParams.add({ "search": `${$w("#searchbox").value}`})
}
The above snippet should remove the params that contain "author", "profession", "tag", and "category" if they exist, and add the param with "search". It only adds the "search" param but removes nothing, even if it exists.
Is this an issue with queryParams.remove or with the code above? If anybody sees anything I'm missing, the help would be much appreciated!
@Quentin Plomteux @David - SKmedia and anybody else who looks at this later: JFYI another solution that works, as recommended by Wix folks while they are looking into the issue, is to remove multiple params at once:
wixLocation.queryParams.remove(["category", "author"]);
Not sure why I didn't think to try this! It works for my purposes for now. Thanks again for all your help!
@Quentin Plomteux thanks so much for all your help! That explanation would make sense.
@David - SKmedia absolutely -- I'm using clearInterval() for this. Is there a way that you know of to set a timeout such that after the interval runs X times, it cancels itself?
@Naomi Day Glad to know it works.
I think it does not work because as soon as it erase one queryParam It replace the whole reference, meaning that your initial reference point to something not connect to Wix system.
By using the interval, it reference the latest version of queryParams and delete from the binded queryParams. But that could be something else :p
Also @Quentin Plomteux I'm not seeing how to report a bug/create an issue, only how to make a post... would you mind pointing me towards how to do that? I'd still like to file this as a bug!
It seems like queryParams.remove will only remove one query parameter per function, no matter how many times it's called.
@Quentin Plomteux oddly enough, that does work. I'll modify it so it doesn't remove the params I need... any thoughts as to why this works?
@Naomi Day could you report the bug and create a ticket for that issue so we can report it? :) (we need the ticket number)
it's because of queryParams.remove not returning anything. rm is just an array of undefined. My last idea would be
cancelInterval = setInterval(() => { let keys = Object.keys(wixLocation.query); if(keys[0]) wixLocation.queryParams.remove(keys[0]) else cancelInterval(); }, 1000) // start with a big interval than reduce until it breaks
does it work? :D (I have not tested the code in the editor there might be a syntax error)
It prints 'done' and doesn't still remove the params!
If it isn't an invincible bug, then this should force it to work. Otherwise, it will have to be addressed by Wix first:
const rm = Object.keys(wixLocation.query).map(o => { return () => { wixLocation.queryParams.remove(o) } }); Promise.all(rm).then(() => console.log('done')).catch(() => console.warn('failed'));
@Quentin Plomteux thanks for your answer! Any way I could follow the bug that's been filed?
I tried that -- it also didn't remove any of the params. :(
Hello @Naomi Day,
It looks like a bug indeed In the meantime, and based on @David - SKmedia answer, you could try this
wixLocation.queryParams.remove(Object.keys(wixLocation.query));
Let us know if it works :)
Have you tried something like this?:
Object.keys(wixLocation.query).forEach(k => wixLocation.queryParams.remove(k));