fix(store): ensure trailing slash on storefront routing and redirect /store to prevent blank page on reload (#83)

This commit is contained in:
ShivaniKhaire 2026-11-07 15:46:46 +05:30
parent 6410d21e66
commit 874221fdd8
2 changed files with 35 additions and 6 deletions

View File

@ -36,8 +36,8 @@ export default function App() {
setActiveTab(tab); setActiveTab(tab);
if (typeof window !== 'undefined') { if (typeof window !== 'undefined') {
const isStorePrefix = window.location.pathname.startsWith('/store'); const isStorePrefix = window.location.pathname.startsWith('/store');
const basePath = isStorePrefix ? '/store' : ''; const basePath = isStorePrefix ? '/store/' : '/';
const newPath = tab === 'storefront' ? (basePath || '/') : (basePath + '/' + tab); const newPath = tab === 'storefront' ? basePath : `${basePath}${tab}`;
if (window.location.pathname !== newPath) { if (window.location.pathname !== newPath) {
window.history.pushState({ tab }, '', newPath); window.history.pushState({ tab }, '', newPath);
} }
@ -235,6 +235,12 @@ export default function App() {
// Load configuration & user profile on startup // Load configuration & user profile on startup
useEffect(() => { 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 params = new URLSearchParams(window.location.search);
const urlToken = params.get('token'); const urlToken = params.get('token');
const urlPlanCode = params.get('planCode'); const urlPlanCode = params.get('planCode');
@ -269,8 +275,10 @@ export default function App() {
} }
if (urlToken || urlPlanCode) { if (urlToken || urlPlanCode) {
const newUrl = window.location.pathname; const isStorePrefix = window.location.pathname.startsWith('/store');
window.history.replaceState({}, document.title, newUrl); const basePath = isStorePrefix ? '/store/' : '/';
const cleanPath = activeTab === 'subscriptions' ? `${basePath}subscriptions` : basePath;
window.history.replaceState({}, document.title, cleanPath);
} }
// Fetch public Razorpay Config // Fetch public Razorpay Config

View File

@ -1,11 +1,32 @@
import { defineConfig } from 'vite' import { defineConfig } from 'vite'
import react from '@vitejs/plugin-react' 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/ // https://vite.dev/config/
export default defineConfig({ export default defineConfig({
base: process.env.VITE_BASE_PATH || '/store/', base: rawBase,
plugins: [react()], 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: { server: {
port: 3000, port: 3000,
} }
}) })