|
1 | | -import React from 'react'; |
2 | | -import { BrowserRouter as Router, Routes, Route, Navigate } from 'react-router-dom'; |
| 1 | +import React, { useEffect } from 'react'; |
| 2 | +import { BrowserRouter as Router, Routes, Route, Navigate, useNavigate, useLocation } from 'react-router-dom'; |
3 | 3 | import Layout from './components/Layout'; |
4 | 4 | import SearchPage from './pages/SearchPage'; |
5 | 5 | import DirectoryPage from './pages/DirectoryPage'; |
6 | 6 | import GeneratorPage from './pages/GeneratorPage'; |
7 | 7 | import StatusPage from './pages/StatusPage'; |
8 | 8 | import './styles/index.css'; |
9 | 9 |
|
| 10 | +// Component to handle GitHub Pages SPA redirect |
| 11 | +function RedirectHandler() { |
| 12 | + const navigate = useNavigate(); |
| 13 | + const location = useLocation(); |
| 14 | + |
| 15 | + useEffect(() => { |
| 16 | + // Check for redirect parameter from 404.html |
| 17 | + const urlParams = new URLSearchParams(location.search); |
| 18 | + const redirectPath = urlParams.get('p'); |
| 19 | + |
| 20 | + if (redirectPath) { |
| 21 | + // Clean up the URL by removing the redirect parameter |
| 22 | + const newUrl = new URL(window.location); |
| 23 | + newUrl.searchParams.delete('p'); |
| 24 | + window.history.replaceState({}, '', newUrl.pathname + newUrl.search + newUrl.hash); |
| 25 | + |
| 26 | + // Navigate to the intended path |
| 27 | + navigate(redirectPath, { replace: true }); |
| 28 | + } |
| 29 | + }, [navigate, location.search]); |
| 30 | + |
| 31 | + return null; |
| 32 | +} |
| 33 | + |
10 | 34 | function App() { |
11 | 35 | console.log('Needle Demo App loading...'); |
12 | 36 |
|
13 | 37 | return ( |
14 | 38 | <Router> |
15 | 39 | <div className="App"> |
| 40 | + <RedirectHandler /> |
16 | 41 | <Layout> |
17 | 42 | <Routes> |
18 | 43 | <Route path="/" element={<Navigate to="/search" replace />} /> |
|
0 commit comments