Skip to content

Conversation

GoldenSupremeSaltedFish
Copy link
Contributor

@GoldenSupremeSaltedFish GoldenSupremeSaltedFish commented Jun 21, 2025

What type of PR is this?
/kind bug
/area core
/milestone 2.21.x

What this PR does / why we need it:
Prevents published post view counts from being incorrectly increased when previewing posts in the admin panel.

Previously, the preview mode would load the tracking script, which resulted in inflated view counts. This PR adds a condition to detect preview mode and disables the tracking logic to avoid counting views for unpublished content.

Does this PR introduce user-facing changes?

文章预览页面不再统计访问数据

Copy link

f2c-ci-robot bot commented Jun 21, 2025

@GoldenSupremeSaltedFish: You must be a member of the halo-dev/milestone-maintainers GitHub team to set the milestone. If you believe you should be able to issue the /milestone command, please contact your Milestone Maintainers Team and have them propose you as an additional delegate for this responsibility.

In response to this:

What type of PR is this?
/kind bug
/area core
/milestone 2.21.x

What this PR does / why we need it:
Prevents published post view counts from being incorrectly increased when previewing posts in the admin panel.

Previously, the preview mode would load the tracking script, which resulted in inflated view counts. This PR adds a condition to detect preview mode and disables the tracking logic to avoid counting views for unpublished content.

Does this PR introduce user-facing changes?

Fix: previewing a post no longer increases its view count

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes-sigs/prow repository.

@f2c-ci-robot f2c-ci-robot bot added the kind/bug Categorizes issue or PR as related to a bug. label Jun 21, 2025
@f2c-ci-robot f2c-ci-robot bot requested review from guqing and JohnNiang June 21, 2025 07:59
@f2c-ci-robot f2c-ci-robot bot added area/core Issues or PRs related to the Halo Core do-not-merge/release-note-label-needed Indicates that a PR should not merge because it's missing one of the release note labels. labels Jun 21, 2025
@ruibaby
Copy link
Member

ruibaby commented Jun 23, 2025

I don't particularly recommend implementing this by modifying the tracker script. Instead, I suggest simply not loading this script on the preview page at all.

See https://github.com/halo-dev/halo/blob/7b257917bc69f09a8d8564167d7eac031ce96c56/application/src/main/java/run/halo/app/theme/dialect/HaloTrackerProcessor.java

cc @guqing

@guqing
Copy link
Member

guqing commented Jun 23, 2025

You can store an attribute in the ServerWebExchange in

RouterFunction<ServerResponse> previewRouter() {
to determine whether to inject the script by getting the value from HaloTrackerProcessor's context.getVariable

@GoldenSupremeSaltedFish
Copy link
Contributor Author

Received. I will resolve this issue by June 26th. Thank you for your reminder

@GoldenSupremeSaltedFish
Copy link
Contributor Author

Can this issue be assigned to me?

@ruibaby
Copy link
Member

ruibaby commented Jun 23, 2025

Can this issue be assigned to me?

Done

- Introduced a new constant `IS_PREVIEW` in `ModelConst` to indicate preview mode.
- Added `PreviewContextVariablesAcquirer` to provide context variables for preview mode.
- Updated `HaloTrackerProcessor` to skip script injection when in preview mode.
- Modified `PreviewRouterFunction` to set the preview mode attribute during post and single page rendering.
- 在 `ModelConst` 中添加 `IS_PREVIEW` 常量,用于指示预览模式。
- 更新 `PreviewContextVariablesAcquirer`,提供预览模式的上下文变量。
- 修改 `HaloTrackerProcessor`,在预览模式下跳过脚本注入。
- 更新 `PreviewRouterFunction`,为文章和单页渲染设置预览模式属性,并添加相关方法注释。
- Replace var with const/let
- Use arrow functions
- Simplify nested structures
- Use modern JS features
- Improve code maintainability

Fixes SonarQube code quality issues.
@GoldenSupremeSaltedFish
Copy link
Contributor Author

done

ruibaby and others added 4 commits June 25, 2025 22:55
Frontend preview detection is no longer needed as the preview mode
is now properly handled by HaloTrackerProcessor on the backend side
through template variable checking.

This follows the suggestion from @ruibaby that this file no longer
needs modification for preview mode support.
- Remove frontend preview mode detection
- Restore original var declarations and function syntax
- Remove Chinese comments
- Preview mode is now handled entirely by backend HaloTrackerProcessor

As suggested by @ruibaby, this file no longer needs modification.
- Replace var with const/let declarations
- Use arrow functions and modern syntax
- Reduce function nesting depth
- Extract helper functions for better separation
- Use template strings and strict equality
- Implement early return patterns

Fixes SonarQube code quality issues without changing functionality.
No preview detection logic added as per @ruibaby suggestion.
@GoldenSupremeSaltedFish
Copy link
Contributor Author

@ruibaby #1763 fix implemented and merged. Ready for your verification.🥰

@ruibaby
Copy link
Member

ruibaby commented Jun 26, 2025

halo-tracker.js still has diff, I recommend using the following method to revert the changes:

git checkout upstream/main -- application/src/main/resources/static/halo-tracker.js

git commit -m "Revert halo-tracker.js"

git push 

@guqing please help review the other parts.

@ruibaby ruibaby changed the title Fix/1763 preview no count refactor: exclude article preview page from data tracking Jun 30, 2025
@f2c-ci-robot f2c-ci-robot bot added release-note Denotes a PR that will be considered when it comes time to generate release notes. and removed do-not-merge/release-note-label-needed Indicates that a PR should not merge because it's missing one of the release note labels. labels Jun 30, 2025
@ruibaby ruibaby changed the title refactor: exclude article preview page from data tracking refactor: exclude post preview page from data tracking Jun 30, 2025
GoldenSupremeSaltedFish and others added 2 commits July 3, 2025 12:34
- Add SKIP_TRACKER constant in HaloTrackerProcessor
- Remove IS_PREVIEW from ModelConst to avoid polluting global constants
- Remove PreviewContextVariablesAcquirer for simpler implementation
- Update preview router to use SKIP_TRACKER attribute

This change ensures that post view counts are not incremented during preview,
providing a cleaner and more maintainable solution.

Fix halo-dev#1763
Copy link
Member

@guqing guqing left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

/approve

@f2c-ci-robot f2c-ci-robot bot added the approved Indicates a PR has been approved by an approver from all required OWNERS files. label Jul 4, 2025
Copy link

sonarqubecloud bot commented Jul 4, 2025

Copy link
Member

@ruibaby ruibaby left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

/lgtm

@f2c-ci-robot f2c-ci-robot bot added the lgtm Indicates that a PR is ready to be merged. label Jul 4, 2025
Copy link

f2c-ci-robot bot commented Jul 4, 2025

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: guqing, ruibaby

The full list of commands accepted by this bot can be found here.

The pull request process is described here

Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@f2c-ci-robot f2c-ci-robot bot merged commit 7922699 into halo-dev:main Jul 4, 2025
4 checks passed
@GoldenSupremeSaltedFish GoldenSupremeSaltedFish deleted the fix/1763-preview-no-count branch July 4, 2025 08:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
approved Indicates a PR has been approved by an approver from all required OWNERS files. area/core Issues or PRs related to the Halo Core kind/bug Categorizes issue or PR as related to a bug. lgtm Indicates that a PR is ready to be merged. release-note Denotes a PR that will be considered when it comes time to generate release notes.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants