GoogleChromeLabs / prerender-loader

📰 Painless universal pre-rendering for Webpack.
https://npm.im/prerender-loader
Apache License 2.0
1.91k stars 50 forks source link

'PRERENDER' in template.html #24

Open ezekielchentnik opened 5 years ago

ezekielchentnik commented 5 years ago

is there a way to access 'PRERENDER' env variable inside a regular template.html?

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="utf-8" />
    <script>
        if ('serviceWorker' in navigator) {
            navigator.serviceWorker.register('/service-worker.js');
        }
    </script>
    <title>Taco</title>
    <meta name="mobile-web-app-capable" content="yes" />
    <meta name="apple-mobile-web-app-capable" content="yes" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <meta name="theme-color" content="#000000" />
    <link rel="manifest" href="/manifest.json" />
</head>

<body>
    <noscript>You need to enable JavaScript to run this app.</noscript>
    <div id="portal"></div>
    <main id="root">{{prerender:./prerender.js}}</div>
</body>

</html>
developit commented 5 years ago

Hmm - I think it should be there? That gets into one of the weirder issues with DefinePlugin - it leaks into parent compiler instances, but perhaps it doesn't leak into sibling compilers? If so then there's no way to have that variable exposed to your template.

One option would be to pass a value directly to html-webpack-plugin via the templateParameters option:

new HtmlWebpackPlugin({
  template: '!!prerender-loader!template.html',
  templateParameters: {
    PRERENDER: true
  }
})
ezekielchentnik commented 5 years ago

thx, I'll give it a try, the 'PRERENDER' provided inside the plugin doesn't seem to be available

developit commented 5 years ago

IIRC you can use self.PRERENDER