Skip to main content

Serve secondary templates from your site

Get your site ready before an author deploys a secondary template. Your site then shows a secondary template only to links that name it, keeps it out of search engines and shared caches, and answers 404 for a link it can't serve.

A secondary template is a connected template on the same content type as the primary one, at exactly the same Template URL. It opens only when the address carries cs-template and its composition UID. Freeform templates and sections can't be secondary. Authors use it to show a redesign on your real address before visitors see it. For the model, see Primary and secondary templates.

The example on this page is the Blog post template, primary at /blog/{{entry.slug}} on production, and Blog post v2, a secondary template at the same URL. Its link is https://www.example.com/blog/ai-101?cs-template=blog_post_v2.

Prerequisites​

What this is and what you get​

The Studio SDK decides which template a request gets. Your site decides how that page is served: its status code, its headers and whether search engines may index it. The SDK adds nothing to the page's HTML, so these steps are yours.

Two requests to your site. A request for /blog/ai-101?cs-template=blog_post_v2 skips your CDN cache, reaches your route with its query string, and the Studio SDK picks Blog post v2. Your route answers with Cache-Control: private, no-store and a noindex robots tag. A request for /blog/ai-101 without the parameter goes through your normal cache, gets Blog post, and is cached and indexed as usual.

When you're done:

  • /blog/ai-101 shows Blog post to every visitor, from your cache as before.
  • /blog/ai-101?cs-template=blog_post_v2 shows Blog post v2, with noindex and no shared caching.
  • /blog/ai-101?cs-template=blog_post_v3 answers 404.

Faster ways to do this​

  • Skill. Ask your agent to get your site ready for secondary templates, and it uses deploy-studio-site. It checks your Studio SDK version, your query string, your robots tag and your cache headers. See What are skills?

Run Studio SDK X or later on every deployment​

Upgrade @contentstack/studio-react to Studio SDK X or later, then deploy your site everywhere it runs. See Install the Studio SDK.

An older Studio SDK doesn't know which template is secondary. It treats Blog post and Blog post v2 as two templates at one URL, so visitors can get Blog post v2 without the link.

Studio checks the version only on the site the canvas loads. If that site is too old, Studio greys out the secondary choices when an author deploys. It can't see your other deployments, such as your production servers, so upgrading them is up to you.

Keep cs-template in the query string​

A secondary template's link works only when cs-template reaches the Studio SDK. Pass the query string through, and don't read it yourself.

Code that builds a new URL from the path alone drops cs-template, such as a proxy or a rewrite. Copy the original query string onto any URL you build. See Keep Studio's query string.

A page you build ahead of time, with no request to forward, gets no cs-template. It always shows the primary template.

Never put cs-template in a link on your site, in a Custom Preview URL or in a URL rule. Everyone who follows that link sees the secondary template.

Keep secondary templates out of search and shared caches​

A request with cs-template in its query string can show a secondary template. Treat every such request the same way:

  • Add a noindex robots tag, so search engines leave the page out.
  • On the server, send Cache-Control: private, no-store, so no shared cache keeps the page.

The plain address, without cs-template, stays indexed and cached as before. A link whose cs-template names the primary template gets noindex too, which does no harm, because search engines still index the plain address.

On the server:

import { isCompositionNotFound } from "@contentstack/studio-react";

const hasTemplateParam = new URLSearchParams(search).has("cs-template");

try {
const specOptions = await sdk.fetchCompositionData({ url: pathname, searchQuery: search });
return {
status: 200,
headers: hasTemplateParam ? { "Cache-Control": "private, no-store" } : {},
robots: hasTemplateParam ? "noindex" : null, // write <meta name="robots" content="noindex" /> into the head
specOptions,
};
} catch (error) {
if (isCompositionNotFound(error)) return { status: 404, robots: "noindex" };
return { status: 500 };
}

In the browser, add the tag while the address has cs-template:

useEffect(() => {
if (!new URLSearchParams(window.location.search).has("cs-template")) return;
const meta = document.createElement("meta");
meta.name = "robots";
meta.content = "noindex";
document.head.appendChild(meta);
return () => meta.remove();
}, []);

A site that renders only in the browser can't set response headers. Keep its cs-template links out of shared caches at your CDN instead, as the next step shows.

Keep your CDN from serving a cached secondary template​

A CDN that ignores the query string stores /blog/ai-101?cs-template=blog_post_v2 and /blog/ai-101 as one page. It can then give Blog post v2 to every visitor. Skip the cache for any request with cs-template in its query string. Cache-Control: private, no-store on secondary pages also keeps them out of a cache that honors it.

Studio's own requests need the same care. See Caching and invalidation.

When cs-template names a template your site can't show at the address, the fetch fails with a not-found error, as it does for an address no template matches. It never falls back to the primary template.

codeWhenisCompositionNotFoundRespond with
REQUESTED_COMPOSITION_UNAVAILABLEcs-template names a composition UID that isn't a secondary template here: mistyped, not deployed in this environment and language, at another URL, or on another content type.trueA 404
NO_PRIMARY_TEMPLATE_FOR_SECONDARYcs-template names a secondary template at this URL, but no template is primary there.trueA 404

isCompositionNotFound already counts both, so the 404 handling from Send a 404 when no template matches covers them. Add noindex to the 404 too. The message names the code, the address and the composition UID:

[Contentstack Studio] REQUESTED_COMPOSITION_UNAVAILABLE: Check that the composition UID is correct and that it's published in this environment and locale.

For each code, what it means and how to fix it, see REQUESTED_COMPOSITION_UNAVAILABLE and NO_PRIMARY_TEMPLATE_FOR_SECONDARY.

How each way of fetching treats secondary templates​

Studio, Visual Editor and Live Preview ignore cs-template, and show the template the author opens. On your live site, it depends on how your route fetches:

  • By address, a visitor gets the primary template, or the secondary template that cs-template names. See Fetch by address.
  • By composition UID, a visitor gets the template you name, secondary or not. Use it only for templates you mean to show. See Fetch a template and an entry by UID.
  • By content type, a visitor gets a primary template, and cs-template is ignored. See Fetch by content type.

A secondary template falls back across languages the way the primary template does. See Fetch content in a language.

Check that a secondary template is served correctly​

Ask an author to deploy Blog post v2 as a secondary template, then:

  1. Open https://www.example.com/blog/ai-101. It shows Blog post.

  2. Open https://www.example.com/blog/ai-101?cs-template=blog_post_v2. It shows Blog post v2, and the page source has <meta name="robots" content="noindex" />.

  3. Check the headers of the same address. On a server-rendered site, the response has Cache-Control: private, no-store:

    curl -sI "https://www.example.com/blog/ai-101?cs-template=blog_post_v2"
  4. Open https://www.example.com/blog/ai-101?cs-template=blog_post_v3. It answers 404.

If it doesn't work​

Where to go next​

Report a problem with this page