From 874221fdd8bb5e3f412715ec1f095a37f3afa006 Mon Sep 17 00:00:00 2001 From: ShivaniKhaire Date: Sat, 7 Nov 2026 15:46:46 +0530 Subject: [PATCH] fix(store): ensure trailing slash on storefront routing and redirect /store to prevent blank page on reload (#83) --- src/App.tsx | 16 ++++++++++++---- vite.config.ts | 25 +++++++++++++++++++++++-- 2 files changed, 35 insertions(+), 6 deletions(-) diff --git a/src/App.tsx b/src/App.tsx index 9dfc89b..b3c36d0 100644 --- a/src/App.tsx +++ b/src/App.tsx @@ -36,8 +36,8 @@ export default function App() { setActiveTab(tab); if (typeof window !== 'undefined') { const isStorePrefix = window.location.pathname.startsWith('/store'); - const basePath = isStorePrefix ? '/store' : ''; - const newPath = tab === 'storefront' ? (basePath || '/') : (basePath + '/' + tab); + const basePath = isStorePrefix ? '/store/' : '/'; + const newPath = tab === 'storefront' ? basePath : `${basePath}${tab}`; if (window.location.pathname !== newPath) { window.history.pushState({ tab }, '', newPath); } @@ -235,6 +235,12 @@ export default function App() { // Load configuration & user profile on startup useEffect(() => { + // Normalize URL if opened at /store without trailing slash + if (typeof window !== 'undefined' && window.location.pathname === '/store') { + const normalizedPath = '/store/' + (window.location.search || '') + (window.location.hash || ''); + window.history.replaceState(window.history.state, document.title, normalizedPath); + } + const params = new URLSearchParams(window.location.search); const urlToken = params.get('token'); const urlPlanCode = params.get('planCode'); @@ -269,8 +275,10 @@ export default function App() { } if (urlToken || urlPlanCode) { - const newUrl = window.location.pathname; - window.history.replaceState({}, document.title, newUrl); + const isStorePrefix = window.location.pathname.startsWith('/store'); + const basePath = isStorePrefix ? '/store/' : '/'; + const cleanPath = activeTab === 'subscriptions' ? `${basePath}subscriptions` : basePath; + window.history.replaceState({}, document.title, cleanPath); } // Fetch public Razorpay Config diff --git a/vite.config.ts b/vite.config.ts index 0d9f089..198efe6 100644 --- a/vite.config.ts +++ b/vite.config.ts @@ -1,11 +1,32 @@ import { defineConfig } from 'vite' import react from '@vitejs/plugin-react' +const rawBase = process.env.VITE_BASE_PATH || '/store/' +const baseWithoutSlash = rawBase.endsWith('/') ? rawBase.slice(0, -1) : rawBase + // https://vite.dev/config/ export default defineConfig({ - base: process.env.VITE_BASE_PATH || '/store/', - plugins: [react()], + base: rawBase, + plugins: [ + react(), + { + name: 'redirect-base-middleware', + configureServer(server) { + server.middlewares.use((req, res, next) => { + const [pathname, search] = (req.url || '').split('?') + if (baseWithoutSlash && pathname === baseWithoutSlash) { + const target = `${rawBase}${search ? `?${search}` : ''}` + res.writeHead(302, { Location: target }) + res.end() + return + } + next() + }) + } + } + ], server: { port: 3000, } }) +