Skip to content

Commit bc4ec8c

Browse files
committed
Serve redirects in deploy previews from the Hugo manifest
grafana/website#32512 replaces client-side alias pages with a generated redirects manifest (dist/redirects.txt). Previews serve dist directly, so without rewrites alias URLs would 404 once that change ships in docs-base. Generate dist/preview-redirects.conf after the Hugo build with the redirects binary already present in the docs-base image, and include it in the preview nginx server block. Older images have a redirects binary without the --manifest option, so fall back to scraping dist for alias pages, which preserves current behaviour. The temporary alias in whats-new.md triggers the preview workflow and exercises the redirect end to end. Remove it before merge. Signed-off-by: Jack Baldry <jack.baldry@grafana.com>
1 parent c998fff commit bc4ec8c

4 files changed

Lines changed: 23 additions & 0 deletions

File tree

deploy-preview/Dockerfile

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,8 @@ RUN rm -rf /etc/nginx/sites-enabled && \
88
COPY deploy-preview-files/deploy-preview/nginx.conf /etc/nginx/nginx.conf
99
COPY build.conf /etc/nginx/build.conf
1010

11+
# The build script writes redirect rewrites into dist. Ensure the include
12+
# target exists even when the build script predates redirects generation.
13+
RUN touch /usr/share/nginx/dist/preview-redirects.conf
14+
1115
RUN nginx -t

deploy-preview/build

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,14 @@ EOF
5151
fi
5252
cat "/hugo/content/docs/${REPO}/_index.md"
5353
HUGO_SSI=false hugo --environment=docs --destination=dist/ --baseURL= --minify
54+
55+
# Generate nginx rewrites for the preview server from the Hugo redirects
56+
# manifest. Older docs-base images have a redirects binary without the
57+
# --manifest option, so fall back to scraping dist for alias pages.
58+
touch dist/preview-redirects.conf
59+
redirects --base-url "" --manifest dist/redirects.txt dist dist/preview-redirects.conf \
60+
|| redirects --base-url "" dist dist/preview-redirects.conf \
61+
|| true
5462
'
5563
# End legacy input.
5664
else
@@ -245,6 +253,14 @@ EOINDEX
245253
done
246254
247255
HUGO_SSI=false hugo --environment=docs --destination=dist/ --baseURL= --minify
256+
257+
# Generate nginx rewrites for the preview server from the Hugo redirects
258+
# manifest. Older docs-base images have a redirects binary without the
259+
# --manifest option, so fall back to scraping dist for alias pages.
260+
touch dist/preview-redirects.conf
261+
redirects --base-url "" --manifest dist/redirects.txt dist dist/preview-redirects.conf \
262+
|| redirects --base-url "" dist dist/preview-redirects.conf \
263+
|| true
248264
EOSCRIPT
249265
chmod +x "${tempfile}"
250266
}

deploy-preview/nginx.conf

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ http {
3939
listen 80;
4040

4141
include /etc/nginx/build.conf;
42+
include /usr/share/nginx/dist/preview-redirects.conf;
4243

4344
add_header "X-UA-Compatible" "IE=Edge,chrome=1";
4445
add_header "Strict-Transport-Security" "max-age=31536000; includeSubDomains; preload";

docs/sources/whats-new.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
---
2+
aliases:
3+
- /docs/writers-toolkit/test-preview-redirect/ # TODO: remove before merge, exercises preview redirects.
24
date: 2022-08-27
35
description: |
46
A summary of notable changes to Writers' Toolkit guidance.

0 commit comments

Comments
 (0)