Read portal SEO origin from request context

SNI middleware already stores scheme://host; SEOFromRequest
no longer takes a base URL or re-strips the path. Expose
context helpers and drop the obsolete strip tests.

Signed-off-by: Émile Ré <emile@probo.com>
This commit is contained in:
Émile Ré
2026-07-22 12:19:25 +02:00
parent 9ee0df4d3f
commit 52310dbf65
5 changed files with 57 additions and 115 deletions

View File

@@ -26,7 +26,9 @@ import (
"slices"
"strings"
"go.gearno.de/x/ref"
"go.probo.inc/probo/pkg/iam"
"go.probo.inc/probo/pkg/server/api/complianceportal"
)
const defaultCompliancePortalLocale = "en"
@@ -34,11 +36,11 @@ const defaultCompliancePortalLocale = "en"
// SEOFromRequest derives html lang, a self-referencing canonical URL, and
// hreflang alternates (including x-default → English) for the SPA shell.
// Portals are host-routed (slug subdomain / custom domain); the request path
// is already relative to the portal root. pageBaseURL is normalized to its
// origin so a stale path in the base cannot double the route. When pageBaseURL
// has no usable origin, canonical and hreflang are left empty so callers do
// not emit relative SEO links.
func SEOFromRequest(r *http.Request, pageBaseURL string) (htmlLang, canonical string, hreflang []HreflangLink) {
// is already relative to the portal root. The portal origin comes from
// request context (set by SNI middleware as scheme://host). When missing,
// canonical and hreflang are left empty so callers do not emit relative SEO
// links.
func SEOFromRequest(r *http.Request) (htmlLang, canonical string, hreflang []HreflangLink) {
pathname := r.URL.Path
if pathname == "" {
pathname = "/"
@@ -47,10 +49,8 @@ func SEOFromRequest(r *http.Request, pageBaseURL string) (htmlLang, canonical st
locale, rest := splitLocaleFromAppPath(pathname)
htmlLang = locale
origin := portalOrigin(pageBaseURL)
origin := ref.UnrefOrZero(complianceportal.CompliancePortalBaseURLFromContext(r.Context()))
if origin == "" {
// No absolute origin — return lang only; callers must not emit
// relative canonical/hreflang URLs.
return htmlLang, "", nil
}
@@ -74,18 +74,6 @@ func SEOFromRequest(r *http.Request, pageBaseURL string) (htmlLang, canonical st
return htmlLang, canonical, hreflang
}
// portalOrigin returns scheme://host from pageBaseURL, dropping any path,
// query, or fragment. Callers may pass a full request URL by mistake.
// Relative or malformed values yield "" so SEO links are omitted.
func portalOrigin(pageBaseURL string) string {
parsed, err := url.Parse(pageBaseURL)
if err != nil || parsed.Scheme == "" || parsed.Host == "" {
return ""
}
return (&url.URL{Scheme: parsed.Scheme, Host: parsed.Host}).String()
}
func splitLocaleFromAppPath(appPath string) (locale, rest string) {
segments := strings.Split(strings.Trim(appPath, "/"), "/")
if len(segments) == 0 || segments[0] == "" {