From fe64fe5be009dcd0bfe23fe80d38df7fb20653d2 Mon Sep 17 00:00:00 2001 From: Andrew Rabert Date: Mon, 29 Apr 2024 17:28:13 -0400 Subject: [PATCH] Fix legacy URL redirect --- src/hooks/useLegacyRouterSync.ts | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/src/hooks/useLegacyRouterSync.ts b/src/hooks/useLegacyRouterSync.ts index fe28df0905..c1af8fa310 100644 --- a/src/hooks/useLegacyRouterSync.ts +++ b/src/hooks/useLegacyRouterSync.ts @@ -2,8 +2,6 @@ import { Update } from 'history'; import { useLayoutEffect, useState } from 'react'; import type { History, Router } from '@remix-run/router'; -const normalizePath = (pathname: string) => pathname.replace(/^!/, ''); - interface UseLegacyRouterSyncProps { router: Router; history: History; @@ -20,8 +18,18 @@ export function useLegacyRouterSync({ router, history }: UseLegacyRouterSyncProp * implementation, so we need to remove the leading `!` from the pathname. React Router already removes the * hash for us. */ - if (update.location.pathname.startsWith('!')) { - history.replace(normalizePath(update.location.pathname), update.location.state); + if (update.location.pathname.startsWith('/!/')) { + history.replace( + { ...update.location, pathname: update.location.pathname.replace(/^\/!/, '') }, + update.location.state); + } else if (update.location.pathname.startsWith('/!')) { + history.replace( + { ...update.location, pathname: update.location.pathname.replace(/^\/!/, '/') }, + update.location.state); + } else if (update.location.pathname.startsWith('!')) { + history.replace( + { ...update.location, pathname: update.location.pathname.replace(/^!/, '') }, + update.location.state); } else if (!isSynced) { await router.navigate(update.location, { replace: true }); }