The browser maintains the history stack. I think it'd be more work than it's worth to process the stack. An easier solution may be to just send some extra route state to indicate to the description page where the user transitioned from.
jobSearch.js
Add a boolean flag state to the Link
.
<Link
href={{
pathname: "/jobdescription",
query: {
jobId: item,
fromSearch: true // <-- navigating from the search page
},
}}
>
jobDescription.js
Conditionally render the middle "Search Page" breadcrumb on the route state.
<ol className="list-reset py-4 pl-4 rounded flex bg-grey-light text-grey">
<li className="px-2">
<Link
href={{
pathname: "/"
}}
>
<a className="text-gray-600 no-underline text-indigo">Home</a>
</Link>
</li>
{router.query.fromSearch && (
<>
<li>/</li>
<li className="px-2">
<Link
href={{
pathname: "/jobsearch"
}}
>
<a className="text-gray-600 no-underline text-indigo">
Search Page
</a>
</Link>
</li>
</>
)}
<li>/</li>
<li className="px-2"> Job - {router.query.jobId} </li>
</ol>
Note: This sends the extra "state" as part of a query string. Since Nextjs Link
components don't support route state (like other react routing/navigation libraries) the options are to send it via the literal URL, or to temporarily store the link state in local/session storage or app state (e.g. redux). See this github issue for the request for link state.
Edit
I found a "workaround" that allows you to send extra query parameters yet display a custom URL in the address bar. Use the optional as
decorator. this does duplicate some of the work, but will at least work for users who may bookmark the URL.
<Link
href={{
pathname: "/jobdescription",
query: {
jobId: item,
fromSearch: true // <-- navigating from the search page
},
}}
as={`/jobDescription?jobId=${item}`}
>
The above linked sandbox is updated to use this fix.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…