Skip to content

Commit 8faa727

Browse files
committed
Revert "Fix 404 redirect to always go to demo root page instead of preserving path"
This reverts commit 793bac0.
1 parent 793bac0 commit 8faa727

2 files changed

Lines changed: 33 additions & 5 deletions

File tree

demo/public/404.html

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,15 @@
99
<title>Needle Demo</title>
1010
<script>
1111
// GitHub Pages SPA redirect
12-
// Always redirect to the root demo page
12+
// Redirect to the main demo page and let React Router handle the routing
13+
var segmentCount = 2; // Number of path segments to keep (/Needle/demo/)
1314
var l = window.location;
14-
var basePath = '/Needle/demo/';
1515
l.replace(
1616
l.protocol + '//' + l.hostname + (l.port ? ':' + l.port : '') +
17-
basePath
17+
l.pathname.split('/').slice(0, 1 + segmentCount).join('/') + '/?p=' +
18+
encodeURIComponent(l.pathname.slice(1).split('/').slice(segmentCount).join('/')) +
19+
(l.search ? '&' + l.search.slice(1) : '') +
20+
l.hash
1821
);
1922
</script>
2023
</head>

demo/src/App.js

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,43 @@
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';
33
import Layout from './components/Layout';
44
import SearchPage from './pages/SearchPage';
55
import DirectoryPage from './pages/DirectoryPage';
66
import GeneratorPage from './pages/GeneratorPage';
77
import StatusPage from './pages/StatusPage';
88
import './styles/index.css';
99

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+
1034
function App() {
1135
console.log('Needle Demo App loading...');
1236

1337
return (
1438
<Router basename="/Needle/demo">
1539
<div className="App">
40+
<RedirectHandler />
1641
<Layout>
1742
<Routes>
1843
<Route path="/" element={<Navigate to="/search" replace />} />

0 commit comments

Comments
 (0)