From a5e6fcae5d9f2e6666bd7bfc1ba437cbfd36d726 Mon Sep 17 00:00:00 2001 From: Edmund Hung Date: Mon, 21 Nov 2022 20:34:13 +0100 Subject: [PATCH] fix: url redirection (#24) * fix: ensure graceful migration from the old urls * fix: missing resources index page --- app/routes/_layout.resources.index.tsx | 19 +++++++++++++++++++ app/routes/_layout.resources.tsx | 20 ++------------------ app/routes/discover.$list.tsx | 21 +++++++++++++++++++-- app/routes/discover.tsx | 15 +++++++++++++-- 4 files changed, 53 insertions(+), 22 deletions(-) create mode 100644 app/routes/_layout.resources.index.tsx diff --git a/app/routes/_layout.resources.index.tsx b/app/routes/_layout.resources.index.tsx new file mode 100644 index 0000000..5049120 --- /dev/null +++ b/app/routes/_layout.resources.index.tsx @@ -0,0 +1,19 @@ +import type { MetaFunction } from '@remix-run/cloudflare'; +import About from '~/components/About'; +import { formatMeta } from '~/helpers'; +import { getSearchOptions, getTitleBySearchOptions } from '~/search'; + +export const meta: MetaFunction = ({ location }) => { + const searchOptions = getSearchOptions( + `${location.pathname}${location.search}`, + ); + const title = getTitleBySearchOptions(searchOptions); + + return formatMeta({ + title, + }); +}; + +export default function Index() { + return ; +} diff --git a/app/routes/_layout.resources.tsx b/app/routes/_layout.resources.tsx index 7a03b14..d645e7a 100644 --- a/app/routes/_layout.resources.tsx +++ b/app/routes/_layout.resources.tsx @@ -1,15 +1,10 @@ -import type { LoaderArgs, MetaFunction } from '@remix-run/cloudflare'; +import type { LoaderArgs } from '@remix-run/cloudflare'; import { json, redirect } from '@remix-run/cloudflare'; import { Outlet, useLoaderData, useLocation } from '@remix-run/react'; import type { ShouldReloadFunction } from '@remix-run/react'; import Feed from '~/components/Feed'; import { search } from '~/resources'; -import { - getRelatedSearchParams, - getSearchOptions, - getTitleBySearchOptions, -} from '~/search'; -import { formatMeta } from '~/helpers'; +import { getRelatedSearchParams, getSearchOptions } from '~/search'; export async function loader({ request, context }: LoaderArgs) { const profile = await context.session.getUserProfile(); @@ -37,17 +32,6 @@ export async function loader({ request, context }: LoaderArgs) { } } -export const meta: MetaFunction = ({ location }) => { - const searchOptions = getSearchOptions( - `${location.pathname}${location.search}`, - ); - const title = getTitleBySearchOptions(searchOptions); - - return formatMeta({ - title, - }); -}; - export const unstable_shouldReload: ShouldReloadFunction = ({ url, prevUrl, diff --git a/app/routes/discover.$list.tsx b/app/routes/discover.$list.tsx index 4c13309..2c37235 100644 --- a/app/routes/discover.$list.tsx +++ b/app/routes/discover.$list.tsx @@ -2,7 +2,24 @@ import type { LoaderArgs } from '@remix-run/cloudflare'; import { redirect } from '@remix-run/cloudflare'; export function loader({ request, params }: LoaderArgs) { - const url = new URL(request.url); + if (!params.list) { + throw new Error('params.list is not available'); + } - throw redirect(`/${params.list}${url.search}`, 301); + const { searchParams } = new URL(request.url); + const resourceId = searchParams.get('resourceId'); + + searchParams.delete('resourceId'); + + let url = `/${params.list}`; + + if (resourceId) { + searchParams.set('list', params.list); + url = `/resources/${resourceId}?${searchParams}`; + } else if (Array.from(searchParams.keys()).length > 0) { + searchParams.set('list', params.list); + url = `/resources?${searchParams}`; + } + + throw redirect(url, 301); } diff --git a/app/routes/discover.tsx b/app/routes/discover.tsx index 5bc228a..cc6c395 100644 --- a/app/routes/discover.tsx +++ b/app/routes/discover.tsx @@ -2,7 +2,18 @@ import type { LoaderArgs } from '@remix-run/cloudflare'; import { redirect } from '@remix-run/cloudflare'; export function loader({ request }: LoaderArgs) { - const url = new URL(request.url); + const { searchParams } = new URL(request.url); + const resourceId = searchParams.get('resourceId'); - throw redirect(`/${url.search}`, 301); + searchParams.delete('resourceId'); + + const search = + Array.from(searchParams.keys()).length > 0 + ? `?${searchParams.toString()}` + : ''; + const url = resourceId + ? `/resources/${resourceId}${search}` + : `/resources${search}`; + + throw redirect(url, 301); }