Browse Source

feat: enable static builds

AJ ONeal 1 year ago
parent
commit
f4f1283cd5
7 changed files with 75 additions and 43 deletions
  1. 17 0
      package-lock.json
  2. 1 0
      package.json
  3. 4 4
      src/lib/constants.ts
  4. 16 0
      src/routes/+layout.js
  5. 3 25
      src/routes/+page.server.ts
  6. 28 12
      src/routes/+page.svelte
  7. 6 2
      svelte.config.js

+ 17 - 0
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",

+ 1 - 0
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",

+ 4 - 4
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';

+ 16 - 0
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';

+ 3 - 25
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
 	};
 };

+ 28 - 12
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'

+ 6 - 2
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
+		})
 	}
 };