最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

node.js - useRuntimeConfig().public.apiUrl becomes undefined on client-side navigation (Dockerized setup) - Stack Overflow

programmeradmin2浏览0评论

I’m running a Nuxt 3 app in a Docker container, and I’m having an issue where useRuntimeConfig().public.apiUrl is undefined when navigating from an SSR page to a non-SSR page. However, it works correctly on a hard refresh.

Works fine locally in dev mode (bun dev) and even when I run nuxt build on my Mac.

Breaks in production inside Docker.

Docker setup:

services:
  frontend:
    build:
      context: .
      dockerfile: Dockerfile
    ports:
      - "3000:3000"
    environment:
      - HOST=0.0.0.0
      - NUXT_PUBLIC_API_URL=/api

Nuxt Config

export default defineNuxtConfig({
  runtimeConfig: {
    public: {
      apiUrl: process.env.NUXT_PUBLIC_API_URL,
    },
  },
  routeRules: {
    "*/map": { ssr: false },
  },
});

Api service(composable)

import axios from "axios";
import { useRuntimeConfig } from "#app";

export function useApiService() {
  const config = useRuntimeConfig();

  return axios.create({
    baseURL: config.public.apiUrl,
    headers: { "Content-Type": "application/json" },
    withCredentials: true,
  });
}

The problem

  • Navigating from an SSR page (index.vue) to a non-SSR page (map.vue) causes useRuntimeConfig().public.apiUrl to become undefined.

  • However, if I hard refresh the non-SSR page (map.vue), it works correctly.

Has anyone faced this issue before? How do I ensure that NUXT_PUBLIC_API_URL persists during client-side navigation?

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论