feat: enable static builds

This commit is contained in:
AJ ONeal 2023-10-21 14:43:56 -06:00
parent 78ebae6095
commit f4f1283cd5
No known key found for this signature in database
GPG key ID: 65118FF2A9DB590F
7 changed files with 75 additions and 43 deletions

17
package-lock.json generated
View file

@ -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",

View file

@ -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",

View file

@ -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';

16
src/routes/+layout.js Normal file
View file

@ -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';

View file

@ -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
};
};

View file

@ -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'

View file

@ -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
})
}
};