Skip to content

Commit e27a1fa

Browse files
authored
Merge pull request #1853 from dashitongzhi/feature/subdirectory-support
feat(frontend): add PUBLIC_URL support for subdirectory deployment
2 parents c8d11d6 + 146d8f4 commit e27a1fa

3 files changed

Lines changed: 35 additions & 25 deletions

File tree

apps/frontend/src/api_client/api.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@ import { jwtDecode } from "jwt-decode";
33
import { Cookies } from "react-cookie";
44
import { notification } from "../service/notifications";
55

6-
const API_BASE_URL = "/api";
6+
const PUBLIC_URL = import.meta.env.VITE_PUBLIC_URL || import.meta.env.PUBLIC_URL || "";
7+
const API_BASE_URL = PUBLIC_URL + "/api";
78

89
// Custom fetch client with auth and refresh token functionality
910
class FetchClient {
@@ -95,7 +96,7 @@ class FetchClient {
9596
cookies.remove("access");
9697
cookies.remove("refresh");
9798
cookies.remove("jwt");
98-
window.location.href = "/login";
99+
window.location.href = PUBLIC_URL + "/login";
99100
}
100101

101102
// Always show notifications for login attempts (wrong credentials),
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
export const serverAddress = "";
2-
// // This is a dirty hack. Grabs current host for when sharing. URL handling needs cleaned up. DW 12-13-20
3-
export const shareAddress = window.location.host;
1+
export const serverAddress = import.meta.env.VITE_PUBLIC_URL || import.meta.env.PUBLIC_URL || "";
2+
// This is used for sharing. URL handling needs to account for subdirectory.
3+
export const shareAddress = window.location.host + (import.meta.env.VITE_PUBLIC_URL || import.meta.env.PUBLIC_URL || "");

apps/frontend/vite.config.ts

Lines changed: 29 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,34 @@
11
import react from "@vitejs/plugin-react";
2-
import { defineConfig } from "vitest/config";
2+
import { defineConfig, loadEnv } from "vitest/config";
33
import { tanstackRouter } from '@tanstack/router-plugin/vite'
44

5-
export default defineConfig({
6-
base: "/",
7-
plugins: [tanstackRouter({target: "react", autoCodeSplitting: true}), react()],
8-
appType: 'spa',
9-
server: {
10-
host: "0.0.0.0",
11-
port: 3000,
12-
},
13-
test: {
14-
globals: true,
15-
environment: "jsdom",
16-
setupFiles: "./src/setupTests.ts",
17-
css: true,
18-
reporters: ["verbose"],
19-
coverage: {
20-
reporter: ["text", "json", "html"],
21-
include: ["src/**/*"],
22-
exclude: [],
5+
export default defineConfig(({ mode }) => {
6+
const env = loadEnv(mode, process.cwd(), '')
7+
const publicUrl = env.PUBLIC_URL || env.VITE_PUBLIC_URL || '/';
8+
9+
return {
10+
base: publicUrl,
11+
plugins: [tanstackRouter({target: "react", autoCodeSplitting: true}), react()],
12+
appType: 'spa',
13+
server: {
14+
host: "0.0.0.0",
15+
port: 3000,
2316
},
24-
},
17+
build: {
18+
assetsDir: 'assets',
19+
emptyOutDir: true,
20+
},
21+
test: {
22+
globals: true,
23+
environment: "jsdom",
24+
setupFiles: "./src/setupTests.ts",
25+
css: true,
26+
reporters: ["verbose"],
27+
coverage: {
28+
reporter: ["text", "json", "html"],
29+
include: ["src/**/*"],
30+
exclude: [],
31+
},
32+
},
33+
}
2534
});

0 commit comments

Comments
 (0)