Skip to content

web search done right

don't show discussions widget if there is a user applied optic

Mikkel Denkera4a3a8d1ed08
diff --git a/frontend/src/routes/search/+page.server.ts b/frontend/src/routes/search/+page.server.ts
index 67356118..f7f9c473 100644
--- a/frontend/src/routes/search/+page.server.ts
+++ b/frontend/src/routes/search/+page.server.ts
@@ -1,4 +1,9 @@
-import { discussionsOptic, extractSearchParams, type SearchParams, type SearchResults } from '$lib/search';
+import {
+ discussionsOptic,
+ extractSearchParams,
+ type SearchParams,
+ type SearchResults,
+} from '$lib/search';
import { redirect } from '@sveltejs/kit';
import type { Actions } from './$types';
import { api } from '$lib/api';
@@ -23,7 +28,7 @@ export const load = async ({ locals, fetch, url, getClientAddress }) => {
redirect(301, '/');
}
- let start = Date.now();
+ const start = Date.now();
const { data: websitesReq } = api.search(
{
@@ -38,60 +43,70 @@ export const load = async ({ locals, fetch, url, getClientAddress }) => {
{ fetch, headers: { 'X-Forwarded-For': getClientAddress() } },
);
- const { data: widgetReq } = params.currentPage == 1 ?
- api.searchWidget(
- {
- query: params.query,
- },
- { fetch, headers: { 'X-Forwarded-For': getClientAddress() } },
- )
- : { data: undefined };
-
- const { data: sidebarReq } = params.currentPage == 1 ?
- api.searchSidebar(
- {
- query: params.query,
- },
- { fetch, headers: { 'X-Forwarded-For': getClientAddress() } },
- )
- : { data: undefined };
-
- const { data: discussionsReq } = params.currentPage == 1 ?
- api.search(
- {
- query: params.query,
- optic: discussionsOptic,
- numResults: 10,
- safeSearch: params.safeSearch,
- selectedRegion: params.selectedRegion,
- hostRankings: params.host_rankings,
- returnRankingSignals: true,
- countResults: false,
- },
- { fetch, headers: { 'X-Forwarded-For': getClientAddress() } },
- )
- : { data: undefined };
-
- const [websites, widget, sidebar, discussionsRes] = await Promise.all([websitesReq, widgetReq, sidebarReq, discussionsReq]);
- const discussions = discussionsRes?.type == "websites" ? discussionsRes.webpages : undefined;
-
- let results: SearchResults = websites.type == "websites" ? {
- ...websites,
- widget,
- sidebar,
- discussions,
- } : {
- ...websites,
- };
-
- if (results.type == "websites") {
+ const { data: widgetReq } =
+ params.currentPage == 1
+ ? api.searchWidget(
+ {
+ query: params.query,
+ },
+ { fetch, headers: { 'X-Forwarded-For': getClientAddress() } },
+ )
+ : { data: undefined };
+
+ const { data: sidebarReq } =
+ params.currentPage == 1
+ ? api.searchSidebar(
+ {
+ query: params.query,
+ },
+ { fetch, headers: { 'X-Forwarded-For': getClientAddress() } },
+ )
+ : { data: undefined };
+
+ const { data: discussionsReq } =
+ params.currentPage == 1 && params.optic == undefined
+ ? api.search(
+ {
+ query: params.query,
+ optic: discussionsOptic,
+ numResults: 10,
+ safeSearch: params.safeSearch,
+ selectedRegion: params.selectedRegion,
+ hostRankings: params.host_rankings,
+ returnRankingSignals: true,
+ countResults: false,
+ },
+ { fetch, headers: { 'X-Forwarded-For': getClientAddress() } },
+ )
+ : { data: undefined };
+
+ const [websites, widget, sidebar, discussionsRes] = await Promise.all([
+ websitesReq,
+ widgetReq,
+ sidebarReq,
+ discussionsReq,
+ ]);
+ const discussions = discussionsRes?.type == 'websites' ? discussionsRes.webpages : undefined;
+
+ const results: SearchResults =
+ websites.type == 'websites'
+ ? {
+ ...websites,
+ widget,
+ sidebar,
+ discussions,
+ }
+ : {
+ ...websites,
+ };
+
+ if (results.type == 'websites') {
results.searchDurationMs = Date.now() - start;
}
return { form: searchParams, results };
};
-
export const actions: Actions = {
default: async (event) => {
const { request } = event;