Skip to content

Commit 0fbb3b8

Browse files
committed
fix: normalizza il baseUrl in vari componenti per gestire correttamente dev e produzione
1 parent b6388e9 commit 0fbb3b8

5 files changed

Lines changed: 58 additions & 11 deletions

File tree

src/components/HamburgerMenuReact.tsx

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { useState } from 'react';
2+
import { normalizeBaseUrl } from '../lib/paths';
23

34
interface HamburgerMenuReactProps {
45
baseUrl: string;
@@ -7,6 +8,9 @@ interface HamburgerMenuReactProps {
78
export default function HamburgerMenuReact({ baseUrl }: HamburgerMenuReactProps) {
89
const [isOpen, setIsOpen] = useState(false);
910

11+
// Normalizza il baseUrl per gestire correttamente dev e produzione
12+
const normalizedBaseUrl = normalizeBaseUrl(baseUrl);
13+
1014
const toggleMenu = () => {
1115
setIsOpen(!isOpen);
1216
};
@@ -59,7 +63,7 @@ export default function HamburgerMenuReact({ baseUrl }: HamburgerMenuReactProps)
5963
>
6064
<div className="py-2">
6165
<a
62-
href={baseUrl}
66+
href={normalizedBaseUrl || '/'}
6367
className="block px-4 py-2 text-gray-700 hover:bg-gray-100 transition-colors duration-150"
6468
onClick={closeMenu}
6569
>
@@ -72,7 +76,7 @@ export default function HamburgerMenuReact({ baseUrl }: HamburgerMenuReactProps)
7276
</a>
7377

7478
<a
75-
href={`${baseUrl.replace(/\/$/, '')}/tabella`}
79+
href={`${normalizedBaseUrl}/tabella`}
7680
className="block px-4 py-2 text-gray-700 hover:bg-gray-100 transition-colors duration-150"
7781
onClick={closeMenu}
7882
>
@@ -85,7 +89,7 @@ export default function HamburgerMenuReact({ baseUrl }: HamburgerMenuReactProps)
8589
</a>
8690

8791
<a
88-
href={`${baseUrl.replace(/\/$/, '')}/info`}
92+
href={`${normalizedBaseUrl}/info`}
8993
className="block px-4 py-2 text-gray-700 hover:bg-gray-100 transition-colors duration-150"
9094
onClick={closeMenu}
9195
>

src/components/TableView.tsx

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
import { useState, useMemo, useEffect, useCallback } from 'react';
1+
import React, { useState, useMemo, useEffect, useCallback } from 'react';
22
import type { Initiative } from '../types/initiative';
3-
import { formatNumber } from '../lib/initiatives';
3+
import { formatDate, formatNumber, isSigningActive } from '../lib/initiatives';
4+
import { normalizeBaseUrl } from '../lib/paths';
45
import HamburgerMenuReact from './HamburgerMenuReact';
56
import { ArrowLeftIcon, XMarkIcon } from '@heroicons/react/24/outline';
67

@@ -17,6 +18,9 @@ export default function TableView({ initiatives, baseUrl }: TableViewProps) {
1718
const [categoryFilter, setCategoryFilter] = useState('');
1819
const [statusFilter, setStatusFilter] = useState('');
1920
const [typeFilter, setTypeFilter] = useState('');
21+
22+
// Normalizza il baseUrl per gestire correttamente dev e produzione
23+
const normalizedBaseUrl = normalizeBaseUrl(baseUrl);
2024
const [sortColumn, setSortColumn] = useState<SortColumn>('dataApertura');
2125
const [sortDirection, setSortDirection] = useState<SortDirection>('desc');
2226
const [isInitialized, setIsInitialized] = useState(false);
@@ -353,7 +357,7 @@ export default function TableView({ initiatives, baseUrl }: TableViewProps) {
353357
<div className="max-w-7xl mx-auto px-4 sm:px-6 lg:px-8">
354358
<div className="flex justify-between items-center h-16">
355359
<div className="flex items-center">
356-
<a href={baseUrl} className="inline-flex items-center text-blue-600 hover:text-blue-700 font-medium">
360+
<a href={normalizedBaseUrl || '/'} className="inline-flex items-center text-blue-600 hover:text-blue-700 font-medium">
357361
<ArrowLeftIcon className="w-5 h-5 mr-2" />
358362
Torna alle iniziative
359363
</a>
@@ -552,7 +556,7 @@ export default function TableView({ initiatives, baseUrl }: TableViewProps) {
552556
<tr key={initiative.id} className="hover:bg-gray-50">
553557
<td className="px-6 py-4">
554558
<a
555-
href={`${baseUrl.replace(/\/$/, '')}/initiative/${initiative.id}`}
559+
href={`${normalizedBaseUrl}/initiative/${initiative.id}`}
556560
className="text-blue-600 hover:text-blue-800 font-medium"
557561
>
558562
{initiative.titolo}

src/layouts/Layout.astro

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,16 @@ const ogImageUrl = (() => {
2020
const site = Astro.site?.toString() || '';
2121
const baseUrl = import.meta.env.BASE_URL || '/';
2222
const cleanSite = site.replace(/\/$/, '');
23-
// Assicuriamoci che baseUrl finisca sempre con /
24-
const cleanBase = baseUrl.endsWith('/') ? baseUrl : `${baseUrl}/`;
23+
24+
// Gestisci correttamente il baseUrl per dev e produzione
25+
let cleanBase = '';
26+
if (baseUrl !== '/') {
27+
cleanBase = baseUrl.startsWith('/') ? baseUrl : `/${baseUrl}`;
28+
cleanBase = cleanBase.endsWith('/') ? cleanBase : `${cleanBase}/`;
29+
} else {
30+
cleanBase = '/';
31+
}
32+
2533
return `${cleanSite}${cleanBase}og-images/${ogImage}`;
2634
})();
2735
---
@@ -34,7 +42,13 @@ const ogImageUrl = (() => {
3442
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
3543
<link rel="icon" type="image/svg+xml" href={(() => {
3644
const baseUrl = import.meta.env.BASE_URL || '/';
37-
return baseUrl.endsWith('/') ? `${baseUrl}favicon.svg` : `${baseUrl}/favicon.svg`;
45+
// Se è solo '/', non aggiungere nulla prima del favicon.svg
46+
if (baseUrl === '/') {
47+
return '/favicon.svg';
48+
}
49+
// Altrimenti, rimuovi trailing slash e aggiungi favicon
50+
const cleanBase = baseUrl.replace(/\/$/, '');
51+
return `${cleanBase}/favicon.svg`;
3852
})()} />
3953
<title>{title}</title>
4054

src/lib/paths.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,21 @@ export function getBasePath(): string {
77
export function createPath(path: string): string {
88
const basePath = getBasePath();
99
const cleanPath = path.startsWith('/') ? path : `/${path}`;
10+
11+
// Se basePath è vuoto (modalità dev), restituisci solo il path
12+
if (!basePath || basePath === '') {
13+
return cleanPath;
14+
}
15+
1016
return basePath + cleanPath;
1117
}
18+
19+
// Utility per gestire correttamente il baseUrl in modalità dev e produzione
20+
export function normalizeBaseUrl(baseUrl: string): string {
21+
// Se è solo '/', restituisci stringa vuota per evitare doppie slash
22+
if (baseUrl === '/') {
23+
return '';
24+
}
25+
// Rimuovi trailing slash se presente
26+
return baseUrl.replace(/\/$/, '');
27+
}

src/pages/initiative/[id].astro

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,15 @@ const description = String(initiative.descrizioneBreve || initiative.descrizione
4343
const fullTitle = `${title} - Referendum e Iniziative Popolari`;
4444
const signingActive = isSigningActive(initiative);
4545
const ogImage = `og-${initiative.id}.png`;
46+
47+
// Funzione per normalizzare il baseUrl
48+
const normalizeBaseUrl = (baseUrl: string): string => {
49+
if (baseUrl === '/') return '';
50+
return baseUrl.replace(/\/$/, '');
51+
};
52+
53+
const normalizedBaseUrl = normalizeBaseUrl(import.meta.env.BASE_URL || '/');
54+
4655
const currentUrl = (() => {
4756
const site = Astro.site?.toString() || '';
4857
const baseUrl = import.meta.env.BASE_URL || '/';
@@ -85,7 +94,7 @@ const currentUrl = (() => {
8594
<!-- Badges -->
8695
<div class="flex flex-wrap gap-2 sm:gap-3 mb-4 sm:mb-6">
8796
<a
88-
href={`${import.meta.env.BASE_URL.replace(/\/$/, '')}/?categoria=${encodeURIComponent(initiative.idDecCatIniziativa?.nome || '')}`}
97+
href={`${normalizedBaseUrl}/?categoria=${encodeURIComponent(initiative.idDecCatIniziativa?.nome || '')}`}
8998
class="inline-flex items-center px-2 py-1 sm:px-3 sm:py-1 rounded-full text-xs sm:text-sm font-medium bg-blue-100 text-blue-800 hover:bg-blue-200 transition-colors duration-200 cursor-pointer"
9099
>
91100
<TagIcon className="w-3 h-3 sm:w-4 sm:h-4 mr-1" />

0 commit comments

Comments
 (0)