Skip to content

Commit 2fd50ca

Browse files
author
Minseo Kim
committed
Fix asset base path
1 parent 31c299e commit 2fd50ca

6 files changed

Lines changed: 42 additions & 26 deletions

File tree

src/layouts/RootLayout/Header/Logo.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@ import Link from "next/link"
22
import Image from "next/image"
33
import { CONFIG } from "site.config"
44
import styled from "@emotion/styled"
5+
import { withBasePath } from "src/libs/utils/assetPath"
56

67
const Logo = () => {
7-
const basePath = process.env.NEXT_PUBLIC_BASE_PATH || ""
8-
const logoSrc = `${basePath}/images/shared/Microsoft-Logo-PNG-Photos.png`
8+
const logoSrc = withBasePath("/images/shared/Microsoft-Logo-PNG-Photos.png")
99

1010
return (
1111
<StyledWrapper href="/" aria-label={CONFIG.blog.title}>

src/libs/utils/assetPath.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import { CONFIG } from "site.config"
2+
3+
const resolveBasePath = () => {
4+
const envBasePath = process.env.NEXT_PUBLIC_BASE_PATH || ""
5+
if (envBasePath) return envBasePath
6+
7+
if (process.env.NODE_ENV === "production") {
8+
try {
9+
const url = new URL(CONFIG.link)
10+
return url.pathname.replace(/\/$/, "")
11+
} catch {
12+
return ""
13+
}
14+
}
15+
16+
return ""
17+
}
18+
19+
export const withBasePath = (url: string) => {
20+
if (!url) return url
21+
if (url.startsWith("http") || url.startsWith("data:") || url.startsWith("#")) {
22+
return url
23+
}
24+
25+
const basePath = resolveBasePath()
26+
if (url.startsWith("/")) return `${basePath}${url}`
27+
return url
28+
}

src/pages/_document.tsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
11
import Document, { Html, Head, Main, NextScript } from "next/document"
22
import { CONFIG } from "site.config"
3+
import { withBasePath } from "src/libs/utils/assetPath"
34

45
class MyDocument extends Document {
56
render() {
67
return (
78
<Html lang={CONFIG.lang}>
89
<Head>
9-
<link rel="icon" href="/favicon.ico" />
10+
<link rel="icon" href={withBasePath("/favicon.ico")} />
1011
<link
1112
rel="apple-touch-icon"
1213
sizes="192x192"
13-
href="/apple-touch-icon.png"
14+
href={withBasePath("/apple-touch-icon.png")}
1415
></link>
1516
<link
1617
rel="alternate"

src/routes/Detail/PostDetail/PostHeader.tsx

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,13 @@ import { formatDate } from "src/libs/utils"
55
import Image from "next/image"
66
import React from "react"
77
import styled from "@emotion/styled"
8+
import { withBasePath } from "src/libs/utils/assetPath"
89

910
type Props = {
1011
data: TPost
1112
}
1213

1314
const PostHeader: React.FC<Props> = ({ data }) => {
14-
const basePath = process.env.NEXT_PUBLIC_BASE_PATH || ""
15-
const withBasePath = (url?: string) => {
16-
if (!url) return url
17-
if (url.startsWith("http") || url.startsWith("data:")) return url
18-
if (url.startsWith("/")) return `${basePath}${url}`
19-
return url
20-
}
21-
2215
return (
2316
<StyledWrapper>
2417
<h1 className="title">{data.title}</h1>
@@ -30,11 +23,9 @@ const PostHeader: React.FC<Props> = ({ data }) => {
3023
<div className="author">
3124
<Image
3225
css={{ borderRadius: "50%" }}
33-
src={
34-
withBasePath(
35-
data.author[0].profile_photo || CONFIG.profile.image
36-
) || ""
37-
}
26+
src={withBasePath(
27+
data.author[0].profile_photo || CONFIG.profile.image
28+
)}
3829
alt="profile_photo"
3930
width={24}
4031
height={24}
@@ -63,7 +54,7 @@ const PostHeader: React.FC<Props> = ({ data }) => {
6354
{data.thumbnail && (
6455
<div className="thumbnail">
6556
<Image
66-
src={withBasePath(data.thumbnail) || ""}
57+
src={withBasePath(data.thumbnail)}
6758
css={{ objectFit: "cover" }}
6859
fill
6960
alt={data.title}

src/routes/Feed/MobileProfileCard.tsx

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,14 @@ import { CONFIG } from "site.config"
22
import Image from "next/image"
33
import React from "react"
44
import styled from "@emotion/styled"
5+
import { withBasePath } from "src/libs/utils/assetPath"
56

67
type Props = {
78
className?: string
89
}
910

1011
const MobileProfileCard: React.FC<Props> = () => {
11-
const basePath = process.env.NEXT_PUBLIC_BASE_PATH || ""
12-
const profileSrc = CONFIG.profile.image.startsWith("/")
13-
? `${basePath}${CONFIG.profile.image}`
14-
: CONFIG.profile.image
12+
const profileSrc = withBasePath(CONFIG.profile.image)
1513

1614
return (
1715
<StyledWrapper>

src/routes/Feed/ProfileCard.tsx

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,12 @@ import Image from "next/image"
33
import React from "react"
44
import { CONFIG } from "site.config"
55
import { Emoji } from "src/components/Emoji"
6+
import { withBasePath } from "src/libs/utils/assetPath"
67

78
type Props = {}
89

910
const ProfileCard: React.FC<Props> = () => {
10-
const basePath = process.env.NEXT_PUBLIC_BASE_PATH || ""
11-
const profileSrc = CONFIG.profile.image.startsWith("/")
12-
? `${basePath}${CONFIG.profile.image}`
13-
: CONFIG.profile.image
11+
const profileSrc = withBasePath(CONFIG.profile.image)
1412

1513
return (
1614
<StyledWrapper>

0 commit comments

Comments
 (0)