From c2e56f1c2e84d72464c667bd02bed4b33cb643d4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Aleksa=20Siri=C5=A1ki?= <31509435+aleksasiriski@users.noreply.github.com> Date: Fri, 10 Nov 2023 17:48:44 +0100 Subject: [PATCH] fix: remove double api call (on ssr and csr) --- src/routes/healthz/api/private/+page.server.ts | 2 +- src/routes/healthz/api/public/+page.ts | 2 +- src/routes/search/+page.svelte | 2 +- src/routes/search/+page.ts | 18 +++++++++++++----- 4 files changed, 16 insertions(+), 8 deletions(-) diff --git a/src/routes/healthz/api/private/+page.server.ts b/src/routes/healthz/api/private/+page.server.ts index ce8f10b6..56bc7ec8 100644 --- a/src/routes/healthz/api/private/+page.server.ts +++ b/src/routes/healthz/api/private/+page.server.ts @@ -1,7 +1,7 @@ export const csr = false; -import type { PageServerLoad } from './$types'; import { env } from '$env/dynamic/private'; +import type { PageServerLoad } from './$types'; export const load: PageServerLoad = async ({ fetch }) => { const apiUrl = `${env.API_URL}/healthz`; diff --git a/src/routes/healthz/api/public/+page.ts b/src/routes/healthz/api/public/+page.ts index da012dff..b67d44bc 100644 --- a/src/routes/healthz/api/public/+page.ts +++ b/src/routes/healthz/api/public/+page.ts @@ -1,7 +1,7 @@ export const ssr = false; -import type { PageLoad } from './$types'; import { env } from '$env/dynamic/public'; +import type { PageLoad } from './$types'; export const load: PageLoad = async ({ fetch }) => { const apiUrl = `${env.PUBLIC_API_URL}/healthz`; diff --git a/src/routes/search/+page.svelte b/src/routes/search/+page.svelte index e3af59ea..6c455f65 100644 --- a/src/routes/search/+page.svelte +++ b/src/routes/search/+page.svelte @@ -24,7 +24,7 @@
- {#each data.results as result (result.URL)} + {#each data.results as result}
{result.URL}

diff --git a/src/routes/search/+page.ts b/src/routes/search/+page.ts index e2e85246..bf7d732b 100644 --- a/src/routes/search/+page.ts +++ b/src/routes/search/+page.ts @@ -1,6 +1,6 @@ -import type { PageLoad } from './$types'; import { env } from '$env/dynamic/public'; import { browser } from '$app/environment'; +import type { PageLoad } from './$types'; export const load: PageLoad = async ({ fetch, url }) => { const q = url.searchParams.get('q'); @@ -17,13 +17,21 @@ export const load: PageLoad = async ({ fetch, url }) => { } else { apiUri = env.PUBLIC_API_URL_CSR; } - const apiUrl = `${apiUri}/search?${url.searchParams}`; - const response = await fetch(apiUrl); - const results = await response.json(); + + // it's important to use the included fetch so that the fetch is not run again in the browser + const resultsP = new Promise((resolve, reject) => { + fetch(apiUrl) + .then((res) => { + resolve(res.json()); + }) + .catch((err) => { + reject(err); + }); + }); return { query: q, - results: results + results: browser ? resultsP : await resultsP }; };