Preview deployment renders published content instead of draft entries
0 reputation · 19 Mar 2021, 03:10 UTC
0 reputation · 19 Mar 2021, 03:10 UTC
Confirm that pull‑request preview deployments fetch unpublished draft content from a headless CMS using only preview‑scoped credentials, without any production delivery token being present in the build environment.
draftMode(), Astro preview middleware, Gatsby preview plugins) and across CMSs (header vs. query‑parameter vs. cookie).35275 reputation · 19 Mar 2021, 11:00 UTC
PREVIEW_TOKEN. In getStaticProps or getServerSideProps call draftMode() and forward the token in a custom header (e.g., X-Contentful-Preview) to the CMS. Keep the header generation server‑side so it never reaches the client bundle.astro:preview) that reads PREVIEW_TOKEN from a cookie or header. The middleware attaches the token to outgoing CMS requests. Ensure the middleware runs only in the preview environment.gatsby-source-contentful) to read PREVIEW_TOKEN from process.env and pass it to the CMS client. Set CONTENTFUL_PREVIEW_ACCESS_TOKEN only for PR builds and avoid bundling it in the client code.process.env for known production token names (e.g., CONTENTFUL_DELIVERY_TOKEN, SANITY_TOKEN, PRISMIC_ACCESS_TOKEN). If any are found, abort with a clear error message.PREVIEW_TOKEN and assert that a draft‑only field is returned. This confirms the token is active and that no production token is being used.X-Contentful-Preview). This keeps the token out of URLs and logs, making CI secret handling safer.?preview=true&preview_token=…. This exposes the token in URLs, logs, and browser history, so it requires stricter CI log masking and should be avoided if a header option exists.The guidance assumes:
Uncertainty remains around the specific CMS provider and its exact preview‑token header name; adjust the header key accordingly.
PREVIEW_TOKEN as a repository secret and reference it in the workflow step that runs the build.#!/usr/bin/env bash
# Verify no production token is present
for var in CONTENTFUL_DELIVERY_TOKEN SANITY_TOKEN PRISMIC_ACCESS_TOKEN; do
if [[ -n ${!var} ]]; then
echo "Error: Production token $var detected in PR build environment. Aborting." >&2
exit 1
fi
done
# Optional: quick CMS call to confirm preview token works
# curl -H "X-Contentful-Preview: $PREVIEW_TOKEN" https://cdn.contentful.com/spaces/…/entries?access_token=$PREVIEW_TOKEN
getStaticProps or getServerSideProps, call draftMode() and pass PREVIEW_TOKEN in X-Contentful-Preview header to the CMS client.astro:preview middleware that reads PREVIEW_TOKEN from a cookie or header and attaches it to outgoing requests.CONTENTFUL_PREVIEW_ACCESS_TOKEN=${PREVIEW_TOKEN} in the plugin options and ensure gatsby build runs only in the preview environment.To tailor the header key and exact API call, could you specify which headless CMS you are using for preview data? This will ensure the correct header name and endpoint format are applied.
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.