nuxt-modules / partytown

Partytown integration for Nuxt. Run third-party scripts from a web worker.
MIT License
316 stars 8 forks source link

Example of reverse proxy configuration #294

Open madsh93 opened 1 year ago

madsh93 commented 1 year ago

Hi

I'm looking to implement Facebook pixel. This requires a reverse proxy. Can this be done with Nitro? If that's the case I was hoping you'd be able to provide an example similar as to how you did with Plausible and GTM.

Rekwian commented 5 months ago

I'm not sure if my answer matches your question, but I'll try. I tried a solution, I think it can be called "proxy". Simply create an API that fetches the URL that needs to be "proxied" and in the script, call that URL.

An example from my experience with Axeptio : I created an API called Axeptio

//  /server/api/axeptio
export default defineEventHandler((event) => {
  return $fetch('https://static.axept.io/sdk.js')
})

And in my nuxt.config.ts

export default defineNuxtConfig({
  app: {
    head: {
      script: [
        {
          innerHTML: `
          window.axeptioSettings = {
            clientId: "xxx",
          };

          (function(d, s) {
            var t = d.getElementsByTagName(s)[0], e = d.createElement(s);
            e.async = true; e.src = "/api/axeptio";
            t.parentNode.insertBefore(e, t);
          })(document, "script");
          `,
          type: 'text/partytown',
          tagPosition: 'bodyClose',
        }
      ],
    }
  },
  ...

And it works fine.