From 7a249522a4e13e974619f4aca286502dce675a82 Mon Sep 17 00:00:00 2001 From: Kalle <38327916+Sendouc@users.noreply.github.com> Date: Wed, 29 Mar 2023 21:45:26 +0300 Subject: [PATCH] Fix sploosh build stat pages returning 404 --- app/features/build-stats/routes/builds.$slug.popular.tsx | 4 ++-- app/features/build-stats/routes/builds.$slug.stats.tsx | 5 ++--- app/utils/remix.ts | 7 +++++++ 3 files changed, 11 insertions(+), 5 deletions(-) diff --git a/app/features/build-stats/routes/builds.$slug.popular.tsx b/app/features/build-stats/routes/builds.$slug.popular.tsx index 4e729c672..750134731 100644 --- a/app/features/build-stats/routes/builds.$slug.popular.tsx +++ b/app/features/build-stats/routes/builds.$slug.popular.tsx @@ -6,7 +6,7 @@ import { Ability } from "~/components/Ability"; import { Main } from "~/components/Main"; import { useTranslation } from "~/hooks/useTranslation"; import { i18next } from "~/modules/i18n"; -import { notFoundIfFalsy, type SendouRouteHandle } from "~/utils/remix"; +import { notFoundIfNullLike, type SendouRouteHandle } from "~/utils/remix"; import { makeTitle } from "~/utils/strings"; import { weaponNameSlugToId } from "~/utils/unslugify.server"; import { @@ -60,7 +60,7 @@ export const handle: SendouRouteHandle = { export const loader = async ({ params, request }: LoaderArgs) => { const t = await i18next.getFixedT(request, ["builds", "weapons", "common"]); const slug = params["slug"]; - const weaponId = notFoundIfFalsy(weaponNameSlugToId(slug)); + const weaponId = notFoundIfNullLike(weaponNameSlugToId(slug)); const weaponName = t(`weapons:MAIN_${weaponId}`); diff --git a/app/features/build-stats/routes/builds.$slug.stats.tsx b/app/features/build-stats/routes/builds.$slug.stats.tsx index 77f526c3f..e16a2e201 100644 --- a/app/features/build-stats/routes/builds.$slug.stats.tsx +++ b/app/features/build-stats/routes/builds.$slug.stats.tsx @@ -12,8 +12,7 @@ import { abilityPointCountsToAverages } from "../build-stats-utils"; import { Ability } from "~/components/Ability"; import styles from "../build-stats.css"; import { WeaponImage } from "~/components/Image"; -import type { SendouRouteHandle } from "~/utils/remix"; -import { notFoundIfFalsy } from "~/utils/remix"; +import { notFoundIfNullLike, type SendouRouteHandle } from "~/utils/remix"; import { MAX_AP, ONE_HOUR_IN_MS, TWELVE_HOURS_IN_MS } from "~/constants"; import { useTranslation } from "~/hooks/useTranslation"; import { @@ -70,7 +69,7 @@ export const handle: SendouRouteHandle = { export const loader = async ({ params, request }: LoaderArgs) => { const t = await i18next.getFixedT(request, ["builds", "weapons", "common"]); - const weaponId = notFoundIfFalsy(weaponNameSlugToId(params["slug"])); + const weaponId = notFoundIfNullLike(weaponNameSlugToId(params["slug"])); const weaponName = t(`weapons:MAIN_${weaponId}`); diff --git a/app/utils/remix.ts b/app/utils/remix.ts index 8b5ab58b1..616f4b039 100644 --- a/app/utils/remix.ts +++ b/app/utils/remix.ts @@ -9,6 +9,13 @@ export function notFoundIfFalsy(value: T | null | undefined): T { return value; } +export function notFoundIfNullLike(value: T | null | undefined): T { + if (value === null || value === undefined) + throw new Response(null, { status: 404 }); + + return value; +} + export function badRequestIfFalsy(value: T | null | undefined): T { if (!value) throw new Response(null, { status: 400 });