Skip to content

Conversation

@toofishes
Copy link

@toofishes toofishes commented Jan 30, 2026

Changes

Two distinct changes here, broken down below. After making both of these changes, I am able to use server-side generation of a Vue-based website using vite-ssg and the node package, with a static import of the analytics code so it gets inlined and packed into the main JS bundle.

Initialize currentEngagementURL to empty string, not location.href

For use in a server-side rendered environment, it is important we don't access browser variables before init or track are called, that might not exist in Node/Bun/etc.

There isn't much reason to set this variable to location.href prior to postPageviewTrack being called, as we won't use it until engagements are registered, in which case, we will have set the URL from the payload.

This is the current error when importing (but not running) the code in Node:

Welcome to Node.js v25.4.0.
Type ".help" for more information.
> import('@plausible-analytics/tracker/plausible.js')
Promise {
  <pending>,
  Symbol(async_id_symbol): 103,
  Symbol(trigger_async_id_symbol): 6
}
> Uncaught ReferenceError: location is not defined

Set exports in plausible tracker package.json

Since the code is not in index.js, we need to declare where the code should be imported from when the module is imported using modern standards in order to be importable in Node. Webpack can use the old modules style, but Node does not. This addresses #5879.

After both of these changes, the import is successful:

Welcome to Node.js v25.4.0.
Type ".help" for more information.
> await import('@plausible-analytics/tracker')
[Module: null prototype] {
  DEFAULT_FILE_TYPES: [
    'pdf', 'xlsx', 'docx', 'txt',
...
    'wma', 'dmg'
  ],
  init: [Function: init],
  track: [Function: track]
}

Tests

  • This PR does not require tests

Changelog

  • Entry has been added to changelog
  • This PR does not make a user-facing change

I didn't update the tracker changelog- this seems like something done at release time, when I read the tracker-specific docs.

Documentation

  • This change does not need a documentation update

Dark mode

  • This PR does not change the UI

For use in a server-side rendered environment, it is important we don't access
browser variables before `init` or `track` are called, that might not exist in
Node/Bun/etc.

There isn't much reason to set this variable to location.href prior to
`postPageviewTrack` being called, as we won't use it until engagements are
registered, in which case, we will have set the URL from the payload.

This is the current error when importing (but not running) the code in Node:

```
Welcome to Node.js v25.4.0.
Type ".help" for more information.
> import('@plausible-analytics/tracker/plausible.js')
Promise {
  <pending>,
  Symbol(async_id_symbol): 103,
  Symbol(trigger_async_id_symbol): 6
}
> Uncaught ReferenceError: location is not defined
```
Since the code is not in `index.js`, we need to declare where the code should
be imported from when the module is imported using modern standards in order to
be importable in Node. Webpack can use the old `modules` style, but Node does
not. This addresses plausible#5879.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant