So I’ve finished first pass of our dynamic directory using URL paths.
I can have up to 5 parameters in a URL path - currently if someone was to alter the URL it’ll just load the page and return the page structure but with no data as the SC won’t find a match.
I was wondering if anyone had ideas on how I could return a 404 page not found or even redirect the user?
The SC doesn’t return an error when no match found - I was thinking of simply throwing an error and using that but it doesn’t present any error.
You can throw a 404 response from the server action that is used in your SC.
In the server action, just add a condition step to check if there are 0 matches, and in that case, add a “response” step with status code 404.
With this in place, you can configure the dynamic event for 404 and redirect to some 404 page.
OR, you can also directly use the “redirect” step in the server action, when condition returns 0 rows. But, that redirect step is not recommended to be used in that manner (last I read about it here).
Alternatively, you could set a value in the API for notfound and the use a condition if the notfound is present onsuccess of the SC action on the page to use the browser component (add it to your page if you haven’t already) to go to the 404 page:
or using the same notfound value, you could have two conditional regions on your page. One which shows the record if notfound is not present and one which shows a 404 style page if true (1). This is not a true 404 as it will return 200 but show a message to the user that the record wasn’t found.
Sorry for being that repetitive, but I find this step very important for my project. if I get it I will make a guide for wappler newbies, I think it is something very important.
Does the query return something when the page is available?
My only other thought is the server action firing with incorrect params at times if it’s auto loading the new app connect is so fast that on a SPA if you come from a different page for example site.com/list then go to a url param like site.com/details/1 it tends to load twice the first one will load without params and there for the query would have been null then it runs the action again with params.
To check this in dev tools look in the Network Tab then XHR, then navigate to the page as normal and see if it fires the server action twice etc (This might be nothing but depends how your testing this)
Please note I am no expert but maybe this will help if this works I’ll go into more detail about the changes I made etc (I saw you was trying server side so I tried a server side solution using redirect)
I see you did a second api workflow, why? Also you placed itemurlglobal inside $_PARAM in this api. Then you used this new api in the server side data of the page.
Could you go in detail? From my point of view its very similar of what I have done with your help, why with this changes now it works? I would like to understand it better so I can learn from this.
I created a second action that used PARAM as GET didn’t pass the url variables to the server side data so the query always returned no records. You could do this in one action if you want to bind all your app side data with the server side bindings in code view when not on an element you see:
The second action was just because I find it easier to bind data from Server Connects over using the server side data bindings (maybe there is an easier way to bind them but I’ve not experimented much)