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:
@@ -38,7 +38,17 @@ func CompliancePortalFromContext(ctx context.Context) *coredata.CompliancePortal
|
|||||||
return page
|
return page
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func ContextWithCompliancePortal(ctx context.Context, page *coredata.CompliancePortal) context.Context {
|
||||||
|
return context.WithValue(ctx, compliancePortalKey, page)
|
||||||
|
}
|
||||||
|
|
||||||
func CompliancePortalBaseURLFromContext(ctx context.Context) *string {
|
func CompliancePortalBaseURLFromContext(ctx context.Context) *string {
|
||||||
page, _ := ctx.Value(compliancePortalBaseURLKey).(*string)
|
page, _ := ctx.Value(compliancePortalBaseURLKey).(*string)
|
||||||
return page
|
return page
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ContextWithCompliancePortalBaseURL stores the portal origin (scheme://host).
|
||||||
|
// Callers must not include a path — consumers append their own routes.
|
||||||
|
func ContextWithCompliancePortalBaseURL(ctx context.Context, baseURL string) context.Context {
|
||||||
|
return context.WithValue(ctx, compliancePortalBaseURLKey, &baseURL)
|
||||||
|
}
|
||||||
|
|||||||
@@ -21,7 +21,6 @@
|
|||||||
package complianceportal
|
package complianceportal
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
|
||||||
"errors"
|
"errors"
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/url"
|
"net/url"
|
||||||
@@ -115,21 +114,15 @@ func NewSNIMiddleware(visitorSvc *visitor.Service) func(next http.Handler) http.
|
|||||||
// Origin only — consumers append their own paths (SEO, sitemap,
|
// Origin only — consumers append their own paths (SEO, sitemap,
|
||||||
// robots, brand assets, OAuth). Including r.URL.Path here would
|
// robots, brand assets, OAuth). Including r.URL.Path here would
|
||||||
// duplicate the route (e.g. /fr/documents/fr/documents).
|
// duplicate the route (e.g. /fr/documents/fr/documents).
|
||||||
baseURL := &url.URL{
|
baseURL := (&url.URL{
|
||||||
Host: r.Host,
|
Host: r.Host,
|
||||||
Scheme: "https",
|
Scheme: "https",
|
||||||
}
|
}).String()
|
||||||
baseURLString := baseURL.String()
|
ctx = ContextWithCompliancePortalBaseURL(ctx, baseURL)
|
||||||
|
|
||||||
ctx = context.WithValue(
|
|
||||||
ctx,
|
|
||||||
compliancePortalBaseURLKey,
|
|
||||||
&baseURLString,
|
|
||||||
)
|
|
||||||
r = r.WithContext(ctx)
|
r = r.WithContext(ctx)
|
||||||
|
|
||||||
if compliancePage.Active {
|
if compliancePage.Active {
|
||||||
ctx = context.WithValue(ctx, compliancePortalKey, compliancePage)
|
ctx = ContextWithCompliancePortal(ctx, compliancePage)
|
||||||
next.ServeHTTP(w, r.WithContext(ctx))
|
next.ServeHTTP(w, r.WithContext(ctx))
|
||||||
|
|
||||||
return
|
return
|
||||||
|
|||||||
@@ -20,7 +20,6 @@ import (
|
|||||||
|
|
||||||
"github.com/go-chi/chi/v5"
|
"github.com/go-chi/chi/v5"
|
||||||
"go.gearno.de/kit/log"
|
"go.gearno.de/kit/log"
|
||||||
"go.gearno.de/x/ref"
|
|
||||||
"go.probo.inc/probo/pkg/baseurl"
|
"go.probo.inc/probo/pkg/baseurl"
|
||||||
visitor "go.probo.inc/probo/pkg/complianceportal/visitor"
|
visitor "go.probo.inc/probo/pkg/complianceportal/visitor"
|
||||||
"go.probo.inc/probo/pkg/esign"
|
"go.probo.inc/probo/pkg/esign"
|
||||||
@@ -140,19 +139,15 @@ func compliancePageHeadData() HeadDataFunc {
|
|||||||
description = *tc.Description
|
description = *tc.Description
|
||||||
}
|
}
|
||||||
|
|
||||||
|
htmlLang, canonical, hreflang := SEOFromRequest(r)
|
||||||
|
|
||||||
headData := HeadData{
|
headData := HeadData{
|
||||||
Title: tc.Title,
|
Title: tc.Title,
|
||||||
Description: description,
|
Description: description,
|
||||||
}
|
HTMLLang: htmlLang,
|
||||||
|
OGURL: canonical,
|
||||||
// Canonical / hreflang / og:url must be absolute. Without a portal
|
CanonicalURL: canonical,
|
||||||
// origin in context, skip them rather than emit relative paths.
|
Hreflang: hreflang,
|
||||||
if pageBase := ref.UnrefOrZero(compliancePageBaseURL); pageBase != "" {
|
|
||||||
htmlLang, canonical, hreflang := SEOFromRequest(r, pageBase)
|
|
||||||
headData.HTMLLang = htmlLang
|
|
||||||
headData.OGURL = canonical
|
|
||||||
headData.CanonicalURL = canonical
|
|
||||||
headData.Hreflang = hreflang
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if tc.LogoFileID != nil && compliancePageBaseURL != nil {
|
if tc.LogoFileID != nil && compliancePageBaseURL != nil {
|
||||||
|
|||||||
@@ -26,7 +26,9 @@ import (
|
|||||||
"slices"
|
"slices"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
|
"go.gearno.de/x/ref"
|
||||||
"go.probo.inc/probo/pkg/iam"
|
"go.probo.inc/probo/pkg/iam"
|
||||||
|
"go.probo.inc/probo/pkg/server/api/complianceportal"
|
||||||
)
|
)
|
||||||
|
|
||||||
const defaultCompliancePortalLocale = "en"
|
const defaultCompliancePortalLocale = "en"
|
||||||
@@ -34,11 +36,11 @@ const defaultCompliancePortalLocale = "en"
|
|||||||
// SEOFromRequest derives html lang, a self-referencing canonical URL, and
|
// SEOFromRequest derives html lang, a self-referencing canonical URL, and
|
||||||
// hreflang alternates (including x-default → English) for the SPA shell.
|
// hreflang alternates (including x-default → English) for the SPA shell.
|
||||||
// Portals are host-routed (slug subdomain / custom domain); the request path
|
// Portals are host-routed (slug subdomain / custom domain); the request path
|
||||||
// is already relative to the portal root. pageBaseURL is normalized to its
|
// is already relative to the portal root. The portal origin comes from
|
||||||
// origin so a stale path in the base cannot double the route. When pageBaseURL
|
// request context (set by SNI middleware as scheme://host). When missing,
|
||||||
// has no usable origin, canonical and hreflang are left empty so callers do
|
// canonical and hreflang are left empty so callers do not emit relative SEO
|
||||||
// not emit relative SEO links.
|
// links.
|
||||||
func SEOFromRequest(r *http.Request, pageBaseURL string) (htmlLang, canonical string, hreflang []HreflangLink) {
|
func SEOFromRequest(r *http.Request) (htmlLang, canonical string, hreflang []HreflangLink) {
|
||||||
pathname := r.URL.Path
|
pathname := r.URL.Path
|
||||||
if pathname == "" {
|
if pathname == "" {
|
||||||
pathname = "/"
|
pathname = "/"
|
||||||
@@ -47,10 +49,8 @@ func SEOFromRequest(r *http.Request, pageBaseURL string) (htmlLang, canonical st
|
|||||||
locale, rest := splitLocaleFromAppPath(pathname)
|
locale, rest := splitLocaleFromAppPath(pathname)
|
||||||
htmlLang = locale
|
htmlLang = locale
|
||||||
|
|
||||||
origin := portalOrigin(pageBaseURL)
|
origin := ref.UnrefOrZero(complianceportal.CompliancePortalBaseURLFromContext(r.Context()))
|
||||||
if origin == "" {
|
if origin == "" {
|
||||||
// No absolute origin — return lang only; callers must not emit
|
|
||||||
// relative canonical/hreflang URLs.
|
|
||||||
return htmlLang, "", nil
|
return htmlLang, "", nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -74,18 +74,6 @@ func SEOFromRequest(r *http.Request, pageBaseURL string) (htmlLang, canonical st
|
|||||||
return htmlLang, canonical, hreflang
|
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) {
|
func splitLocaleFromAppPath(appPath string) (locale, rest string) {
|
||||||
segments := strings.Split(strings.Trim(appPath, "/"), "/")
|
segments := strings.Split(strings.Trim(appPath, "/"), "/")
|
||||||
if len(segments) == 0 || segments[0] == "" {
|
if len(segments) == 0 || segments[0] == "" {
|
||||||
|
|||||||
@@ -26,23 +26,35 @@ import (
|
|||||||
|
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
"github.com/stretchr/testify/require"
|
"github.com/stretchr/testify/require"
|
||||||
|
"go.probo.inc/probo/pkg/server/api/complianceportal"
|
||||||
complianceportal_v1 "go.probo.inc/probo/pkg/server/api/complianceportal/v1"
|
complianceportal_v1 "go.probo.inc/probo/pkg/server/api/complianceportal/v1"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
func requestWithPortalOrigin(t *testing.T, rawURL, origin string) *http.Request {
|
||||||
|
t.Helper()
|
||||||
|
|
||||||
|
req, err := http.NewRequest(http.MethodGet, rawURL, nil)
|
||||||
|
require.NoError(t, err)
|
||||||
|
|
||||||
|
if origin != "" {
|
||||||
|
req = req.WithContext(
|
||||||
|
complianceportal.ContextWithCompliancePortalBaseURL(req.Context(), origin),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
return req
|
||||||
|
}
|
||||||
|
|
||||||
func TestSEOFromRequest(t *testing.T) {
|
func TestSEOFromRequest(t *testing.T) {
|
||||||
t.Parallel()
|
t.Parallel()
|
||||||
|
|
||||||
req, err := http.NewRequest(
|
req := requestWithPortalOrigin(
|
||||||
http.MethodGet,
|
t,
|
||||||
"https://acme.probopage.localhost/fr/documents",
|
"https://acme.probopage.localhost/fr/documents",
|
||||||
nil,
|
|
||||||
)
|
|
||||||
require.NoError(t, err)
|
|
||||||
|
|
||||||
lang, canonical, hreflang := complianceportal_v1.SEOFromRequest(
|
|
||||||
req,
|
|
||||||
"https://acme.probopage.localhost",
|
"https://acme.probopage.localhost",
|
||||||
)
|
)
|
||||||
|
|
||||||
|
lang, canonical, hreflang := complianceportal_v1.SEOFromRequest(req)
|
||||||
assert.Equal(t, "fr", lang)
|
assert.Equal(t, "fr", lang)
|
||||||
assert.Equal(t, "https://acme.probopage.localhost/fr/documents", canonical)
|
assert.Equal(t, "https://acme.probopage.localhost/fr/documents", canonical)
|
||||||
require.NotEmpty(t, hreflang)
|
require.NotEmpty(t, hreflang)
|
||||||
@@ -69,78 +81,22 @@ func TestSEOFromRequest(t *testing.T) {
|
|||||||
func TestSEOFromRequest_EscapesPathSegments(t *testing.T) {
|
func TestSEOFromRequest_EscapesPathSegments(t *testing.T) {
|
||||||
t.Parallel()
|
t.Parallel()
|
||||||
|
|
||||||
req, err := http.NewRequest(
|
req := requestWithPortalOrigin(
|
||||||
http.MethodGet,
|
t,
|
||||||
"https://acme.probopage.localhost/en/docs/foo%20bar",
|
"https://acme.probopage.localhost/en/docs/foo%20bar",
|
||||||
nil,
|
|
||||||
)
|
|
||||||
require.NoError(t, err)
|
|
||||||
|
|
||||||
_, canonical, _ := complianceportal_v1.SEOFromRequest(
|
|
||||||
req,
|
|
||||||
"https://acme.probopage.localhost",
|
"https://acme.probopage.localhost",
|
||||||
)
|
)
|
||||||
|
|
||||||
|
_, canonical, _ := complianceportal_v1.SEOFromRequest(req)
|
||||||
assert.Equal(t, "https://acme.probopage.localhost/en/docs/foo%20bar", canonical)
|
assert.Equal(t, "https://acme.probopage.localhost/en/docs/foo%20bar", canonical)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestSEOFromRequest_StripsPathFromBaseURL(t *testing.T) {
|
func TestSEOFromRequest_MissingOriginOmitsLinks(t *testing.T) {
|
||||||
t.Parallel()
|
t.Parallel()
|
||||||
|
|
||||||
req, err := http.NewRequest(
|
req := requestWithPortalOrigin(t, "https://trust.acme.com/fr/documents", "")
|
||||||
http.MethodGet,
|
|
||||||
"https://trust.acme.com/fr/documents",
|
|
||||||
nil,
|
|
||||||
)
|
|
||||||
require.NoError(t, err)
|
|
||||||
|
|
||||||
// SNI middleware once stored scheme+host+path; SEO must not double it.
|
lang, canonical, hreflang := complianceportal_v1.SEOFromRequest(req)
|
||||||
_, canonical, hreflang := complianceportal_v1.SEOFromRequest(
|
|
||||||
req,
|
|
||||||
"https://trust.acme.com/fr/documents",
|
|
||||||
)
|
|
||||||
assert.Equal(t, "https://trust.acme.com/fr/documents", canonical)
|
|
||||||
|
|
||||||
var enHref string
|
|
||||||
|
|
||||||
for _, link := range hreflang {
|
|
||||||
if link.Lang == "en" {
|
|
||||||
enHref = link.Href
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
assert.Equal(t, "https://trust.acme.com/en/documents", enHref)
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestSEOFromRequest_EmptyBaseURLOmitsLinks(t *testing.T) {
|
|
||||||
t.Parallel()
|
|
||||||
|
|
||||||
req, err := http.NewRequest(
|
|
||||||
http.MethodGet,
|
|
||||||
"https://trust.acme.com/fr/documents",
|
|
||||||
nil,
|
|
||||||
)
|
|
||||||
require.NoError(t, err)
|
|
||||||
|
|
||||||
lang, canonical, hreflang := complianceportal_v1.SEOFromRequest(req, "")
|
|
||||||
assert.Equal(t, "fr", lang)
|
|
||||||
assert.Empty(t, canonical)
|
|
||||||
assert.Nil(t, hreflang)
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestSEOFromRequest_RelativeBaseURLOmitsLinks(t *testing.T) {
|
|
||||||
t.Parallel()
|
|
||||||
|
|
||||||
req, err := http.NewRequest(
|
|
||||||
http.MethodGet,
|
|
||||||
"https://trust.acme.com/fr/documents",
|
|
||||||
nil,
|
|
||||||
)
|
|
||||||
require.NoError(t, err)
|
|
||||||
|
|
||||||
lang, canonical, hreflang := complianceportal_v1.SEOFromRequest(
|
|
||||||
req,
|
|
||||||
"/fr/documents",
|
|
||||||
)
|
|
||||||
assert.Equal(t, "fr", lang)
|
assert.Equal(t, "fr", lang)
|
||||||
assert.Empty(t, canonical)
|
assert.Empty(t, canonical)
|
||||||
assert.Nil(t, hreflang)
|
assert.Nil(t, hreflang)
|
||||||
|
|||||||
Reference in New Issue
Block a user