I need to pass a nonce from `_document` to `_app` in Next.js

Viewed 36

I tried using the enhanceApp or enhanceComponent but the prop or static property never reaches _app for some reason.

  ctx.renderPage = () =>
    originalRenderPage({
      enhanceApp: (App) => {
        App.nonce = nonce;
        return App;
      },
      enhanceComponent: (Component) => {
        Component.nonce = nonce;
        return Component;
      }
    });

Or even:

  ctx.renderPage = () =>
    originalRenderPage({
      enhanceApp: (App: AppType | React.ComponentType<{nonce: string}>) =>
        function EnhancedApp(props) {
          return <App {...props} nonce={nonce} />;
        }
    });

Obs.: I cannot add a getInitialProps in app because I need static page optimization.

Is this the right approach? Any help would be greatly appreciated.

0 Answers
Related