Fix sploosh build stat pages returning 404

This commit is contained in:
Kalle
2023-03-29 21:45:26 +03:00
parent 935b5ff955
commit 7a249522a4
3 changed files with 11 additions and 5 deletions

View File

@@ -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}`);

View File

@@ -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}`);

View File

@@ -9,6 +9,13 @@ export function notFoundIfFalsy<T>(value: T | null | undefined): T {
return value;
}
export function notFoundIfNullLike<T>(value: T | null | undefined): T {
if (value === null || value === undefined)
throw new Response(null, { status: 404 });
return value;
}
export function badRequestIfFalsy<T>(value: T | null | undefined): T {
if (!value) throw new Response(null, { status: 400 });