From bf431dcf449bf42eee28b2ebd8106f95145bcbcb Mon Sep 17 00:00:00 2001 From: Alicia Sykes Date: Sun, 10 May 2026 18:59:20 +0100 Subject: [PATCH] feat: Protocol no longer required when searching --- src/client/utils/address-type-checker.ts | 9 +++++++ src/client/views/Home.tsx | 17 +++++-------- src/client/views/Results.tsx | 9 +++++-- src/components/homepage/AnimatedInput.astro | 13 +++++++++- src/components/homepage/HeroForm.astro | 28 +++------------------ src/pages/check/[...target].astro | 17 +++++++------ 6 files changed, 46 insertions(+), 47 deletions(-) diff --git a/src/client/utils/address-type-checker.ts b/src/client/utils/address-type-checker.ts index 9856014..94be8b7 100644 --- a/src/client/utils/address-type-checker.ts +++ b/src/client/utils/address-type-checker.ts @@ -42,3 +42,12 @@ export const determineAddressType = (address: string | undefined): AddressType = if (isUrl(address)) return 'url'; return 'err'; }; + +// Strip protocol and path/query/hash so the route param stays a bare host +export const normalizeAddress = (input: string | undefined): string => { + if (!input) return ''; + let s = input.trim().replace(/^https?:\/\//i, ''); + const stop = s.search(/[/?#]/); + if (stop !== -1) s = s.slice(0, stop); + return s; +}; diff --git a/src/client/views/Home.tsx b/src/client/views/Home.tsx index 7ac2de6..37c295b 100644 --- a/src/client/views/Home.tsx +++ b/src/client/views/Home.tsx @@ -11,7 +11,7 @@ import FancyBackground from 'client/components/misc/FancyBackground'; import docs from 'client/utils/docs'; import colors from 'client/styles/colors'; -import { determineAddressType } from 'client/utils/address-type-checker'; +import { determineAddressType, normalizeAddress } from 'client/utils/address-type-checker'; const HomeContainer = styled.section` display: flex; @@ -140,7 +140,7 @@ const SiteFeaturesWrapper = styled(StyledCard)` `; const Home = (): JSX.Element => { - const defaultPlaceholder = 'e.g. https://duck.com/'; + const defaultPlaceholder = 'e.g. duck.com'; const [userInput, setUserInput] = useState(''); const [errorMsg, setErrMsg] = useState(''); const [placeholder] = useState(defaultPlaceholder); @@ -149,18 +149,17 @@ const Home = (): JSX.Element => { const location = useLocation(); - /* Redirect strait to results, if somehow we land on /check?url=[] */ useEffect(() => { const query = new URLSearchParams(location.search); const urlFromQuery = query.get('url'); if (urlFromQuery) { - navigate(`/check/${encodeURIComponent(urlFromQuery)}`, { replace: true }); + const target = normalizeAddress(urlFromQuery); + if (target) navigate(`/check/${target}`, { replace: true }); } }, [navigate, location.search]); - /* Check is valid address, either show err or redirect to results page */ const submit = () => { - let address = userInput.endsWith('/') ? userInput.slice(0, -1) : userInput; + const address = normalizeAddress(userInput); const addressType = determineAddressType(address); if (addressType === 'empt') { @@ -168,12 +167,8 @@ const Home = (): JSX.Element => { } else if (addressType === 'err') { setErrMsg('Must be a valid URL, IPv4 or IPv6 Address'); } else { - // if the addressType is 'url' and address doesn't start with 'http://' or 'https://', prepend 'https://' - if (addressType === 'url' && !/^https?:\/\//i.test(address)) { - address = 'https://' + address; - } const resultRouteParams: NavigateOptions = { state: { address, addressType } }; - navigate(`/check/${encodeURIComponent(address)}`, resultRouteParams); + navigate(`/check/${address}`, resultRouteParams); } }; diff --git a/src/client/views/Results.tsx b/src/client/views/Results.tsx index e971028..03c857c 100644 --- a/src/client/views/Results.tsx +++ b/src/client/views/Results.tsx @@ -58,7 +58,8 @@ const ResultsContent = styled.section` const makeSiteName = (address: string): string => { try { - return new URL(address).hostname.replace('www.', ''); + const withScheme = /^https?:\/\//i.test(address) ? address : `https://${address}`; + return new URL(withScheme).hostname.replace(/^www\./, ''); } catch { return address; } @@ -175,7 +176,11 @@ const Results = (props: { address?: string }): JSX.Element => { {address && ( {addressType === 'url' && ( - + )} diff --git a/src/components/homepage/AnimatedInput.astro b/src/components/homepage/AnimatedInput.astro index 2f3c528..5a65bd5 100644 --- a/src/components/homepage/AnimatedInput.astro +++ b/src/components/homepage/AnimatedInput.astro @@ -11,7 +11,18 @@ const placeholders = [ ---
- +
{ diff --git a/src/components/homepage/HeroForm.astro b/src/components/homepage/HeroForm.astro index 7010651..08e24f5 100644 --- a/src/components/homepage/HeroForm.astro +++ b/src/components/homepage/HeroForm.astro @@ -28,37 +28,15 @@ import Screenshots from './Screenshots.astro';
diff --git a/src/pages/check/[...target].astro b/src/pages/check/[...target].astro index 1747203..b4767fb 100644 --- a/src/pages/check/[...target].astro +++ b/src/pages/check/[...target].astro @@ -2,6 +2,7 @@ import BaseLayout from '@layouts/Base.astro'; import Main from '../../client/main.tsx'; import '../../client/styles/index.css'; +import { normalizeAddress } from '../../client/utils/address-type-checker'; export const prerender = false; @@ -10,7 +11,8 @@ const { search } = new URL(Astro.request.url); const searchUrl = new URLSearchParams(search).get('url'); if (searchUrl) { - Astro.redirect(`/check/${encodeURIComponent(searchUrl)}`); + const target = normalizeAddress(searchUrl); + if (target) Astro.redirect(`/check/${target}`); } --- @@ -27,22 +29,21 @@ if (searchUrl) {