SSR and SSG are increasingly popular web technologies, having well documented support for integrating Segment with these type of rendering would improve Segment's DX among many new projects, teams, and startups.
I use Next.js and had to do some searching along with trial and error to connect analytics.js to my app. Next does not use any static html files when developing - it pre-renders them at build/run time from jsx or tsx files. When the static pages are pre-rendered on the server (at build time/request time) the window variable doesn't exist, so the <script> snippet throws an error.
Generally, I would prefer to have control over the delivery of any javascript libraries, and I see similar sentiment across the issues for analytics.js and analytics.js-core.
Here's a workaround I found (so far) to be successful for adding the appropriate script tag in case anyone else is searching for this as I was. One note on the snippet itself - I wrote it inline at first to increase the readability and Typescript threw a number of errors, so you'll see a few tweaks and ts-ignore statements (even though it's in a template literal and TS doesn't parse it anymore).
SSR and SSG are increasingly popular web technologies, having well documented support for integrating Segment with these type of rendering would improve Segment's DX among many new projects, teams, and startups.
I use Next.js and had to do some searching along with trial and error to connect analytics.js to my app. Next does not use any static html files when developing - it pre-renders them at build/run time from jsx or tsx files. When the static pages are pre-rendered on the server (at build time/request time) the
window
variable doesn't exist, so the<script>
snippet throws an error.Generally, I would prefer to have control over the delivery of any javascript libraries, and I see similar sentiment across the issues for analytics.js and analytics.js-core.
Here's a workaround I found (so far) to be successful for adding the appropriate script tag in case anyone else is searching for this as I was. One note on the snippet itself - I wrote it inline at first to increase the readability and Typescript threw a number of errors, so you'll see a few tweaks and ts-ignore statements (even though it's in a template literal and TS doesn't parse it anymore).