Do not access location during import of tracker module
#6039
+7
−1
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