Do not access location during import of tracker module#6039
Open
toofishes wants to merge 2 commits intoplausible:masterfrom
Open
Do not access location during import of tracker module#6039toofishes wants to merge 2 commits intoplausible:masterfrom
location during import of tracker module#6039toofishes wants to merge 2 commits intoplausible:masterfrom
Conversation
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.
95aae20 to
31e45ae
Compare
Author
|
I'm not sure if I'm able to get the |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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.hrefFor use in a server-side rendered environment, it is important we don't access browser variables before
initortrackare called, that might not exist in Node/Bun/etc.There isn't much reason to set this variable to location.href prior to
postPageviewTrackbeing 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:
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 oldmodulesstyle, but Node does not. This addresses #5879.After both of these changes, the import is successful:
Tests
Changelog
I didn't update the tracker changelog- this seems like something done at release time, when I read the tracker-specific docs.
Documentation
Dark mode