From 74c33e9483c4a5f1acdbb321e395b982b670dec8 Mon Sep 17 00:00:00 2001 From: Jason Stirnaman Date: Mon, 22 Dec 2025 11:03:27 -0500 Subject: [PATCH] fix(ci): add path offset for PR preview subdirectory baseURL (#6662) When PR preview builds use a subdirectory baseURL like /docs-v2/pr-preview/pr-XXXX/, shortcodes that parse .RelPermalink to detect product context fail because the path has extra segments. This fix: - Adds config/pr-preview/params.yml with prPreviewPathOffset: 3 - Updates workflow to use -e pr-preview environment - Updates api-endpoint, influxdb/host, and children shortcodes to use the offset when indexing path segments - Adds nil-safety with default fallback for placeholder_host Normal builds are unaffected (offset defaults to 0). --- .github/workflows/pr-preview.yml | 3 ++- config/pr-preview/params.yml | 8 ++++++++ layouts/shortcodes/api-endpoint.html | 9 ++++++--- layouts/shortcodes/children.html | 11 +++++++---- layouts/shortcodes/influxdb/host.html | 9 ++++++--- 5 files changed, 29 insertions(+), 11 deletions(-) create mode 100644 config/pr-preview/params.yml diff --git a/.github/workflows/pr-preview.yml b/.github/workflows/pr-preview.yml index 17516f7a4..d0aba7ecc 100644 --- a/.github/workflows/pr-preview.yml +++ b/.github/workflows/pr-preview.yml @@ -90,7 +90,8 @@ jobs: PREVIEW_BASE_URL: https://influxdata.github.io/docs-v2/pr-preview/pr-${{ github.event.number }}/ run: | START_TIME=$(date +%s) - npx hugo --minify --baseURL "$PREVIEW_BASE_URL" + # Use pr-preview environment for path offset config + npx hugo --minify --baseURL "$PREVIEW_BASE_URL" -e pr-preview END_TIME=$(date +%s) DURATION=$((END_TIME - START_TIME)) echo "build-time=${DURATION}s" >> $GITHUB_OUTPUT diff --git a/config/pr-preview/params.yml b/config/pr-preview/params.yml new file mode 100644 index 000000000..8e9719724 --- /dev/null +++ b/config/pr-preview/params.yml @@ -0,0 +1,8 @@ +# PR Preview environment parameters +# Used when building with: npx hugo -e pr-preview + +# Number of path segments to skip when parsing RelPermalink for product detection +# For baseURL like https://influxdata.github.io/docs-v2/pr-preview/pr-6657/ +# The path has 3 extra segments: docs-v2, pr-preview, pr-XXXX +# Workaround to correctly detect products in preview builds for Hugo templates relying on relPermalink to set active product +prPreviewPathOffset: 3 diff --git a/layouts/shortcodes/api-endpoint.html b/layouts/shortcodes/api-endpoint.html index 1203a87a4..bb48c4f78 100644 --- a/layouts/shortcodes/api-endpoint.html +++ b/layouts/shortcodes/api-endpoint.html @@ -1,13 +1,16 @@ {{- $productPathData := findRE "[^/]+.*?" .Page.RelPermalink -}} -{{- $currentVersion := index $productPathData 1 -}} +{{- /* Support deployments (such as CI tools) with subdirectory baseURL */ -}} +{{- $pathOffset := .Site.Params.prPreviewPathOffset | default 0 -}} +{{- $currentVersion := index $productPathData (add $pathOffset 1) -}} {{- $endpoint := .Get "endpoint" -}} {{- $isOSS := ne (len (findRE `^v[0-9]` $currentVersion)) 0 -}} {{- $parsedProductKey := cond $isOSS "oss" $currentVersion -}} {{- $productKey := .Get "influxdb_host" | default $parsedProductKey -}} {{- $productAliases := dict "oss" "influxdb" "cloud" "influxdb_cloud" "cloud-tsm" "influxdb_cloud" "core" "influxdb3_core" "enterprise" "influxdb3_enterprise" "cloud-serverless" "influxdb3_cloud_serverless" "serverless" "influxdb3_cloud_serverless" "cloud-dedicated" "influxdb3_cloud_dedicated" "dedicated" "influxdb3_cloud_dedicated" "clustered" "influxdb3_clustered" -}} {{- $productRef := index $productAliases $productKey -}} -{{- $productData := index .Site.Data.products $productRef -}} -{{- $placeholderHost := $productData.placeholder_host }} +{{- $productData := dict -}} +{{- with $productRef }}{{- $productData = index $.Site.Data.products . | default dict -}}{{- end -}} +{{- $placeholderHost := $productData.placeholder_host | default "localhost:8086" }} {{- $method := .Get "method" | upper -}} {{- $methodStyle := .Get "method" | lower -}} {{- $apiRef := .Get "api-ref" | default "" -}} diff --git a/layouts/shortcodes/children.html b/layouts/shortcodes/children.html index 1bea94f94..67d2921c3 100644 --- a/layouts/shortcodes/children.html +++ b/layouts/shortcodes/children.html @@ -42,14 +42,17 @@ {{ end }} {{ end }} {{ if .Params.list_code_example }} - {{- $productPathData := findRE "[^/]+.*?" .Page.RelPermalink -}} - {{- $currentVersion := index $productPathData 1 -}} + {{- $productPathData := findRE "[^/]+.*?" $.Page.RelPermalink -}} + {{- /* Support deployments (such as CI tools) with subdirectory baseURL */ -}} + {{- $pathOffset := $.Site.Params.prPreviewPathOffset | default 0 -}} + {{- $currentVersion := index $productPathData (add $pathOffset 1) -}} {{- $isOSS := ne (len (findRE `^v[0-9]` $currentVersion)) 0 -}} {{- $productKey := cond $isOSS "oss" $currentVersion -}} {{- $productAliases := dict "oss" "influxdb" "core" "influxdb3_core" "enterprise" "influxdb3_enterprise" "cloud" "influxdb_cloud" "cloud-tsm" "influxdb_cloud" "cloud-serverless" "influxdb3_cloud_serverless" "serverless" "influxdb3_cloud_serverless" "cloud-dedicated" "influxdb3_cloud_dedicated" "dedicated" "influxdb3_cloud_dedicated" "clustered" "influxdb3_clustered" -}} {{- $productRef := index $productAliases $productKey -}} - {{- $productData := index .Site.Data.products $productRef -}} - {{- $placeholderHost := $productData.placeholder_host }} + {{- $productData := dict -}} + {{- with $productRef }}{{- $productData = index $.Site.Data.products . | default dict -}}{{- end -}} + {{- $placeholderHost := $productData.placeholder_host | default "localhost:8086" }} {{ .Params.list_code_example | replaceRE `\{\{[<\%] influxdb/host [>%]\}\}` $placeholderHost | .RenderString }} {{ end }} diff --git a/layouts/shortcodes/influxdb/host.html b/layouts/shortcodes/influxdb/host.html index 0ed5f7b84..61494e80b 100644 --- a/layouts/shortcodes/influxdb/host.html +++ b/layouts/shortcodes/influxdb/host.html @@ -1,9 +1,12 @@ {{- $productPathData := findRE "[^/]+.*?" .Page.RelPermalink -}} -{{- $currentVersion := index $productPathData 1 -}} +{{- /* Support deployments (such as CI tools) with subdirectory baseURL */ -}} +{{- $pathOffset := .Site.Params.prPreviewPathOffset | default 0 -}} +{{- $currentVersion := index $productPathData (add $pathOffset 1) -}} {{- $isOSS := ne (len (findRE `^v[0-9]` $currentVersion)) 0 -}} {{- $parsedProductKey := cond $isOSS "oss" $currentVersion -}} {{- $productKey := .Get 0 | default $parsedProductKey -}} {{- $productAliases := dict "oss" "influxdb" "cloud" "influxdb_cloud" "cloud-tsm" "influxdb_cloud" "core" "influxdb3_core" "enterprise" "influxdb3_enterprise" "cloud-serverless" "influxdb3_cloud_serverless" "serverless" "influxdb3_cloud_serverless" "cloud-dedicated" "influxdb3_cloud_dedicated" "dedicated" "influxdb3_cloud_dedicated" "clustered" "influxdb3_clustered" -}} {{- $productRef := index $productAliases $productKey -}} -{{- $productData := index .Site.Data.products $productRef -}} -{{ $productData.placeholder_host }} \ No newline at end of file +{{- $productData := dict -}} +{{- with $productRef }}{{- $productData = index $.Site.Data.products . | default dict -}}{{- end -}} +{{ $productData.placeholder_host | default "localhost:8086" }} \ No newline at end of file