Skip to content

Commit 00f7304

Browse files
committed
Okay, REALLY ACTUALLY fixed the routing for real
this time. I think.
1 parent 9fc6df3 commit 00f7304

File tree

6 files changed

+37
-45
lines changed

6 files changed

+37
-45
lines changed

404.html

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,13 @@
44
<meta charset="utf-8">
55
<title>Redirecting...</title>
66
<script>
7-
// Simple redirect to index.html
8-
window.location.replace('/');
7+
if (window.location.pathname !== '/') {
8+
const path = window.location.pathname.replace(/^\//, '') + window.location.search;
9+
window.location.replace('/?p=' + encodeURIComponent(path));
10+
}
911
</script>
1012
</head>
1113
<body>
12-
<p>Redirecting to homepage...</p>
14+
<p>Redirecting...</p>
1315
</body>
1416
</html>

modules/navigation.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -293,14 +293,13 @@ export function getCurrentTranslation() {
293293
}
294294
export function navigateFromURL() {
295295
const urlParams = parseURL();
296-
if (!urlParams && window.location.pathname === '/') {
296+
if (!urlParams && !window.location.search) {
297297
const defaultParams = {
298298
translation: 'BSB',
299299
book: 'Genesis',
300300
chapter: 1
301301
};
302-
const newUrl = `/${defaultParams.translation.toLowerCase()}/${defaultParams.book.toLowerCase()}/${defaultParams.chapter}`;
303-
window.history.replaceState(defaultParams, '', newUrl);
302+
updateURL(defaultParams.translation, defaultParams.book, defaultParams.chapter);
304303
return loadDefaultPassage(defaultParams);
305304
}
306305
if (urlParams) {

modules/state.js

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -430,12 +430,15 @@ export function updateURL(translation, book, chapter) {
430430
}
431431
const cleanBook = bookAbbr.toLowerCase();
432432
const cleanChapter = Math.max(1, parseInt(chapter) || 1);
433-
const newURL = `#/${cleanTranslation}/${cleanBook}/${cleanChapter}`;
434-
window.location.hash = newURL;
433+
const newQuery = `?p=${cleanTranslation}/${cleanBook}/${cleanChapter}`;
434+
if (window.location.search !== newQuery) {
435+
window.history.replaceState({}, '', newQuery);
436+
}
435437
}
436438
export function parseURL() {
437-
const path = window.location.hash ? window.location.hash.substring(1) : window.location.pathname;
438-
if (path === '/' || path === '') {
439+
const urlParams = new URLSearchParams(window.location.search);
440+
const path = urlParams.get('p');
441+
if (!path) {
439442
return null;
440443
}
441444
const pathParts = path.split('/').filter(part => part !== '');
@@ -445,19 +448,15 @@ export function parseURL() {
445448
const chapter = parseInt(pathParts[2], 10);
446449
const book = ABBREVIATION_TO_BOOK_NAME[bookAbbreviation];
447450
if (!book) {
448-
console.warn('Invalid book abbreviation in URL:', bookAbbreviation);
451+
console.warn('Invalid book abbreviation in query:', bookAbbreviation);
449452
return null;
450453
}
451454
if (!AVAILABLE_TRANSLATIONS.includes(translation)) {
452-
console.warn('Invalid translation in URL:', translation);
453-
return null;
454-
}
455-
if (!BOOKS_ABBREVIATED.includes(bookAbbreviation)) {
456-
console.warn('Invalid book in URL:', book);
455+
console.warn('Invalid translation in query:', translation);
457456
return null;
458457
}
459458
if (isNaN(chapter) || chapter <= 0 || chapter > 150) {
460-
console.warn('Invalid chapter in URL:', chapter);
459+
console.warn('Invalid chapter in query:', chapter);
461460
return null;
462461
}
463462
return { translation, book, chapter };

src/404.html

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,13 @@
44
<meta charset="utf-8">
55
<title>Redirecting...</title>
66
<script>
7-
// Simple redirect to index.html
8-
window.location.replace('/');
7+
if (window.location.pathname !== '/') {
8+
const path = window.location.pathname.replace(/^\//, '') + window.location.search;
9+
window.location.replace('/?p=' + encodeURIComponent(path));
10+
}
911
</script>
1012
</head>
1113
<body>
12-
<p>Redirecting to homepage...</p>
14+
<p>Redirecting...</p>
1315
</body>
1416
</html>

src/modules/navigation.js

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -487,21 +487,14 @@ export function getCurrentTranslation() {
487487
/* Navigate based on URL parameters */
488488
export function navigateFromURL() {
489489
const urlParams = parseURL();
490-
491-
// Handle root path redirect
492-
if (!urlParams && window.location.pathname === '/') {
493-
// Default to BSB Genesis 1 for root path
490+
if (!urlParams && !window.location.search) {
494491
const defaultParams = {
495492
translation: 'BSB',
496493
book: 'Genesis',
497494
chapter: 1
498495
};
499496

500-
// Update URL without page reload
501-
const newUrl = `/${defaultParams.translation.toLowerCase()}/${defaultParams.book.toLowerCase()}/${defaultParams.chapter}`;
502-
window.history.replaceState(defaultParams, '', newUrl);
503-
504-
// Load the default passage
497+
updateURL(defaultParams.translation, defaultParams.book, defaultParams.chapter);
505498
return loadDefaultPassage(defaultParams);
506499
}
507500

src/modules/state.js

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -687,49 +687,46 @@ export function updateURL(translation, book, chapter) {
687687
const cleanBook = bookAbbr.toLowerCase();
688688
const cleanChapter = Math.max(1, parseInt(chapter) || 1);
689689

690-
const newURL = `#/${cleanTranslation}/${cleanBook}/${cleanChapter}`;
691-
window.location.hash = newURL;
690+
const newQuery = `?p=${cleanTranslation}/${cleanBook}/${cleanChapter}`;
691+
if (window.location.search !== newQuery) {
692+
window.history.replaceState({}, '', newQuery);
693+
}
692694
}
693695

696+
694697
/**
695698
* Parse URL parameters to extract translation, book, and chapter
696699
* @returns {Object|null} Object with translation, book, chapter or null if invalid
697700
*/
698701
export function parseURL() {
699-
const path = window.location.hash ? window.location.hash.substring(1) : window.location.pathname;
700-
if (path === '/' || path === '') {
702+
const urlParams = new URLSearchParams(window.location.search);
703+
const path = urlParams.get('p');
704+
705+
if (!path) {
701706
return null;
702707
}
703708

704709
const pathParts = path.split('/').filter(part => part !== '');
710+
705711
if (pathParts.length >= 3) {
706712
const translation = pathParts[0].toUpperCase();
707713
let bookAbbreviation = pathParts[1].toUpperCase();
708714
const chapter = parseInt(pathParts[2], 10);
709715

710-
// Convert abbreviation to full book name
711716
const book = ABBREVIATION_TO_BOOK_NAME[bookAbbreviation];
712717

713718
if (!book) {
714-
console.warn('Invalid book abbreviation in URL:', bookAbbreviation);
719+
console.warn('Invalid book abbreviation in query:', bookAbbreviation);
715720
return null;
716721
}
717722

718-
// Validate translation
719723
if (!AVAILABLE_TRANSLATIONS.includes(translation)) {
720-
console.warn('Invalid translation in URL:', translation);
721-
return null;
722-
}
723-
724-
// Validate book (using the full name)
725-
if (!BOOKS_ABBREVIATED.includes(bookAbbreviation)) {
726-
console.warn('Invalid book in URL:', book);
724+
console.warn('Invalid translation in query:', translation);
727725
return null;
728726
}
729727

730-
// Validate chapter
731728
if (isNaN(chapter) || chapter <= 0 || chapter > 150) {
732-
console.warn('Invalid chapter in URL:', chapter);
729+
console.warn('Invalid chapter in query:', chapter);
733730
return null;
734731
}
735732

0 commit comments

Comments
 (0)