Skip to content

Commit

Permalink
fix: url redirection (#24)
Browse files Browse the repository at this point in the history
* fix: ensure graceful migration from the old urls

* fix: missing resources index page
  • Loading branch information
edmundhung authored Nov 21, 2022
1 parent 4147b73 commit a5e6fca
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 22 deletions.
19 changes: 19 additions & 0 deletions app/routes/_layout.resources.index.tsx
Original file line number Diff line number Diff line change
@@ -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 <About />;
}
20 changes: 2 additions & 18 deletions app/routes/_layout.resources.tsx
Original file line number Diff line number Diff line change
@@ -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();
Expand Down Expand Up @@ -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,
Expand Down
21 changes: 19 additions & 2 deletions app/routes/discover.$list.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
15 changes: 13 additions & 2 deletions app/routes/discover.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

0 comments on commit a5e6fca

Please sign in to comment.