Skip to content
This repository has been archived by the owner on Feb 11, 2021. It is now read-only.

[Solution] Prevent preview script from being downloaded if not needed #228

Open
Bram-Zijp opened this issue Jun 10, 2020 · 0 comments
Open

Comments

@Bram-Zijp
Copy link

I thought id just drop this here as a solution for those who need and as inspiration for the plugin author. It basically injects a script that will check if it has the expected value's to load the preview script.

// inside gatsby-ssr.js
import React from 'react';

const injectPrismaScriptHtml = `
(function(){
  var isPreviewPage = window.location.pathname.split('/').indexOf('preview') >= 0;
  var hasPrismicCookie = document.cookie.indexOf('prismic.preview') >= 0;
  if (isPreviewPage || hasPrismicCookie) {
    var script = document.createElement('script');
    script.src = '//static.cdn.prismic.io/prismic.min.js';
    script.type = 'text/javascript';
    document.body.appendChild(script);
  }
})();
`;

export const onRenderBody = ({ setPostBodyComponents }) => {
  setPostBodyComponents([
    <script
      // eslint-disable-next-line react/no-danger
      dangerouslySetInnerHTML={{
        __html: injectPrismaScriptHtml,
      }}
    />,
  ]);
};
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant