feat: clickable tag filters with shareable URL on /courses#558
feat: clickable tag filters with shareable URL on /courses#558adrianbp2 wants to merge 4 commits into
Conversation
- TagChip accepts linkToFilter prop to render as a link to /courses?tag=X - Courses page reads filter state from URL query params on mount/navigation - Filter changes sync to URL with router.replace (no history pollution) - Tags on CourseCard and course detail page link to filtered courses view
alasdairwilson
left a comment
There was a problem hiding this comment.
Hi, thank you for your contribution.
I have some generic feedback that would be good to sort out:
- We should avoid manually concatenating basePath for internal navigation here. next/link already knows how to handle the app base path, so this should just link to "/courses" rather than building by hand.
- We also should not manually construct the query string. It would be cleaner and safer to pass an object like
href={{ pathname: "/courses", query: { tag } }}and let Next handle encoding/serialization. It is safer and clearer. - When trying this out, there is a bit of ugliness due to the SSR page being unfiltered so you get layout changes when the page is re-rendered. If we want shareable filtered URLs to work cleanly, we should initialize the filters during SSR rather than only reading them in a client-side effect. That would probably mean reading the filter values from the request query in getServerSideProps and passing them in as initial state vai the props return, instead of always starting empty and then syncing from router.query in a useEffect.
|
Thx for feedback @alasdairwilson, here are the changes. |
alasdairwilson
left a comment
There was a problem hiding this comment.
Hi, thanks for making those changes.
At a minimum in terms of tests, we would need to test that:
- clicking on a tag takes you to /courses with the correct tag set
- url on adding/removing tag/levels etc
but there are some others that could be useful/ clear filters clearing the url, checking the initial render
I don't mind doing these if you don't want to/don't know how.
|
I know you did exactly what I asked for in terms of replacing the getstaticprops with ssr...but having seen that code change I am wondering if it is the right thing...seems a shame to dump the cached page for this but also I did hate the layout change. Since I don't really know the best solution I'd leave what you have (with get server side props) for now, it can always be changed later. |
|
I've added e2e tests covering all the cases you mentioned. |
|
Ok apologies for taking so long to get back to this, This change comes with at least one bug: The issue is that on /courses it is keeping its local filter state after a same-route navigation like /courses?tag=programming -> /courses?tag=tooling so if you navigate once to courses you now cant navigate again, even though Next is fetching new SSR props in the background. But this made me realise the tag chips on /courses shouldn’t be acting like fresh page navigation at all, they should just toggle the existing filters on the page we should also nav from the same tagchips on the home page |
Closes #557
Changes
linkToFilterprop — renders as a<Link>to/courses?tag=Xtag,level,q,lang) from URL query params on mount and on in-page navigationrouter.replace(shallow) — shareable links without polluting browser historyHow to test
/courses— click any tag chip → URL updates and filter applies/courses/[id]— click a tag → navigates to/courses?tag=Xwith filter active