Skip to content

web search done right

Setting to send search requests with POST instead of GET

Mikkel Denker96ea5530f839
diff --git a/core/src/ranking/mod.rs b/core/src/ranking/mod.rs
index b01bd357..237a5190 100644
--- a/core/src/ranking/mod.rs
+++ b/core/src/ranking/mod.rs
@@ -609,7 +609,7 @@ mod tests {
<meta property="og:description" content="example" />
</head>
<body>
- test
+ test example
</body>
</html>
"#,
diff --git a/frontend/src/lib/components/PostSearchSelect.svelte b/frontend/src/lib/components/PostSearchSelect.svelte
new file mode 100644
index 00000000..816da6f0
--- /dev/null
+++ b/frontend/src/lib/components/PostSearchSelect.svelte
@@ -0,0 +1,13 @@
+<script lang="ts">
+ import Select from '$lib/components/Select.svelte';
+ import { postSearchStore } from '$lib/stores';
+
+ let options = [
+ { value: void 0, label: 'GET' },
+ { value: true, label: 'POST' },
+ ];
+</script>
+
+<div class="noscript:hidden">
+ <Select bind:value={$postSearchStore} class="text-neutral-focus" {options} />
+</div>
\ No newline at end of file
diff --git a/frontend/src/lib/components/Searchbar.svelte b/frontend/src/lib/components/Searchbar.svelte
index da914ce8..489aefe5 100644
--- a/frontend/src/lib/components/Searchbar.svelte
+++ b/frontend/src/lib/components/Searchbar.svelte
@@ -2,7 +2,7 @@
import MagnifyingGlass from '~icons/heroicons/magnifying-glass';
import Button from '$lib/components/Button.svelte';
import { api } from '$lib/api';
- import { safeSearchStore, siteRankingsStore } from '$lib/stores';
+ import { safeSearchStore, siteRankingsStore, postSearchStore } from '$lib/stores';
import { browser } from '$app/environment';
import { derived } from 'svelte/store';
import { compressRanked, rankingsToRanked } from '$lib/rankings';
@@ -81,7 +81,7 @@
let hasFocus = autofocus;
</script>
-<form action="/search" class="flex w-full justify-center" id="searchbar-form">
+<form action="/search" class="flex w-full justify-center" id="searchbar-form" method={$postSearchStore ? "POST" : "GET"}>
<input type="hidden" value={$safeSearchStore ? 'true' : 'false'} name="ss" />
<input type="hidden" value={$compressedRanked} name="sr" id="siteRankingsUuid" />
diff --git a/frontend/src/lib/search.ts b/frontend/src/lib/search.ts
new file mode 100644
index 00000000..e01cf9f6
--- /dev/null
+++ b/frontend/src/lib/search.ts
@@ -0,0 +1,33 @@
+import type { Region } from "./api";
+import { decompressRanked, type RankedSites } from "./rankings";
+
+export type SearchParams = {
+ query: string;
+ currentPage: number;
+ optic: string | undefined;
+ selectedRegion: Region | undefined;
+ safeSearch: boolean;
+ compressedSiteRankings: string | null;
+ siteRankings: RankedSites | undefined;
+ };
+
+ export const extractSearchParams = (searchParams: URLSearchParams | FormData): SearchParams => {
+ const query = searchParams.get('q') as string | undefined ?? '';
+ const currentPage = parseInt(searchParams.get('p') as string | undefined ?? '1') || 1;
+ const optic = searchParams.get('optic') as string | undefined || void 0;
+ const selectedRegion = (searchParams.get('gl') as string | undefined || void 0) as Region | undefined;
+ const safeSearch = searchParams.get('ss') as string | undefined == 'true';
+ const compressedSiteRankings = searchParams.get('sr') as string | undefined || null;
+ const siteRankings = compressedSiteRankings ? decompressRanked(compressedSiteRankings) : void 0;
+
+ return {
+ query,
+ currentPage,
+ optic,
+ selectedRegion,
+ safeSearch,
+ compressedSiteRankings,
+ siteRankings,
+ };
+ };
+
\ No newline at end of file
diff --git a/frontend/src/lib/stores.ts b/frontend/src/lib/stores.ts
index d5b1600c..ec797412 100644
--- a/frontend/src/lib/stores.ts
+++ b/frontend/src/lib/stores.ts
@@ -39,6 +39,9 @@ const writableLocalStorage = <T>(
const SAFE_SEARCH_KEY = 'safeSearch';
export const safeSearchStore = writableLocalStorage<boolean>(SAFE_SEARCH_KEY, false);
+const POST_SEARCH_KEY = 'postSearch';
+export const postSearchStore = writableLocalStorage<boolean>(POST_SEARCH_KEY, false);
+
const OPTICS_KEY = 'optics';
export const opticsStore = writableLocalStorage<OpticOption[]>(OPTICS_KEY, []);
diff --git a/frontend/src/routes/search/+page.server.ts b/frontend/src/routes/search/+page.server.ts
new file mode 100644
index 00000000..6d5cadab
--- /dev/null
+++ b/frontend/src/routes/search/+page.server.ts
@@ -0,0 +1,19 @@
+import { extractSearchParams, type SearchParams } from '$lib/search';
+import type { Actions } from './$types';
+
+export const load = async ({locals}) => {
+ // @ts-ignore
+ const searchParams: SearchParams | undefined = (locals["form"] && extractSearchParams(locals['form'])) || undefined;
+ return {form: searchParams};
+}
+
+export const actions: Actions = {
+ default: async (event) => {
+ const { request } = event;
+
+ // @ts-ignore
+ event.locals.form = await request.formData();
+
+ return { success: true };
+ }
+} satisfies Actions;
\ No newline at end of file
diff --git a/frontend/src/routes/search/+page.ts b/frontend/src/routes/search/+page.ts
index b15ce62d..968447a2 100644
--- a/frontend/src/routes/search/+page.ts
+++ b/frontend/src/routes/search/+page.ts
@@ -3,11 +3,21 @@ import { redirect } from '@sveltejs/kit';
import type { PageLoad } from './$types';
import { fetchRemoteOptic } from '$lib/optics';
import { match } from 'ts-pattern';
-import { decompressRanked, type RankedSites } from '$lib/rankings';
+import { extractSearchParams } from '$lib/search';
import { globals } from '$lib/globals';
-export const load: PageLoad = async ({ fetch, url }) => {
- const params = extractSearchParams(url.searchParams);
+export const load: PageLoad = async (req) => {
+ const { fetch, url } = req;
+ var params = extractSearchParams(url.searchParams);
+
+ if (!params.query.trim()) {
+ const form = req.data['form'];
+ if (form) {
+ params = form;
+ } else {
+ throw redirect(300, '/');
+ }
+ }
if (!params.query.trim()) {
throw redirect(300, '/');
@@ -56,34 +66,4 @@ export const load: PageLoad = async ({ fetch, url }) => {
header: { divider: true },
}),
};
-};
-
-type SearchParams = {
- query: string;
- currentPage: number;
- optic: string | undefined;
- selectedRegion: Region | undefined;
- safeSearch: boolean;
- compressedSiteRankings: string | null;
- siteRankings: RankedSites | undefined;
-};
-
-const extractSearchParams = (searchParams: URLSearchParams): SearchParams => {
- const query = searchParams.get('q') ?? '';
- const currentPage = parseInt(searchParams.get('p') ?? '1') || 1;
- const optic = searchParams.get('optic') || void 0;
- const selectedRegion = (searchParams.get('gl') || void 0) as Region | undefined;
- const safeSearch = searchParams.get('ss') == 'true';
- const compressedSiteRankings = searchParams.get('sr');
- const siteRankings = compressedSiteRankings ? decompressRanked(compressedSiteRankings) : void 0;
-
- return {
- query,
- currentPage,
- optic,
- selectedRegion,
- safeSearch,
- compressedSiteRankings,
- siteRankings,
- };
-};
+};
\ No newline at end of file
diff --git a/frontend/src/routes/settings/+page.svelte b/frontend/src/routes/settings/+page.svelte
index 22ebcfdd..540cc1c4 100644
--- a/frontend/src/routes/settings/+page.svelte
+++ b/frontend/src/routes/settings/+page.svelte
@@ -1,5 +1,6 @@
<script lang="ts">
import ThemeSelect from '$lib/components/ThemeSelect.svelte';
+ import PostSearchSelect from '$lib/components/PostSearchSelect.svelte';
import { safeSearchStore } from '$lib/stores';
const safeSearchOptions = [
@@ -18,39 +19,50 @@
description: 'Pick a color scheme',
type: 'theme',
},
+ {
+ title: 'POST Search Requests',
+ description: 'Send search queries using POST instead of GET',
+ type: 'post',
+ }
] as const;
</script>
<div class="[&_a]:font-medium [&_a]:underline">
<div class="space-y-3">
<h1 class="text-2xl font-medium">Preferences</h1>
- <div>
+ <div class="flex flex-col space-y-5">
{#each settings as setting}
- <h2 class="text-lg">{setting.title}</h2>
- <div class="flex flex-col space-y-1 pr-5 sm:flex-row sm:justify-between sm:space-y-0">
- <p>{setting.description}</p>
- {#if setting.type == 'safe-search'}
- <div class="flex space-x-4 pr-2">
- {#each safeSearchOptions as [name, state]}
- <label class="flex cursor-pointer flex-col items-center" for="safe-search-{state}">
- <input
- type="radio"
- name="safe-search"
- id="safe-search-{state}"
- value={state}
- bind:group={$safeSearchStore}
- />
- <span class="capitalize">{name}</span>
- </label>
- {/each}
+ <div class="flex flex-col sm:flex-row sm:justify-between pr-1">
+ <div class="flex flex-col">
+ <h2 class="text-lg">{setting.title}</h2>
+ <p>{setting.description}</p>
+ </div>
+ <div class="flex flex-col pr-5 sm:flex-row sm:justify-between sm:space-y-0">
+ <div>
+ {#if setting.type == 'safe-search'}
+ <div class="flex space-x-4 pr-2">
+ {#each safeSearchOptions as [name, state]}
+ <label class="flex cursor-pointer flex-col items-center" for="safe-search-{state}">
+ <input
+ type="radio"
+ name="safe-search"
+ id="safe-search-{state}"
+ value={state}
+ bind:group={$safeSearchStore}
+ />
+ <span class="capitalize">{name}</span>
+ </label>
+ {/each}
+ </div>
+ {:else if setting.type == 'theme'}
+ <ThemeSelect />
+ {:else if setting.type == "post"}
+ <PostSearchSelect />
+ {/if}
</div>
- {:else if setting.type == 'theme'}
- <ThemeSelect />
- {/if}
+ </div>
</div>
{/each}
-
- <p class="pt-5 text-neutral/50">More to come soon</p>
</div>
</div>
</div>