diff --git a/devbox.json b/devbox.json new file mode 100644 index 00000000..94f2e977 --- /dev/null +++ b/devbox.json @@ -0,0 +1,16 @@ +{ + "packages": [ + "nodejs@latest", + "nodePackages.pnpm@latest" + ], + "shell": { + "init_hook": [ + "echo 'Welcome to devbox!' > /dev/null" + ], + "scripts": { + "test": [ + "echo \"Error: no test specified\" && exit 1" + ] + } + } +} diff --git a/devbox.lock b/devbox.lock new file mode 100644 index 00000000..8b4c6ae9 --- /dev/null +++ b/devbox.lock @@ -0,0 +1,39 @@ +{ + "lockfile_version": "1", + "packages": { + "nodePackages.pnpm@latest": { + "last_modified": "2023-11-02T13:29:18Z", + "resolved": "github:NixOS/nixpkgs/b644d97bda6dae837d577e28383c10aa51e5e2d2#nodePackages.pnpm", + "source": "devbox-search", + "version": "8.10.2", + "systems": { + "aarch64-linux": { + "store_path": "/nix/store/n7ml2bssqq1rdlzbz8bgp4bgc2a8z2wd-pnpm-8.10.2" + }, + "x86_64-linux": { + "store_path": "/nix/store/fxgnndi9lyb8py91mg0hdq9wy0giqbfk-pnpm-8.10.2" + } + } + }, + "nodejs@latest": { + "last_modified": "2023-10-25T20:49:13Z", + "resolved": "github:NixOS/nixpkgs/75a52265bda7fd25e06e3a67dee3f0354e73243c#nodejs_21", + "source": "devbox-search", + "version": "21.1.0", + "systems": { + "aarch64-darwin": { + "store_path": "/nix/store/b8yb6gs0dhi0zlxcg5dkp0d2xrn80nhh-nodejs-21.1.0" + }, + "aarch64-linux": { + "store_path": "/nix/store/c20ngwkh3xzpzvmxkffccaxygyiqw3n2-nodejs-21.1.0" + }, + "x86_64-darwin": { + "store_path": "/nix/store/6wyanl2ba86pc2jb6nvg212xpq7j7kij-nodejs-21.1.0" + }, + "x86_64-linux": { + "store_path": "/nix/store/wl7yqxb1mhghpp6b8icykcm5wysfqarz-nodejs-21.1.0" + } + } + } + } +} diff --git a/src/routes/search/+error.svelte b/src/routes/search/+error.svelte new file mode 100644 index 00000000..a0ccb50d --- /dev/null +++ b/src/routes/search/+error.svelte @@ -0,0 +1,28 @@ + + +Not Found | BrzaGuza + + +
+
+

+ 500 +

+ +

+ Fastasst API failed. +

+ + + +

Go back to Assland

+ +
+
+
diff --git a/src/routes/search/+page.svelte b/src/routes/search/+page.svelte index 6c455f65..e3af59ea 100644 --- a/src/routes/search/+page.svelte +++ b/src/routes/search/+page.svelte @@ -24,7 +24,7 @@
- {#each data.results as result} + {#each data.results as result (result.URL)}
{result.URL}

diff --git a/src/routes/search/+page.ts b/src/routes/search/+page.ts index bf7d732b..6508b482 100644 --- a/src/routes/search/+page.ts +++ b/src/routes/search/+page.ts @@ -1,13 +1,16 @@ import { env } from '$env/dynamic/public'; import { browser } from '$app/environment'; + import type { PageLoad } from './$types'; +import type { Result } from './types'; -export const load: PageLoad = async ({ fetch, url }) => { +export const load: PageLoad = async ({ fetch, url, setHeaders }) => { const q = url.searchParams.get('q'); if (q === null || q === '') { + const results: Result[] = []; return { query: '', - results: [] + results: results }; } @@ -17,21 +20,22 @@ 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: Result[] = 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); - }); - }); + const age = response.headers.get('age'); + const cacheControl = response.headers.get('cache-control'); + if (age != null && cacheControl != null) { + setHeaders({ + age: age, + 'cache-control': cacheControl + }); + } return { query: q, - results: browser ? resultsP : await resultsP + results: results }; }; diff --git a/src/routes/search/types.ts b/src/routes/search/types.ts new file mode 100644 index 00000000..cdda8b78 --- /dev/null +++ b/src/routes/search/types.ts @@ -0,0 +1,17 @@ +type EngineRank = { + SearchEngine: string; + Rank: number; + Page: number; + OnPageRank: number; +}; + +export type Result = { + URL: string; + Rank: number; + Score: number; + Title: string; + Description: string; + EngineRanks: EngineRank[]; + TimesReturned: number; + Response: any; +};