Optimizing Previews

Avoid loading slow or unnecessary code in page previews

When a page is rendered, external scripts that do not affect the page visually – for example, Google Analytics scripts and other tracking scripts – can slow it down.

In case of page previews from within the CMS Console, such scripts can also pollute the page's statistics, since they are generated by users editing content and not real visitors.

You can make these scripts only work based on the rendering reason for the page. Thus, they will be  loaded only when the rendering reason is, for example, a page requested through a web browser (PageView).

Here is a sample Razor script that uses the RenderingReason to load scripts and some content only when a published page is viewed in a web browser:

@inherits RazorFunction
@using Composite.Core.WebClient.Renderings.Page

<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        @if( PageRenderer.RenderingReason == RenderingReason.Undefined || 
				PageRenderer.RenderingReason == RenderingReason.PageView)
        {
            <script src="onlyForTruePages.js" />
        }
    </head>
    <body>
        @if( PageRenderer.RenderingReason == RenderingReason.Undefined ||
				PageRenderer.RenderingReason == RenderingReason.PageView)
        {
            <h1>Not a preview...</h1>
        }
    </body>
</html>