@deepsel/cms-utils
Advanced tools
| /** | ||
| * Extract the hostname from the current request context. | ||
| * | ||
| * Server-side (Astro SSR): reads the Host header (or X-Original-Host / X-Forwarded-Host) | ||
| * from the incoming request. This is necessary because Astro's Node adapter rewrites | ||
| * request.url to localhost:4321, losing the original domain. | ||
| * | ||
| * Client-side: reads window.location.hostname. | ||
| */ | ||
| export declare function getHostname(astroRequest?: Request | null): string | null; |
| /** | ||
| * Extract the hostname from the current request context. | ||
| * | ||
| * Server-side (Astro SSR): reads the Host header (or X-Original-Host / X-Forwarded-Host) | ||
| * from the incoming request. This is necessary because Astro's Node adapter rewrites | ||
| * request.url to localhost:4321, losing the original domain. | ||
| * | ||
| * Client-side: reads window.location.hostname. | ||
| */ | ||
| export function getHostname(astroRequest) { | ||
| if (astroRequest) { | ||
| // Prefer explicit forwarding headers set by nginx/proxy | ||
| const xOriginalHost = astroRequest.headers.get('x-original-host'); | ||
| if (xOriginalHost) { | ||
| return xOriginalHost.split(':')[0]; | ||
| } | ||
| const xForwardedHost = astroRequest.headers.get('x-forwarded-host'); | ||
| if (xForwardedHost) { | ||
| return xForwardedHost.split(':')[0]; | ||
| } | ||
| // Fall back to Host header (set by nginx: proxy_set_header Host $host) | ||
| const host = astroRequest.headers.get('host'); | ||
| if (host) { | ||
| return host.split(':')[0]; | ||
| } | ||
| // Last resort: parse the URL (may be localhost in production) | ||
| try { | ||
| return new URL(astroRequest.url).hostname; | ||
| } | ||
| catch { | ||
| return null; | ||
| } | ||
| } | ||
| // Client-side | ||
| if (typeof window !== 'undefined') { | ||
| return window.location.hostname; | ||
| } | ||
| return null; | ||
| } |
| import { getDefaultBackendHost } from '../common/utils/getDefaultBackendHost.js'; | ||
| import { getHostname } from '../common/utils/getHostname.js'; | ||
| /** | ||
@@ -23,10 +24,3 @@ * Fetches blog list from the backend by language | ||
| }; | ||
| let hostname = null; | ||
| if (astroRequest) { | ||
| const url = new URL(astroRequest.url); | ||
| hostname = url.hostname; | ||
| } | ||
| else if (typeof window !== 'undefined') { | ||
| hostname = window.location.hostname; | ||
| } | ||
| const hostname = getHostname(astroRequest); | ||
| if (hostname) { | ||
@@ -33,0 +27,0 @@ fetchOptions.headers['X-Original-Host'] = hostname; |
| import { fetchPublicSettings } from '../page/index.js'; | ||
| import { getDefaultBackendHost } from '../common/utils/getDefaultBackendHost.js'; | ||
| import { getHostname } from '../common/utils/getHostname.js'; | ||
| /** | ||
@@ -22,10 +23,3 @@ * Fetches a single blog post from the backend by language and path | ||
| }; | ||
| let hostname = null; | ||
| if (astroRequest) { | ||
| const url = new URL(astroRequest.url); | ||
| hostname = url.hostname; | ||
| } | ||
| else if (typeof window !== 'undefined') { | ||
| hostname = window.location.hostname; | ||
| } | ||
| const hostname = getHostname(astroRequest); | ||
| if (hostname) { | ||
@@ -32,0 +26,0 @@ fetchOptions.headers['X-Original-Host'] = hostname; |
| export * from './cookieUtils.js'; | ||
| export * from './getDefaultBackendHost.js'; | ||
| export * from './getHostname.js'; | ||
| export * from './isObjectOrArray.js'; | ||
@@ -4,0 +5,0 @@ /** |
| export * from './cookieUtils.js'; | ||
| export * from './getDefaultBackendHost.js'; | ||
| export * from './getHostname.js'; | ||
| export * from './isObjectOrArray.js'; | ||
@@ -4,0 +5,0 @@ /** |
| import { fetchPublicSettings } from './fetchPublicSettings.js'; | ||
| import { getDefaultBackendHost } from '../common/utils/getDefaultBackendHost.js'; | ||
| import { getHostname } from '../common/utils/getHostname.js'; | ||
| /** | ||
@@ -31,17 +32,7 @@ * Fetches page data from the backend by language and slug | ||
| }; | ||
| // Send the current hostname to the backend for proper domain detection | ||
| let hostname = null; | ||
| // Server-side: Extract hostname from Astro request | ||
| if (astroRequest) { | ||
| const requestUrl = new URL(astroRequest.url); | ||
| hostname = requestUrl.hostname; | ||
| } | ||
| // Client-side: Extract hostname from window | ||
| else if (typeof window !== 'undefined') { | ||
| hostname = window.location.hostname; | ||
| } | ||
| // Send the current hostname to the backend for domain-based org detection | ||
| const hostname = getHostname(astroRequest); | ||
| if (hostname) { | ||
| fetchOptions.headers['X-Original-Host'] = hostname; | ||
| fetchOptions.headers['X-Frontend-Host'] = hostname; | ||
| // Note: Cannot override Host header due to browser security restrictions | ||
| } | ||
@@ -48,0 +39,0 @@ // Add authentication headers if token exists (for both preview and protected content) |
| import { getDefaultBackendHost } from '../common/utils/getDefaultBackendHost.js'; | ||
| import { getHostname } from '../common/utils/getHostname.js'; | ||
| /** | ||
@@ -34,16 +35,6 @@ * Fetches public settings from the backend | ||
| if (orgId === null) { | ||
| let hostname = null; | ||
| // Server-side: Extract hostname from Astro request | ||
| if (astroRequest) { | ||
| const url = new URL(astroRequest.url); | ||
| hostname = url.hostname; | ||
| } | ||
| // Client-side: Extract hostname from window | ||
| else if (typeof window !== 'undefined') { | ||
| hostname = window.location.hostname; | ||
| } | ||
| const hostname = getHostname(astroRequest); | ||
| if (hostname) { | ||
| headers['X-Original-Host'] = hostname; | ||
| headers['X-Frontend-Host'] = hostname; | ||
| // Note: Cannot override Host header due to browser security restrictions | ||
| } | ||
@@ -50,0 +41,0 @@ } |
| import { fetchPublicSettings } from './fetchPublicSettings.js'; | ||
| import { getDefaultBackendHost } from '../common/utils/getDefaultBackendHost.js'; | ||
| import { getHostname } from '../common/utils/getHostname.js'; | ||
| /** | ||
@@ -9,10 +10,3 @@ * Fetches search results from the backend. | ||
| // Build hostname for headers | ||
| let hostname = null; | ||
| if (astroRequest) { | ||
| const requestUrl = new URL(astroRequest.url); | ||
| hostname = requestUrl.hostname; | ||
| } | ||
| else if (typeof window !== 'undefined') { | ||
| hostname = window.location.hostname; | ||
| } | ||
| const hostname = getHostname(astroRequest); | ||
| const headers = { | ||
@@ -19,0 +13,0 @@ 'Content-Type': 'application/json', |
+1
-1
| { | ||
| "name": "@deepsel/cms-utils", | ||
| "version": "1.9.3", | ||
| "version": "1.9.4", | ||
| "description": "Helper utilities for Deepsel CMS", | ||
@@ -5,0 +5,0 @@ "repository": { |
51839
1.33%66
3.13%1457
1.04%