From 455dc6571e424edd3ba58af6586d3dd8cc062a39 Mon Sep 17 00:00:00 2001 From: Padreug Date: Sat, 2 May 2026 08:57:34 +0200 Subject: [PATCH] feat: add standalone marketplace PWA build MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Adds a standalone Nostr marketplace PWA at market.${domain}, built from the existing src/modules/market plugin. Same standalone pattern as wallet/chat/castle/activities: - market.html entry, vite.market.config.ts (outDir: dist-market, manifest id: market-app, theme: red #dc2626 — Muladhara chakra) - src/market-app/{main.ts, app.ts, app.config.ts, App.vue} bootstraps base + market only, with acceptTokenFromUrl for shared auth from hub - npm run dev:market / build:market / preview:market - main app SW denylist extended with /market/, /cart/, /checkout/ Closes #18. Co-Authored-By: Claude Opus 4.7 (1M context) --- market.html | 19 +++++ package.json | 3 + src/market-app/App.vue | 47 ++++++++++++ src/market-app/app.config.ts | 53 ++++++++++++++ src/market-app/app.ts | 137 +++++++++++++++++++++++++++++++++++ src/market-app/main.ts | 17 +++++ vite.config.ts | 2 +- vite.market.config.ts | 115 +++++++++++++++++++++++++++++ 8 files changed, 392 insertions(+), 1 deletion(-) create mode 100644 market.html create mode 100644 src/market-app/App.vue create mode 100644 src/market-app/app.config.ts create mode 100644 src/market-app/app.ts create mode 100644 src/market-app/main.ts create mode 100644 vite.market.config.ts diff --git a/market.html b/market.html new file mode 100644 index 0000000..52feccb --- /dev/null +++ b/market.html @@ -0,0 +1,19 @@ + + + + + + + + + + + Market — Nostr + + + + +
+ + + diff --git a/package.json b/package.json index da537c9..bf0a573 100644 --- a/package.json +++ b/package.json @@ -15,6 +15,9 @@ "dev:castle": "vite --host --config vite.castle.config.ts", "build:castle": "vue-tsc -b && vite build --config vite.castle.config.ts", "preview:castle": "vite preview --host --config vite.castle.config.ts", + "dev:market": "vite --host --config vite.market.config.ts", + "build:market": "vue-tsc -b && vite build --config vite.market.config.ts", + "preview:market": "vite preview --host --config vite.market.config.ts", "electron:dev": "concurrently \"vite --host\" \"electron-forge start\"", "electron:build": "vue-tsc -b && vite build && electron-builder", "electron:package": "electron-builder", diff --git a/src/market-app/App.vue b/src/market-app/App.vue new file mode 100644 index 0000000..8c7f8ba --- /dev/null +++ b/src/market-app/App.vue @@ -0,0 +1,47 @@ + + + diff --git a/src/market-app/app.config.ts b/src/market-app/app.config.ts new file mode 100644 index 0000000..e222aa4 --- /dev/null +++ b/src/market-app/app.config.ts @@ -0,0 +1,53 @@ +import type { AppConfig } from '@/core/types' + +/** + * Standalone Market app configuration. + * Only enables base + market modules. + */ +export const appConfig: AppConfig = { + modules: { + base: { + name: 'base', + enabled: true, + lazy: false, + config: { + nostr: { + relays: JSON.parse(import.meta.env.VITE_NOSTR_RELAYS || '["wss://relay.damus.io", "wss://nos.lol"]') + }, + auth: { + sessionTimeout: 24 * 60 * 60 * 1000, + }, + pwa: { + autoPrompt: true + }, + imageUpload: { + baseUrl: import.meta.env.VITE_PICTRS_BASE_URL || 'https://img.mydomain.com', + maxSizeMB: 10, + acceptedTypes: ['image/jpeg', 'image/png', 'image/webp', 'image/avif', 'image/gif'] + } + } + }, + market: { + name: 'market', + enabled: true, + lazy: false, + config: { + defaultCurrency: 'sats', + paymentTimeout: 300000, + maxOrderHistory: 50, + apiConfig: { + baseUrl: import.meta.env.VITE_LNBITS_BASE_URL || 'http://localhost:5000' + } + } + }, + }, + + features: { + pwa: true, + pushNotifications: true, + electronApp: false, + developmentMode: import.meta.env.DEV + } +} + +export default appConfig diff --git a/src/market-app/app.ts b/src/market-app/app.ts new file mode 100644 index 0000000..bdeab91 --- /dev/null +++ b/src/market-app/app.ts @@ -0,0 +1,137 @@ +import { createApp } from 'vue' +import { createRouter, createWebHistory } from 'vue-router' +import { createPinia } from 'pinia' +import { pluginManager } from '@/core/plugin-manager' +import { eventBus } from '@/core/event-bus' +import { container } from '@/core/di-container' + +import appConfig from './app.config' +import baseModule from '@/modules/base' +import marketModule from '@/modules/market' + +import App from './App.vue' + +import '@/assets/index.css' +import { i18n, changeLocale, type AvailableLocale } from '@/i18n' + +function acceptTokenFromUrl() { + const params = new URLSearchParams(window.location.search) + const token = params.get('token') + if (token) { + localStorage.setItem('lnbits_access_token', token) + params.delete('token') + const clean = params.toString() + const newUrl = window.location.pathname + (clean ? `?${clean}` : '') + window.location.hash + window.history.replaceState({}, '', newUrl) + console.log('[Market] Auth token accepted from URL') + } +} + +export async function createAppInstance() { + console.log('Starting Market app...') + + acceptTokenFromUrl() + + const app = createApp(App) + + const moduleRoutes = [ + ...baseModule.routes || [], + ...marketModule.routes || [], + ].filter(Boolean) + + const router = createRouter({ + history: createWebHistory(import.meta.env.BASE_URL), + routes: [ + { + path: '/', + redirect: '/market' + }, + { + path: '/login', + name: 'login', + component: import.meta.env.VITE_DEMO_MODE === 'true' + ? () => import('@/pages/LoginDemo.vue') + : () => import('@/pages/Login.vue'), + meta: { requiresAuth: false } + }, + ...moduleRoutes, + ] + }) + + const pinia = createPinia() + + app.use(router) + app.use(pinia) + app.use(i18n) + + const defaultLocale = import.meta.env.VITE_DEFAULT_LOCALE as AvailableLocale | undefined + if (defaultLocale && !localStorage.getItem('user-locale')) { + await changeLocale(defaultLocale) + } + + pluginManager.init(app, router) + + const moduleRegistrations = [] + + if (appConfig.modules.base.enabled) { + moduleRegistrations.push( + pluginManager.register(baseModule, appConfig.modules.base) + ) + } + + if (appConfig.modules.market?.enabled) { + moduleRegistrations.push( + pluginManager.register(marketModule, appConfig.modules.market) + ) + } + + await Promise.all(moduleRegistrations) + await pluginManager.installAll() + + const { auth } = await import('@/composables/useAuthService') + await auth.initialize() + + router.beforeEach(async (to, _from, next) => { + const requiresAuth = to.meta.requiresAuth === true + + if (requiresAuth && !auth.isAuthenticated.value) { + next('/login') + } else if (to.path === '/login' && auth.isAuthenticated.value) { + next('/') + } else { + next() + } + }) + + app.config.errorHandler = (err, _vm, info) => { + console.error('Global error:', err, info) + eventBus.emit('app:error', { error: err, info }, 'app') + } + + if (appConfig.features.developmentMode) { + ;(window as any).__pluginManager = pluginManager + ;(window as any).__eventBus = eventBus + ;(window as any).__container = container + } + + console.log('Market app initialized') + return { app, router } +} + +export async function startApp() { + try { + const { app } = await createAppInstance() + app.mount('#app') + console.log('Market app started!') + eventBus.emit('app:started', {}, 'app') + } catch (error) { + console.error('Failed to start Market app:', error) + document.getElementById('app')!.innerHTML = ` +
+

Failed to Start

+

${error instanceof Error ? error.message : 'Unknown error'}

+

Please refresh the page.

+
+ ` + } +} diff --git a/src/market-app/main.ts b/src/market-app/main.ts new file mode 100644 index 0000000..58c349a --- /dev/null +++ b/src/market-app/main.ts @@ -0,0 +1,17 @@ +import { startApp } from './app' +import { registerSW } from 'virtual:pwa-register' +import 'vue-sonner/style.css' + +const intervalMS = 60 * 60 * 1000 +registerSW({ + onRegistered(r) { + r && setInterval(() => { + r.update() + }, intervalMS) + }, + onOfflineReady() { + console.log('Market app ready to work offline') + } +}) + +startApp() diff --git a/vite.config.ts b/vite.config.ts index fee1c90..7b8d6e5 100644 --- a/vite.config.ts +++ b/vite.config.ts @@ -25,7 +25,7 @@ export default defineConfig(({ mode }) => ({ '**/*.{js,css,html,ico,png,svg}' ], // Don't intercept standalone app paths — they have their own service workers - navigateFallbackDenylist: [/^\/sortir\//, /^\/castle\//], + navigateFallbackDenylist: [/^\/sortir\//, /^\/castle\//, /^\/market\//, /^\/cart\//, /^\/checkout\//], }, includeAssets: [ 'favicon.ico', diff --git a/vite.market.config.ts b/vite.market.config.ts new file mode 100644 index 0000000..0a90c68 --- /dev/null +++ b/vite.market.config.ts @@ -0,0 +1,115 @@ +import { fileURLToPath, URL } from 'node:url' +import vue from '@vitejs/plugin-vue' +import tailwindcss from '@tailwindcss/vite' +import { defineConfig, type Plugin } from 'vite' +import { VitePWA } from 'vite-plugin-pwa' +import { ViteImageOptimizer } from 'vite-plugin-image-optimizer' +import { visualizer } from 'rollup-plugin-visualizer' + +function marketHtmlPlugin(): Plugin { + return { + name: 'market-html-rewrite', + configureServer(server) { + server.middlewares.use((req, _res, next) => { + if ( + req.url && + !req.url.startsWith('/@') && + !req.url.startsWith('/src/') && + !req.url.startsWith('/node_modules/') && + !req.url.includes('.') + ) { + req.url = '/market.html' + } + next() + }) + }, + } +} + +/** + * Vite config for the standalone Market app. + * + * Set VITE_BASE_PATH to deploy under a path prefix: + * VITE_BASE_PATH=/market/ → app.${domain}/market/ (shared auth) + * (default: /) → market.${domain} (standalone subdomain) + */ +export default defineConfig(({ mode }) => ({ + base: process.env.VITE_BASE_PATH || '/', + plugins: [ + marketHtmlPlugin(), + vue(), + tailwindcss(), + VitePWA({ + registerType: 'autoUpdate', + devOptions: { enabled: true }, + workbox: { + globPatterns: ['**/*.{js,css,html,ico,png,svg}'], + navigateFallback: 'market.html', + navigateFallbackAllowlist: [ + new RegExp(`^${(process.env.VITE_BASE_PATH || '/').replace(/[.*+?^${}()|[\]\\]/g, '\\$&')}`), + ], + }, + includeAssets: [ + 'favicon.ico', + 'apple-touch-icon.png', + 'mask-icon.svg', + 'icon-192.png', + 'icon-512.png', + 'icon-maskable-192.png', + 'icon-maskable-512.png', + ], + manifest: { + name: 'Market — Nostr', + short_name: 'Market', + description: 'Decentralized marketplace on Nostr with Lightning payments', + theme_color: '#dc2626', + background_color: '#ffffff', + display: 'standalone', + orientation: 'portrait-primary', + start_url: process.env.VITE_BASE_PATH || '/', + scope: process.env.VITE_BASE_PATH || '/', + id: 'market-app', + categories: ['shopping', 'business'], + lang: 'en', + icons: [ + { src: 'icon-192.png', sizes: '192x192', type: 'image/png', purpose: 'any' }, + { src: 'icon-512.png', sizes: '512x512', type: 'image/png', purpose: 'any' }, + { src: 'icon-maskable-192.png', sizes: '192x192', type: 'image/png', purpose: 'maskable' }, + { src: 'icon-maskable-512.png', sizes: '512x512', type: 'image/png', purpose: 'maskable' }, + ], + }, + }), + ViteImageOptimizer({ + jpg: { quality: 80 }, + png: { quality: 80 }, + webp: { lossless: true }, + }), + mode === 'analyze' && + visualizer({ + open: true, + filename: 'dist-market/stats.html', + gzipSize: true, + brotliSize: true, + }), + ], + resolve: { + alias: { + '@': fileURLToPath(new URL('./src', import.meta.url)), + '@/app.config': fileURLToPath(new URL('./src/market-app/app.config.ts', import.meta.url)), + }, + }, + build: { + outDir: 'dist-market', + rollupOptions: { + input: 'market.html', + output: { + manualChunks: { + 'vue-vendor': ['vue', 'vue-router', 'pinia'], + 'ui-vendor': ['radix-vue', '@vueuse/core'], + 'shadcn': ['class-variance-authority', 'clsx', 'tailwind-merge'], + }, + }, + }, + chunkSizeWarningLimit: 1000, + }, +}))