Selaa lähdekoodia

fix: 隐藏政策路径修复

王晓东 2 kuukautta sitten
vanhempi
commit
d147f2d4e4
4 muutettua tiedostoa jossa 19 lisäystä ja 9 poistoa
  1. 2 0
      .env
  2. 0 1
      src/components/Header.tsx
  3. 16 7
      src/contexts/RouterContext.tsx
  4. 1 1
      vite.config.ts

+ 2 - 0
.env

@@ -0,0 +1,2 @@
+# 基础根路径
+VITE_BASE_PATH = /fara/h5/

+ 0 - 1
src/components/Header.tsx

@@ -18,7 +18,6 @@ export function Header() {
   const scrollToSection = (id: string) => {
     // Close mobile menu first for better UX
     setMobileMenuOpen(false);
-    console.log(id,3333)
     if (currentPage !== 'home') {
       navigateTo('home', id);
     } else {

+ 16 - 7
src/contexts/RouterContext.tsx

@@ -1,5 +1,5 @@
 import React, { createContext, useContext, useState, ReactNode, useEffect } from 'react';
-
+const VITE_BASE_PATH = import.meta.env.VITE_BASE_PATH
 type PageType = 'home' | 'about' | 'blog' | 'careers' | 'privacy' | 'terms' | 'faq' | 'contact';
 
 interface RouterContextType {
@@ -61,10 +61,20 @@ function scrollToElement(elementId: string, initialDelay: number = 150) {
   }, initialDelay);
 }
 
+function extractPath(url: string): string | null {
+  const prefix = VITE_BASE_PATH;
+  if (url.startsWith(prefix)) {
+    return url.slice(prefix.length);
+  }
+  return null;
+}
+
 export function RouterProvider({ children }: { children: ReactNode }) {
   // Initialize page based on current URL path
   const getInitialPage = (): PageType => {
-    const path = window.location.pathname;
+    const result = extractPath(window.location.pathname);
+    const path = result ?? '';
+    console.log(path)
     return pathToPage[path] || 'home';
   };
 
@@ -73,21 +83,21 @@ export function RouterProvider({ children }: { children: ReactNode }) {
   // Handle initial page load
   useEffect(() => {
     const pathname = window.location.pathname;
-    
     // Ensure pathname is normalized
     if (pathname === '' || pathname === null) {
       window.history.replaceState({ page: 'home' }, '', '/');
+    }else{
+      
     }
   }, []);
 
   const navigateTo = (page: PageType, hash?: string) => {
     setCurrentPage(page);
-    
     const path = pageToPath[page];
     const url = hash ? `${path}#${hash}` : path;
-    
+    const BASE_PATH = VITE_BASE_PATH
     // Update browser URL without reloading
-    window.history.pushState({ page }, '', url);
+    window.history.pushState({ page }, '', BASE_PATH + url);
     
     if (hash) {
       // If there's a hash, scroll to the section
@@ -103,7 +113,6 @@ export function RouterProvider({ children }: { children: ReactNode }) {
       const path = window.location.pathname;
       const page = pathToPage[path] || 'home';
       setCurrentPage(page);
-      
       // Handle hash if present
       const hash = window.location.hash.replace('#', '');
       if (hash) {

+ 1 - 1
vite.config.ts

@@ -53,7 +53,7 @@
       target: 'esnext',
       outDir: 'build',
     },
-    base: '/fara/h5/',
+    base: '/fara/h5',
     server: {
       port: 3000,
       open: true,