HashRouting forcing reload of cached page?

Be easy, not a programming professional :slight_smile: Hitting the back button and having my entire app close was getting on my nerves, so I found stucork’s guide and example app for hash routing. (Thanks so much! I’m sure that was a lot of work!) I’ve gotten everything to work in terms of routing but I’ve encountered a strange behavior that I need to fix.

I load up hospital pages by passing a unique ID number to a Globals.hospital_id variable. I then navigate to my HospitalReport form and pull all my information using that Global.hospital_id number to look up all pertinent information associated with that hospital.

When I navigate to the HospitalReport page the first time, it works appropriately and all information is correct however if I hit the back button and try to search for a different hospital, the Global.hospital_id variable is changed appropriately (I print a lot of values for troubleshooting through console) but when navigating forward again to the HospitalReport page, it’s always the hospital from when I visited the very first time. Looking in the console with debugging active, it says #routing loaded HospitalReport from cache. Does this mean it’s potentially loading old values from a cached version and thus not using the new variables? Can I override this behavior?

Thanks for looking!

Okay, I dug into the code for the actual HashRouting dependency and found the routing.clear_cache() function. Calling that prior to navigating to the HospitalReport page does the trick!

Leaving this up for any future newcomers with the same issue.

1 Like

You might want to think about query params rather than global state here


@routing.route('report', url_keys=["id"], title='Safer Nursing | Report {id}')
class HospitalReport(HospitalReportTemplate):
    self.id = self.url_dict["id"]
    ...

(if your user refreshes the page on the “report” page then your app will probably break if you’re relying on global state)

4 Likes

My understanding of this is that your example is for retrieving url_key from a url and set a variable on my form to the url_key value.

When I’m generating the url_hash right before navigation with…
url_hash = event_args[‘sender’].tag.url_hash

How do I get the hospital ID into the url so that it can be pulled?

Thanks for looking stu!

that will really depend on your app.
ultimately you want something like


routing.set_url_hash(f"report?id={id}")

Some examples can be seen in the documentation for routing:
https://anvil-extras.readthedocs.io/en/latest/guides/modules/routing.html

there’s also a clone link at the top you can explore

2 Likes

Oh perfect. I was a dummy and for some reason I must have been in the anvil-extras components section thinking it was the entire list without realizing it because I couldn’t find the docs for routing.

Thanks man. I appreciate it! I see how it works now. The table showing url examples with how it populates out to the patterns, dicts and keys is exactly what I needed.

1 Like

For an update to what I’ve learned, routing.clear_cache() isn’t the best way to accomplish loading a page without pulling from a previously saved version of the page. To load in a page fresh without clearing history and losing the entire history stack, use routing.set_url_hash(‘your_route’, load_from_cache=False)

2 Likes