pwa fixes

This commit is contained in:
Piotr Domański 2026-04-03 22:13:20 +02:00
parent 6b41eafc55
commit 9bbc8cd0b3
2 changed files with 127 additions and 118 deletions

View file

@ -1,79 +1,87 @@
<script lang="ts">
import { login } from '$lib/api/auth';
import { goto } from '$app/navigation';
import { auth } from '$lib/auth/store.svelte';
import { onMount } from 'svelte';
import { today } from '$lib/utils/date';
import { login } from "$lib/api/auth";
import { goto } from "$app/navigation";
import { auth } from "$lib/auth/store.svelte";
import { onMount } from "svelte";
import { today } from "$lib/utils/date";
let username = $state('');
let password = $state('');
let error = $state('');
let loading = $state(false);
let username = $state("");
let password = $state("");
let error = $state("");
let loading = $state(false);
onMount(() => {
if (auth.isAuthenticated) goto(`/diary/${today()}`);
});
onMount(() => {
if (auth.isAuthenticated) goto(`/diary/${today()}`);
});
async function handleSubmit(e: SubmitEvent) {
e.preventDefault();
error = '';
loading = true;
try {
await login(username, password);
goto(`/diary/${today()}`);
} catch (err: unknown) {
error = 'Invalid username or password';
} finally {
loading = false;
}
}
async function handleSubmit(e: SubmitEvent) {
e.preventDefault();
error = "";
loading = true;
try {
await login(username, password);
goto(`/diary/${today()}`);
} catch (err: unknown) {
error = "Invalid username or password";
} finally {
loading = false;
}
}
</script>
<div class="min-h-screen flex items-center justify-center px-4">
<div class="w-full max-w-sm">
<h1 class="text-3xl font-bold text-center mb-8 text-green-500">Fooder</h1>
<div class="w-full max-w-sm">
<h1 class="text-3xl font-bold text-center mb-8 text-green-500">Fooder</h1>
<form onsubmit={handleSubmit} class="space-y-4">
<div>
<label for="username" class="block text-sm font-medium text-zinc-400 mb-1">Username</label>
<input
id="username"
type="text"
bind:value={username}
autocomplete="username"
required
class="w-full bg-zinc-900 border border-zinc-700 rounded-xl px-4 py-3 text-zinc-100 focus:outline-none focus:border-green-500 transition-colors"
/>
</div>
<form onsubmit={handleSubmit} class="space-y-4">
<div>
<label
for="username"
class="block text-sm font-medium text-zinc-400 mb-1">Username</label
>
<input
id="username"
type="text"
bind:value={username}
autocomplete="username"
required
class="w-full bg-zinc-900 border border-zinc-700 rounded-xl px-4 py-3 text-zinc-100 focus:outline-none focus:border-green-500 transition-colors"
/>
</div>
<div>
<label for="password" class="block text-sm font-medium text-zinc-400 mb-1">Password</label>
<input
id="password"
type="password"
bind:value={password}
autocomplete="current-password"
required
class="w-full bg-zinc-900 border border-zinc-700 rounded-xl px-4 py-3 text-zinc-100 focus:outline-none focus:border-green-500 transition-colors"
/>
</div>
<div>
<label
for="password"
class="block text-sm font-medium text-zinc-400 mb-1">Password</label
>
<input
id="password"
type="password"
bind:value={password}
autocomplete="current-password"
required
class="w-full bg-zinc-900 border border-zinc-700 rounded-xl px-4 py-3 text-zinc-100 focus:outline-none focus:border-green-500 transition-colors"
/>
</div>
{#if error}
<p class="text-red-400 text-sm">{error}</p>
{/if}
{#if error}
<p class="text-red-400 text-sm">{error}</p>
{/if}
<button
type="submit"
disabled={loading}
class="w-full bg-green-600 hover:bg-green-500 disabled:opacity-50 rounded-xl py-3 font-semibold transition-colors"
>
{loading ? 'Signing in…' : 'Sign in'}
</button>
</form>
<button
type="submit"
disabled={loading}
class="w-full bg-green-600 hover:bg-green-500 disabled:opacity-50 rounded-xl py-3 font-semibold transition-colors"
>
{loading ? "Signing in…" : "Sign in"}
</button>
</form>
<p class="text-center mt-6 text-zinc-500 text-sm">
No account?
<a href="/register" class="text-green-500 hover:text-green-400">Register</a>
</p>
</div>
<p class="text-center mt-6 text-zinc-500 text-sm">
No account?
<a href="/register" class="text-green-500 hover:text-green-400"
>Register</a
>
</p>
</div>
</div>

View file

@ -4,56 +4,57 @@ import { VitePWA } from 'vite-plugin-pwa';
import tailwindcss from '@tailwindcss/vite';
export default defineConfig({
server: {
proxy: {
'/api': {
target: process.env.VITE_API_URL ?? 'http://localhost:8000',
changeOrigin: true
}
}
},
plugins: [
tailwindcss(),
sveltekit(),
VitePWA({
registerType: 'autoUpdate',
manifestFilename: 'manifest.json',
includeAssets: ['icons/icon.svg', 'icons/apple-touch-icon-180x180.png'],
devOptions: { enabled: true },
manifest: {
name: 'Fooder',
short_name: 'Fooder',
description: 'Simple calorie and macro tracker',
theme_color: '#16a34a',
background_color: '#09090b',
display: 'standalone',
orientation: 'portrait',
start_url: '/diary/today',
id: '/',
categories: ['health', 'food'],
icons: [
{ src: '/icons/pwa-64x64.png', sizes: '64x64', type: 'image/png' },
{ src: '/icons/pwa-192x192.png', sizes: '192x192', type: 'image/png' },
{ src: '/icons/pwa-512x512.png', sizes: '512x512', type: 'image/png' },
{ src: '/icons/maskable-icon-512x512.png', sizes: '512x512', type: 'image/png', purpose: 'maskable' }
]
},
workbox: {
navigateFallback: '/index.html',
globPatterns: ['**/*.{js,css,html,ico,png,svg,woff2}'],
runtimeCaching: [
{
// Cache same-origin API responses (NetworkFirst: try network, fall back to cache)
urlPattern: ({ url }) => url.pathname.startsWith('/api/'),
handler: 'NetworkFirst',
options: {
cacheName: 'api-cache',
networkTimeoutSeconds: 5,
cacheableResponse: { statuses: [200] }
}
}
]
}
})
]
server: {
proxy: {
'/api': {
target: process.env.VITE_API_URL ?? 'http://localhost:8000',
changeOrigin: true
}
}
},
plugins: [
tailwindcss(),
sveltekit(),
VitePWA({
registerType: 'autoUpdate',
manifestFilename: 'manifest.json',
includeAssets: ['icons/icon.svg', 'icons/apple-touch-icon-180x180.png'],
devOptions: { enabled: true },
manifest: {
name: 'Fooder',
short_name: 'Fooder',
description: 'Simple calorie and macro tracker',
theme_color: '#16a34a',
background_color: '#09090b',
display: 'standalone',
orientation: 'portrait',
start_url: '/diary/today',
scope: '/',
id: '/',
categories: ['health', 'food'],
icons: [
{ src: '/icons/pwa-64x64.png', sizes: '64x64', type: 'image/png' },
{ src: '/icons/pwa-192x192.png', sizes: '192x192', type: 'image/png' },
{ src: '/icons/pwa-512x512.png', sizes: '512x512', type: 'image/png' },
{ src: '/icons/maskable-icon-512x512.png', sizes: '512x512', type: 'image/png', purpose: 'maskable' }
]
},
workbox: {
navigateFallback: '/index.html',
globPatterns: ['**/*.{js,css,html,ico,png,svg,woff2}'],
runtimeCaching: [
{
// Cache same-origin API responses (NetworkFirst: try network, fall back to cache)
urlPattern: ({ url }) => url.pathname.startsWith('/api/'),
handler: 'NetworkFirst',
options: {
cacheName: 'api-cache',
networkTimeoutSeconds: 5,
cacheableResponse: { statuses: [200] }
}
}
]
}
})
]
});