-
-
Notifications
You must be signed in to change notification settings - Fork 449
chore(ui): consistent back navigation #1164
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
danielroe
merged 3 commits into
npmx-dev:main
from
iiio2:chore/consistent-back-navigation
Feb 8, 2026
+19
−4
Merged
Changes from 1 commit
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| export function useCanGoBack() { | ||
| const canGoBack = ref(false) | ||
|
|
||
| if (import.meta.client) { | ||
| const router = useRouter() | ||
| canGoBack.value = router.options.history.state.back !== null | ||
| } | ||
|
danielroe marked this conversation as resolved.
Outdated
|
||
|
|
||
| return canGoBack | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🧩 Analysis chain
🌐 Web query:
Vue Router 4 router.options.history.state undefined null documentation💡 Result:
In Vue Router 4, the history implementation you pass to
createRouter({ history })implementsRouterHistory, which has astate: HistoryStateproperty (documented, but marked Alpha in the API docs). (router.vuejs.org)Why
router.options.history.statecan beundefined/nullrouter.optionsis not the recommended public surface to read from. Even though it exists, it’s not the API the docs point you to for “history state”.window.history.statecan benull(e.g., before anything has calledpushState/replaceState, or depending on how the entry was created). Vue Router also stores its own navigation info inhistory.state, and the docs warn you to preserve/merge it if you ever callhistory.replaceState()yourself. (typeerror.org)What the documentation does recommend using
stateoption in navigation:router.push({ ..., state: { ... } })/router.replace({ ..., state: { ... } })(router.vuejs.org)route.state(reactive), rather than having people poke atrouter.options.history.stateor rawwindow.history.state. (github.com)Practical guidance
router.options.history.statefor app logic.state(router.push/replace) and (where available in your version) read it fromroute.state; otherwise, readwindow.history.statebut assume it may benulland that Vue Router also uses it internally. (typeerror.org)Citations:
🏁 Script executed:
Repository: npmx-dev/npmx.dev
Length of output: 94
🏁 Script executed:
Repository: npmx-dev/npmx.dev
Length of output: 345
🏁 Script executed:
rg "useCanGoBack" --type ts --type tsx -A 3 -B 3Repository: npmx-dev/npmx.dev
Length of output: 88
🏁 Script executed:
rg "useCanGoBack" -A 3 -B 3Repository: npmx-dev/npmx.dev
Length of output: 1595
Use the browser History API or Vue Router's navigation state;
router.options.history.stateis not a documented public API and accessing.backis unsafe.router.options.history.stateis an internal, undocumented property and not recommended. The proper approach in Vue Router 4 is to either:window.history.statedirectly (assuming it may benull), orstateoption inrouter.push()/router.replace()and read fromroute.stateBeyond the API concern, the current code accesses
.backwithout guarding againstundefinedornull, which will fail if the browser history state is not initialised. If you must inspectwindow.history.state, use optional chaining:Boolean(window.history.state?.back).