fix(theme): run route watch in onMounted to read hash on client#23
Conversation
Hash is only available in the browser; register the route watch inside onMounted so we don't access window.location.hash during SSR.
WalkthroughA Vue component was refactored to move route observation logic from top-level into an Changes
Estimated code review effort🎯 2 (Simple) | ⏱️ ~10 minutes Poem
🚥 Pre-merge checks | ✅ 3✅ Passed checks (3 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing touches
🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Actionable comments posted: 1
🤖 Fix all issues with AI agents
In @.vitepress/theme/components/customContent.vue:
- Around line 280-282: The linkUrl is only updated when route.path changes, so
hash-only navigations leave linkUrl stale; add a watcher that reacts to hash
changes and updates linkUrl.value accordingly (e.g., observe
window.location.hash or route.hash) for the "/tiny-robot" branch, and also
update linkUrl.value inside the existing handleMessage listener when receiving
the "playground-hash-change" message so both the browser URL and the iframe src
stay in sync; target the linkUrl, route.path check for "/tiny-robot", and the
handleMessage ("playground-hash-change") code paths.
🧹 Nitpick comments (1)
.vitepress/theme/components/customContent.vue (1)
267-296: Consider consolidating the twoonMountedhooks.There are two separate
onMountedcalls (lines 267 and 313). While both work correctly in Vue 3, merging them into a singleonMountedblock would be slightly cleaner.♻️ Suggested consolidation
onMounted(() => { watch( () => route.path, () => { // ... existing watch logic ... }, { immediate: true }, ); -}); - -onMounted(() => { window.addEventListener("message", handleMessage); });Also applies to: 313-315
Hash is only available in the browser; register the route watch inside onMounted so we don't access window.location.hash during SSR.
关联pr:opentiny/docs#78
Summary by CodeRabbit