Skip to content

Commit a7b557c

Browse files
committed
fix: Add router.isReady guards to prevent SSR issues in redirects
Add guards to ensure redirects only run on the client side after: - Router is ready - Required data (node, nodeId, etc.) is available This prevents potential issues during SSR/build time when router.query might be empty or router.isReady is false.
1 parent 63075b0 commit a7b557c

File tree

1 file changed

+9
-5
lines changed

1 file changed

+9
-5
lines changed

components/nodes/NodeDetails.tsx

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -190,8 +190,9 @@ const NodeDetails = () => {
190190
// redirect to correct node ID if the responded node.id doesn't match the URL nodeId
191191
// this handles cases where the node ID has changed or been normalized
192192
React.useEffect(() => {
193-
const isNodeIdMismatchedBetweenURLandNode =
194-
node && node.id && node.id !== nodeId
193+
if (!router.isReady || !node?.id || !nodeId) return
194+
195+
const isNodeIdMismatchedBetweenURLandNode = node.id !== nodeId
195196
if (isNodeIdMismatchedBetweenURLandNode) {
196197
// if we're on /publishers/[publisherId]/nodes/[nodeId], redirect to the correct publisher and node ID
197198
if (_publisherId) {
@@ -201,17 +202,20 @@ const NodeDetails = () => {
201202
router.replace(`/nodes/${node.id}`)
202203
}
203204
}
204-
}, [node, nodeId, _publisherId, publisherId, router])
205+
}, [router, node, nodeId, _publisherId, publisherId])
205206

206207
// redirect to correct /publishers/[publisherId]/nodes/[nodeId] if publisherId in query is different from the one in node
207208
// usually this happens when publisher changes, e.g. when user claims a node
208209
React.useEffect(() => {
210+
if (!router.isReady || !node || !_publisherId || !publisherId || !nodeId)
211+
return
212+
209213
const isPublisherIdMismatchedBetweenURLandNode =
210-
node && _publisherId && publisherId !== _publisherId
214+
publisherId !== _publisherId
211215
if (isPublisherIdMismatchedBetweenURLandNode) {
212216
router.replace(`/publishers/${publisherId}/nodes/${nodeId}`)
213217
}
214-
}, [node, _publisherId, publisherId, nodeId, router])
218+
}, [router, node, _publisherId, publisherId, nodeId])
215219

216220
const shouldShowLoading = isLoading || !router.isReady || !_nodeId
217221
if (shouldShowLoading) {

0 commit comments

Comments
 (0)