From f4f1283cd5c1ac461e80d3128c113b64252b6006 Mon Sep 17 00:00:00 2001 From: AJ ONeal Date: Sat, 21 Oct 2023 14:43:56 -0600 Subject: [PATCH] feat: enable static builds --- package-lock.json | 17 ++++++++++++++++ package.json | 1 + src/lib/constants.ts | 8 ++++---- src/routes/+layout.js | 16 +++++++++++++++ src/routes/+page.server.ts | 28 +++----------------------- src/routes/+page.svelte | 40 ++++++++++++++++++++++++++------------ svelte.config.js | 8 ++++++-- 7 files changed, 75 insertions(+), 43 deletions(-) create mode 100644 src/routes/+layout.js diff --git a/package-lock.json b/package-lock.json index 7ce789258..4e8b9ed4c 100644 --- a/package-lock.json +++ b/package-lock.json @@ -18,6 +18,7 @@ }, "devDependencies": { "@sveltejs/adapter-auto": "^2.0.0", + "@sveltejs/adapter-static": "^2.0.3", "@sveltejs/kit": "^1.20.4", "@typescript-eslint/eslint-plugin": "^6.0.0", "@typescript-eslint/parser": "^6.0.0", @@ -759,6 +760,15 @@ "@sveltejs/kit": "^1.0.0" } }, + "node_modules/@sveltejs/adapter-static": { + "version": "2.0.3", + "resolved": "https://registry.npmjs.org/@sveltejs/adapter-static/-/adapter-static-2.0.3.tgz", + "integrity": "sha512-VUqTfXsxYGugCpMqQv1U0LIdbR3S5nBkMMDmpjGVJyM6Q2jHVMFtdWJCkeHMySc6mZxJ+0eZK3T7IgmUCDrcUQ==", + "dev": true, + "peerDependencies": { + "@sveltejs/kit": "^1.5.0" + } + }, "node_modules/@sveltejs/kit": { "version": "1.26.0", "resolved": "https://registry.npmjs.org/@sveltejs/kit/-/kit-1.26.0.tgz", @@ -4254,6 +4264,13 @@ "rollup": "^3.7.0" } }, + "@sveltejs/adapter-static": { + "version": "2.0.3", + "resolved": "https://registry.npmjs.org/@sveltejs/adapter-static/-/adapter-static-2.0.3.tgz", + "integrity": "sha512-VUqTfXsxYGugCpMqQv1U0LIdbR3S5nBkMMDmpjGVJyM6Q2jHVMFtdWJCkeHMySc6mZxJ+0eZK3T7IgmUCDrcUQ==", + "dev": true, + "requires": {} + }, "@sveltejs/kit": { "version": "1.26.0", "resolved": "https://registry.npmjs.org/@sveltejs/kit/-/kit-1.26.0.tgz", diff --git a/package.json b/package.json index ea472a6f1..f6259075b 100644 --- a/package.json +++ b/package.json @@ -17,6 +17,7 @@ }, "devDependencies": { "@sveltejs/adapter-auto": "^2.0.0", + "@sveltejs/adapter-static": "^2.0.3", "@sveltejs/kit": "^1.20.4", "@typescript-eslint/eslint-plugin": "^6.0.0", "@typescript-eslint/parser": "^6.0.0", diff --git a/src/lib/constants.ts b/src/lib/constants.ts index d76fce8a1..c6e18bf9c 100644 --- a/src/lib/constants.ts +++ b/src/lib/constants.ts @@ -1,7 +1,7 @@ import { browser, dev } from '$app/environment'; -export const ENDPOINT = browser - ? `http://${location.hostname}:11434` +export const API_ENDPOINT = browser + ? `https://localhost/api` : dev - ? 'http://127.0.0.1:11434' - : 'http://host.docker.internal:11434'; + ? `http://localhost:11434/api` + : 'http://host.docker.internal:11434/api'; diff --git a/src/routes/+layout.js b/src/routes/+layout.js new file mode 100644 index 000000000..8a35c5d4a --- /dev/null +++ b/src/routes/+layout.js @@ -0,0 +1,16 @@ +// if you want to generate a static html file +// for your page. +// Documentation: https://kit.svelte.dev/docs/page-options#prerender +export const prerender = true; + +// if you want to Generate a SPA +// you have to set ssr to false. +// This is not the case (so set as true or comment the line) +// Documentation: https://kit.svelte.dev/docs/page-options#ssr +export const ssr = true; + +// How to manage the trailing slashes in the URLs +// the URL for about page witll be /about with 'ignore' (default) +// the URL for about page witll be /about/ with 'always' +// https://kit.svelte.dev/docs/page-options#trailingslash +export const trailingSlash = 'ignore'; diff --git a/src/routes/+page.server.ts b/src/routes/+page.server.ts index f02869827..2899c2fb9 100644 --- a/src/routes/+page.server.ts +++ b/src/routes/+page.server.ts @@ -1,30 +1,8 @@ -import { ENDPOINT } from '$lib/constants'; import type { PageServerLoad } from './$types'; -export const load: PageServerLoad = async ({ url }) => { - const OLLAMA_ENDPOINT = process.env.OLLAMA_ENDPOINT; - console.log(OLLAMA_ENDPOINT); - const models = await fetch( - `${OLLAMA_ENDPOINT != undefined ? OLLAMA_ENDPOINT : ENDPOINT}/api/tags`, - { - method: 'GET', - headers: { - Accept: 'application/json', - 'Content-Type': 'application/json' - } - } - ) - .then(async (res) => { - if (!res.ok) throw await res.json(); - return res.json(); - }) - .catch((error) => { - console.log(error); - return null; - }); - +export const load: PageServerLoad = () => { + const API_ENDPOINT = process.env.API_ENDPOINT; return { - models: models?.models ?? [], - OLLAMA_ENDPOINT: process.env.OLLAMA_ENDPOINT + API_ENDPOINT }; }; diff --git a/src/routes/+page.svelte b/src/routes/+page.svelte index 063fd4278..7c747c261 100644 --- a/src/routes/+page.svelte +++ b/src/routes/+page.svelte @@ -9,18 +9,23 @@ import 'highlight.js/styles/dark.min.css'; import type { PageData } from './$types'; - import { ENDPOINT as SERVER_ENDPOINT } from '$lib/constants'; + import { API_ENDPOINT as DEV_API_ENDPOINT } from '$lib/constants'; import { onMount, tick } from 'svelte'; import { page } from '$app/stores'; - const suggestions = $page.url.searchParams.get('suggestions'); + const suggestions = ''; // $page.url.searchParams.get('suggestions'); import Navbar from '$lib/components/layout/Navbar.svelte'; import SettingsModal from '$lib/components/chat/SettingsModal.svelte'; - export let data: PageData; - $: ({ models, OLLAMA_ENDPOINT } = data); + /* export let data: PageData; */ + /* $: ({ API_ENDPOINT } = data); */ + /* if (!API_ENDPOINT) { */ + /* API_ENDPOINT = DEV_API_ENDPOINT; */ + /* } */ + /* console.log('API_ENDPOINT',API_ENDPOINT) */ + /* console.log('DEV_API_ENDPOINT', DEV_API_ENDPOINT) */ - let ENDPOINT; + let models = []; let textareaElement; let showSettings = false; let db; @@ -36,10 +41,21 @@ let messages = []; onMount(async () => { - ENDPOINT = OLLAMA_ENDPOINT ? OLLAMA_ENDPOINT : SERVER_ENDPOINT; - console.log(OLLAMA_ENDPOINT); - console.log(SERVER_ENDPOINT); - console.log(ENDPOINT); + /* console.log('API_ENDPOINT 2', API_ENDPOINT) */ + const resp = await fetch(`${DEV_API_ENDPOINT}/tags`, { + method: 'GET', + headers: { + Accept: 'application/json', + 'Content-Type': 'application/json' + } + }); + if (!resp.ok) { + let msg = await resp.text(); + let err = new Error(msg); + throw err; + } + const data = await resp.json(); + models = data.models; let settings = localStorage.getItem('settings'); if (settings) { @@ -267,7 +283,7 @@ messages = [...messages, responseMessage]; window.scrollTo({ top: document.body.scrollHeight }); - const res = await fetch(`${ENDPOINT}/api/generate`, { + const res = await fetch(`${API_ENDPOINT}/generate`, { method: 'POST', headers: { 'Content-Type': 'text/event-stream' @@ -363,7 +379,7 @@ messages = [...messages, responseMessage]; window.scrollTo({ top: document.body.scrollHeight }); - const res = await fetch(`${ENDPOINT}/api/generate`, { + const res = await fetch(`${API_ENDPOINT}/generate`, { method: 'POST', headers: { 'Content-Type': 'text/event-stream' @@ -443,7 +459,7 @@ const generateTitle = async (user_prompt) => { console.log('generateTitle'); - const res = await fetch(`${ENDPOINT}/api/generate`, { + const res = await fetch(`${API_ENDPOINT}/generate`, { method: 'POST', headers: { 'Content-Type': 'text/event-stream' diff --git a/svelte.config.js b/svelte.config.js index 2214c60d6..fdcd0148d 100644 --- a/svelte.config.js +++ b/svelte.config.js @@ -1,4 +1,4 @@ -import adapter from '@sveltejs/adapter-node'; +import adapter from '@sveltejs/adapter-static'; import { vitePreprocess } from '@sveltejs/kit/vite'; /** @type {import('@sveltejs/kit').Config} */ @@ -11,7 +11,11 @@ const config = { // adapter-auto only supports some environments, see https://kit.svelte.dev/docs/adapter-auto for a list. // If your environment is not supported or you settled on a specific environment, switch out the adapter. // See https://kit.svelte.dev/docs/adapters for more information about adapters. - adapter: adapter() + adapter: adapter({ + pages: 'build', + assets: 'build', + fallback: null + }) } };