Backend-Adresse zur Laufzeit statt fest im Image

Astro setzt PUBLIC_-Variablen beim Bauen ein, deshalb gehörte bisher
jedes Frontend-Image zu genau einer Umgebung: Heimnetz, Testsystem und
Produktion brauchten je einen eigenen Build.

Jetzt liest der Server die Adresse beim Start aus der Umgebung und gibt
sie als <meta> an den Browser weiter. MEDUSA_INTERNAL_URL erlaubt dem
Frontend-Container zusätzlich den direkten Weg zum Backend, ohne Umweg
über den Reverse Proxy.

Ein Image passt damit überall, ein Adresswechsel ist eine Variable.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
This commit is contained in:
torbinus
2026-09-18 10:22:53 +02:00
parent d227223a74
commit e092057dbe
6 changed files with 63 additions and 15 deletions
+11
View File
@@ -39,6 +39,14 @@ const isLoggedIn = Boolean(Astro.cookies.get("customer_jwt")?.value);
// stable section path so both domains consolidate onto one indexable URL.
const canonicalPath = pathname === "/" ? layers[layer].homeHref : pathname;
const canonicalUrl = new URL(canonicalPath, PRIMARY_DOMAIN).toString();
// Die öffentliche Adresse des Backends — die, die der Browser erreichen muss.
// Sie wird zur Laufzeit gelesen und unten als <meta> mitgegeben, damit ein
// Image für jede Umgebung passt. MEDUSA_INTERNAL_URL gilt nur serverseitig.
const medusaPublicUrl =
process.env.PUBLIC_MEDUSA_BACKEND_URL ?? import.meta.env.PUBLIC_MEDUSA_BACKEND_URL ?? "";
const medusaPublishableKey =
process.env.PUBLIC_MEDUSA_PUBLISHABLE_KEY ?? import.meta.env.PUBLIC_MEDUSA_PUBLISHABLE_KEY ?? "";
---
<html lang="de" data-layer={layer} data-hero={heroTone}>
@@ -48,6 +56,9 @@ const canonicalUrl = new URL(canonicalPath, PRIMARY_DOMAIN).toString();
<link rel="icon" type="image/svg+xml" href="/favicon.svg" />
<title>{title} · {COMPANY_NAME}</title>
<meta name="description" content={description} />
{/* Woher der Browser seine Daten holt. Siehe medusaConfig() in lib/medusa.ts. */}
<meta name="medusa-backend-url" content={medusaPublicUrl} />
<meta name="medusa-publishable-key" content={medusaPublishableKey} />
<link rel="canonical" href={canonicalUrl} />
<meta property="og:type" content="website" />
+31 -2
View File
@@ -1,9 +1,38 @@
import { varietyName } from "./varieties";
const BACKEND_URL = import.meta.env.PUBLIC_MEDUSA_BACKEND_URL;
const PUBLISHABLE_KEY = import.meta.env.PUBLIC_MEDUSA_PUBLISHABLE_KEY;
/**
* Adresse und Schlüssel des Backends stehen bewusst nicht mehr fest im Build.
* Sonst gehört jedes Image zu genau einer Umgebung, und ein Adresswechsel
* heißt neu bauen, hochladen, neu ausrollen.
*
* Auf dem Server kommen die Werte aus der Umgebung, im Browser aus zwei
* <meta>-Tags, die das Layout mitschickt. Damit passt ein Image für Heimnetz,
* Testsystem und Produktion.
*/
function meta(name: string): string | undefined {
return document.querySelector<HTMLMetaElement>(`meta[name="${name}"]`)?.content || undefined;
}
export function medusaConfig(): { url: string; key: string } {
if (typeof document !== "undefined") {
return {
url: meta("medusa-backend-url") ?? "",
key: meta("medusa-publishable-key") ?? "",
};
}
const env = typeof process !== "undefined" ? process.env : ({} as Record<string, string | undefined>);
return {
// Innerhalb des Containers geht es direkt zum Backend, ohne den Umweg
// über den Reverse Proxy. Fehlt der Wert, gilt die öffentliche Adresse.
url: env.MEDUSA_INTERNAL_URL || env.PUBLIC_MEDUSA_BACKEND_URL || import.meta.env.PUBLIC_MEDUSA_BACKEND_URL || "",
key: env.PUBLIC_MEDUSA_PUBLISHABLE_KEY || import.meta.env.PUBLIC_MEDUSA_PUBLISHABLE_KEY || "",
};
}
export async function medusaFetch(path: string, init?: RequestInit) {
const { url: BACKEND_URL, key: PUBLISHABLE_KEY } = medusaConfig();
const res = await fetch(`${BACKEND_URL}${path}`, {
...init,
headers: {
+2 -2
View File
@@ -58,8 +58,8 @@ import Layout from "../../layouts/Layout.astro";
// The invoice route needs the publishable API key header, which a
// plain link click can't send — fetch it ourselves and open the
// resulting PDF as a blob instead.
const backendUrl = import.meta.env.PUBLIC_MEDUSA_BACKEND_URL;
const key = import.meta.env.PUBLIC_MEDUSA_PUBLISHABLE_KEY;
const backendUrl = document.querySelector('meta[name="medusa-backend-url"]').content;
const key = document.querySelector('meta[name="medusa-publishable-key"]').content;
const res = await fetch(`${backendUrl}/store/orders/${order.id}/invoice`, {
headers: { "x-publishable-api-key": key },
});
@@ -185,8 +185,8 @@ const allLabels = order.fulfillments.flatMap((f) => f.labels);
const displayId = invoiceButton.dataset.displayId;
invoiceButton.disabled = true;
try {
const backendUrl = import.meta.env.PUBLIC_MEDUSA_BACKEND_URL;
const key = import.meta.env.PUBLIC_MEDUSA_PUBLISHABLE_KEY;
const backendUrl = document.querySelector('meta[name="medusa-backend-url"]').content;
const key = document.querySelector('meta[name="medusa-publishable-key"]').content;
const res = await fetch(`${backendUrl}/store/orders/${orderId}/invoice`, {
headers: { "x-publishable-api-key": key },
});