drupalSettings accessible to PartyTown SW

Created on 2 July 2025, about 1 month ago

The project I'm working on has a load of GTM variable which are derived from drupalSettings. I'm guessing this isn't going to be a niche approach so it would be good to extend the module so the service worker can access the drupalSettings object when available as a default.

Probably my ignorance of how PT works but I expected party town to be able to resolve things like window.drupalSettings but, based on my testing, this is not the case. My workaround is to include an additional type=text/partytown script on the page where I initialize drupalSettings in the same way core is doing on the main thread.

Is this something you've explored yet?

πŸ’¬ Support request
Status

Active

Version

1.0

Component

Code

Created by

πŸ‡¬πŸ‡§United Kingdom rattusrattus

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

  • Issue created by @rattusrattus
  • πŸ‡¬πŸ‡§United Kingdom rattusrattus
  • πŸ‡ΊπŸ‡ΈUnited States abenbow Asheville, North Carolina

    So the way I handled this is by writing a custom JS library (here), and then mounting it to the page with a lower weight than the Partytown library. This script maps all of the drupalSettings we care about directly into the window.partytown variable and, importantly, gets that done before the browser executes the SW. The demo from the qwik.dev developers shows all of the settings being maped in a separate script tag, but all it does is set a window variable, so I figured this would work too.

    In the module file, I only care about mapping my Drupal config settings into the drupalSettings object and then mounting all of my libraries to the page.

    This method works (I think) because I'm serving everything as a first-party resource, and Drupal is free to sort out what gets mounted to the page and when. Negative weight in a libraries.yml file isn't widly recommended, but it would seem to me that the API exists for these kinds of issues.

    Regardless, letting the module have a foothold in the frontend stack seems like a good way to modify things in the same space and in the same language Partytown is hanging out in.

    Is there a better way to do this? Perhaps. This solution seems to reach all the way across the stack, perhaps too far, and I suspect it may not always work on custom UIs. It would be cool if the prepreocess_html hook could handle everything on its own, but I haven't done much testing there.

  • πŸ‡ΊπŸ‡ΈUnited States abenbow Asheville, North Carolina

    I've been thinking about this for a bit. I've seen Partytown misbehave sometimes and ignore the window.partytown.lib setting, but only for like one request, and then it fixes it.

    Have you seen this happen using your method?

    I like the idea of rendering an HTML tag instead of using a library for some aspects of this tool, but I worry about reliably converting PHP values into JSON. How has this been working for you?

Production build 0.71.5 2024