Skip to content

Commit 3b75af1

Browse files
committed
fix(web): SSR error
1 parent 662eaab commit 3b75af1

5 files changed

Lines changed: 27 additions & 3 deletions

File tree

apps/web/app/composables/useApi.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,13 @@ interface ApiError {
2828

2929
export const useApi = () => {
3030
const config = useRuntimeConfig()
31-
const baseUrl = config.public.apiBase || 'http://127.0.0.1:5214/api/v1'
31+
// Dual base: SSR (in-container) uses the docker service URL (apiBaseSsr);
32+
// the browser uses the host-port public URL. apiBaseSsr is empty outside
33+
// docker → falls back to public.apiBase.
34+
const baseUrl =
35+
(import.meta.server && config.apiBaseSsr
36+
? (config.apiBaseSsr as string)
37+
: config.public.apiBase) || 'http://127.0.0.1:5214/api/v1'
3238

3339
// `credentials: 'include'` only attaches the session cookie in the BROWSER.
3440
// During SSR (Nuxt server) there is no cookie jar, so an auth-gated

apps/web/app/pages/about/[...slug].vue

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@ interface ApiEnvelope<T> {
99
1010
const route = useRoute()
1111
const config = useRuntimeConfig()
12-
const baseUrl = config.public.apiBase as string
12+
const baseUrl = (import.meta.server && config.apiBaseSsr
13+
? config.apiBaseSsr
14+
: config.public.apiBase) as string
1315
1416
const slugParam = computed(() => {
1517
const raw = route.params.slug

apps/web/app/pages/about/index.vue

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,9 @@ interface ApiEnvelope<T> {
2020
}
2121
2222
const config = useRuntimeConfig()
23-
const baseUrl = config.public.apiBase as string
23+
const baseUrl = (import.meta.server && config.apiBaseSsr
24+
? config.apiBaseSsr
25+
: config.public.apiBase) as string
2426
2527
const { data: response } = await useFetch<ApiEnvelope<KunPostsResponse>>(
2628
`${baseUrl}/about/posts`,

apps/web/nuxt.config.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,13 @@ export default defineNuxtConfig({
9292
},
9393

9494
runtimeConfig: {
95+
// SSR runs inside the docker container, where the Go API is reachable by
96+
// its compose service name (api:5214) — NOT by the browser's host-port URL
97+
// (localhost:15010 is the container's own loopback). The browser can't
98+
// resolve `api`, so it keeps using public.apiBase. Set
99+
// NUXT_API_BASE_SSR=http://api:5214/api/v1 in docker; leave empty for local
100+
// air dev (the dual-base reader falls back to public.apiBase).
101+
apiBaseSsr: process.env.NUXT_API_BASE_SSR || '',
95102
public: {
96103
// 本项目 Go Fiber API(不是 鲲 Galgame OAuth)。Go 端口从 apps/api/.env 的 KUN_SERVER_PORT 读,dev 默认 5214。
97104
apiBase:

docker/go.Dockerfile

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,5 +30,12 @@ RUN CGO_ENABLED=0 GOOS=linux go build -trimpath -ldflags="-s -w" \
3030
# HEALTHCHECK, since distroless has no shell/wget. Ports live in compose.
3131
FROM gcr.io/distroless/static-debian13:nonroot
3232
COPY --from=build /out/app /app
33+
# About-page content: the static .mdx posts that cmd/server reads at runtime
34+
# (internal/about, cfg.About.PostsDir). They live in the WEB app's source tree
35+
# (apps/web/posts) and are NOT DB data, so no migration step carries them —
36+
# bake them into the api image so it is self-contained. Point the server at
37+
# them with KUN_POSTS_DIR=/posts (docker/api.env). The banner images under
38+
# apps/web/public/posts are served separately by the web container.
39+
COPY apps/web/posts /posts
3340
USER nonroot:nonroot
3441
ENTRYPOINT ["/app"]

0 commit comments

Comments
 (0)