Skip to content

Commit 732d526

Browse files
committed
Fix API URL for GitHub Pages deployment - use absolute URL for production, relative for development
1 parent aff3e91 commit 732d526

File tree

8 files changed

+33
-9
lines changed

8 files changed

+33
-9
lines changed

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@
1111
"build": "vite build",
1212
"preview": "vite preview",
1313
"predeploy": "pnpm run build",
14-
"deploy": "npx gh-pages -d dist"
14+
"deploy": "npx gh-pages -d dist",
15+
"postbuild": "cp dist/index.html dist/404.html"
1516
},
1617
"dependencies": {
1718
"@tanstack/react-query": "^5.85.0",

src/entities/comment/api/CommentAPI.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { CreateComment, CommentList, UpdateComment, LikeComment, Comment } from
33

44
class CommentAPI extends ApiClient {
55
constructor() {
6-
super("/api/comments")
6+
super("/comments")
77
}
88

99
/**

src/entities/post/api/PostAPI.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import { Post, PostItem, CreatePost, UpdatePost, Tag } from "../model/types"
77
*/
88
class PostAPI extends ApiClient {
99
constructor() {
10-
super("/api/posts")
10+
super("/posts")
1111
}
1212

1313
/**

src/entities/user/api/UserAPI.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { User } from "../model/types"
33

44
class UserAPI extends ApiClient {
55
constructor() {
6-
super("/api/users")
6+
super("/users")
77
}
88

99
/**

src/shared/api/api.ts

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
1-
const BASE_URL = import.meta.env.VITE_API_BASE_URL
1+
// GitHub Pages 배포 환경 감지
2+
const isGitHubPages = window.location.hostname === 'amelia-shin.github.io'
3+
4+
// GitHub Pages에서는 전체 URL, 개발환경에서는 상대 경로
5+
const BASE_URL = isGitHubPages ? "https://dummyjson.com" : ""
6+
27
export const api = {
38
get: <T>(path: string, init?: RequestInit) =>
49
fetch(`${BASE_URL}${path}`, { ...init }).then((r) => r.json() as Promise<T>),
@@ -40,11 +45,18 @@ export class ApiClient {
4045
* @returns 전체 URL
4146
*/
4247
private buildUrl(path: string): string {
43-
// basePath가 이미 /api로 시작하면 중복 방지
48+
// GitHub Pages 배포 환경 감지
49+
const isGitHubPages = window.location.hostname === 'amelia-shin.github.io'
50+
51+
// GitHub Pages에서는 전체 URL 사용
52+
if (isGitHubPages) {
53+
return `https://dummyjson.com${this.basePath}${path}`
54+
}
55+
56+
// 개발 환경에서는 상대 경로 사용
4457
if (this.basePath.startsWith("/api")) {
4558
return this.basePath + path
4659
}
47-
// basePath가 비어있거나 /api로 시작하지 않으면 /api 추가
4860
return "/api" + this.basePath + path
4961
}
5062

src/vite-env.d.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
/// <reference types="vite/client" />
2+
3+
interface ImportMetaEnv {
4+
readonly VITE_API_BASE_URL: string
5+
// 필요한 VITE_ 변수들 추가
6+
// readonly VITE_SOME_FLAG: string;
7+
}
8+
9+
interface ImportMeta {
10+
readonly env: ImportMetaEnv
11+
}

tsconfig.app.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,5 +21,5 @@
2121
"noFallthroughCasesInSwitch": true,
2222
"noUncheckedSideEffectImports": true
2323
},
24-
"include": ["src"]
24+
"include": ["src", "src/vite-env.d.ts"]
2525
}

vite.config.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { defineConfig } from "vite"
22
import react from "@vitejs/plugin-react"
33

44
export default defineConfig(({ mode }) => ({
5-
base: mode === "production" ? "/<리포지토리명>/" : "/",
5+
base: mode === "production" ? "/front_6th_chapter2-3/" : "/",
66
plugins: [react()],
77
server: {
88
proxy: {

0 commit comments

Comments
 (0)