From 3991265addba87726a99509250bbdc562d157863 Mon Sep 17 00:00:00 2001 From: skidoodle Date: Wed, 24 Jan 2024 10:37:11 +0100 Subject: [PATCH 1/2] optimize link validation --- src/pages/api/validate.ts | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/src/pages/api/validate.ts b/src/pages/api/validate.ts index 1963150..004d0ac 100644 --- a/src/pages/api/validate.ts +++ b/src/pages/api/validate.ts @@ -6,6 +6,7 @@ export default async function handler( ) { const { link } = req.query as { link: string } let missingParam: string | null = null + if (!link) { missingParam = 'link' } @@ -15,20 +16,26 @@ export default async function handler( } const domain = link.split('/')[2] - if (domain !== 'erettsegi.albert.lol') { + if (domain !== 'localhost:3000' && domain !== 'erettsegi.albert.lol') { return res.status(400).json({ error: 'Érvénytelen link' }) } try { - res.setHeader('Cache-Control', 's-maxage=31536000') const { protocol, host } = new URL(link) - if (protocol && host) { - const response = await fetch(link, { method: 'HEAD' }) - const status = response.status - res.status(200).json({ status }) - } else { + if (!protocol || !host) { return res.status(400).json({ error: 'Érvénytelen link' }) } + + const response = await fetch(link, { method: 'OPTIONS' }) + + if (!response.ok) { + return res + .status(400) + .json({ error: 'Invalid host or network unreachable' }) + } + + const status = response.status + res.status(200).json({ status }) } catch (error) { res.status(500).json({ error: 'Internal Server Error' }) } From 36a8fd8780594e1cec01db13cd3f26ece9c40049 Mon Sep 17 00:00:00 2001 From: skidoodle Date: Wed, 24 Jan 2024 10:37:19 +0100 Subject: [PATCH 2/2] use deterministic returns --- src/pages/_error.tsx | 50 ++++++++++++++++++++++++++------------------ 1 file changed, 30 insertions(+), 20 deletions(-) diff --git a/src/pages/_error.tsx b/src/pages/_error.tsx index bb273f5..50529de 100644 --- a/src/pages/_error.tsx +++ b/src/pages/_error.tsx @@ -1,12 +1,33 @@ import { Footer } from '@/components/Footer' import { Button } from '@nextui-org/button' -import Link from 'next/link' import type { GetServerSideProps, GetServerSidePropsContext } from 'next' interface ErrorProps { statusCode: number } +const NotFound: React.FC = () => ( + <> +

Az keresett oldal nem található.

+

+ +

+ +) + +const Unexpected: React.FC = () => ( + <> +

Váratlan hiba történt.

+

+ +

+ +) + const ErrorPage: React.FC = ({ statusCode }) => { return ( <> @@ -19,25 +40,14 @@ const ErrorPage: React.FC = ({ statusCode }) => {
- {statusCode == 404 ? ( - <> -

Az keresett oldal nem található.

-

- -

- - ) : ( - <> -

Váratlan hiba történt.

-

- -

- - )} + {(() => { + switch (statusCode) { + case 404: + return + default: + return + } + })()}