~p turns any nested url containing / into %2Fs which mess up the url.
Compiler throws a warning warning: no route path for DemoWeb.Router matches "/#{current_path}?#{options}", although the current liveview’s path (/products or /posts) is defined in the router.
Using |> push_patch(to: "/#{current_path}?#{Plug.Conn.Query.encode(options)}") does seem to work (although compiler still throws a warning), but I don’t want to go back to this as these methods are deprecated after verified routes.
What’s the best way to do this?
Lastly, is there a better way to fetch the url of current LiveView? @chrismccord and @josevalim
~p is about verifying at compile time that a route would be routeable by the application/router. For #{current_path} there’s nothing which could be verified in the first place. You can just remove the sigil. Nothing forces you to use it.
There are also unverified_* functions in VerifiedRoutes which may or may not be of interest.
However, if I may offer some more general advice: it may be better to generate your paths based on the actual state of your application rather than the current path. Like, if you are on a page for a post = %Post{} then perhaps you should regenerate the URL as ~p"/posts/#{assigns.post}?#{params}, like you would wherever you link to that page.
If you try Routex, use main branch of Git or the release candidates of version 1.2 as it has been greatly improved lately (incl fixes by @ken-kost). Routex (using the Plugs and LiveViewHooks extensions) also assigns @url for you to be accessible in templates.