diff --git a/.env.local b/.env.local index c7c37481..f7d7ec09 100644 --- a/.env.local +++ b/.env.local @@ -1,2 +1,2 @@ # 环境变量 @see https://www.nextjs.cn/docs/basic-features/environment-variables -NEXT_PUBLIC_VERSION=4.0.8 \ No newline at end of file +NEXT_PUBLIC_VERSION=4.0.9 \ No newline at end of file diff --git a/blog.config.js b/blog.config.js index d1a0d961..32e80f10 100644 --- a/blog.config.js +++ b/blog.config.js @@ -119,6 +119,8 @@ const BLOG = { // Mermaid 图表CDN MERMAID_CDN: process.env.NEXT_PUBLIC_MERMAID_CDN || 'https://cdnjs.cloudflare.com/ajax/libs/mermaid/10.2.4/mermaid.min.js', // CDN + // QRCodeCDN + QR_CODE_CDN: process.env.NEXT_PUBLIC_QR_CODE_CDN || 'https://cdnjs.cloudflare.com/ajax/libs/qrcodejs/1.0.0/qrcode.min.js', BACKGROUND_LIGHT: '#eeeeee', // use hex value, don't forget '#' e.g #fffefc BACKGROUND_DARK: '#000000', // use hex value, don't forget '#' @@ -307,12 +309,10 @@ const BLOG = { ANALYTICS_CNZZ_ID: process.env.NEXT_PUBLIC_ANALYTICS_CNZZ_ID || '', // 只需要填写站长统计的id, [cnzz_id] -> https://s9.cnzz.com/z_stat.php?id=[cnzz_id]&web_id=[cnzz_id] ANALYTICS_GOOGLE_ID: process.env.NEXT_PUBLIC_ANALYTICS_GOOGLE_ID || '', // 谷歌Analytics的id e.g: G-XXXXXXXXXX - ANALYTICS_ACKEE_TRACKER: - process.env.NEXT_PUBLIC_ANALYTICS_ACKEE_TRACKER || '', // e.g 'https://ackee.tangly1024.net/tracker.js' - ANALYTICS_ACKEE_DATA_SERVER: - process.env.NEXT_PUBLIC_ANALYTICS_ACKEE_DATA_SERVER || '', // e.g https://ackee.tangly1024.net , don't end with a slash - ANALYTICS_ACKEE_DOMAIN_ID: - process.env.NEXT_PUBLIC_ANALYTICS_ACKEE_DOMAIN_ID || '', // e.g '0e2257a8-54d4-4847-91a1-0311ea48cc7b' + // ACKEE网站访客统计工具 + ANALYTICS_ACKEE_TRACKER: process.env.NEXT_PUBLIC_ANALYTICS_ACKEE_TRACKER || '', // e.g 'https://ackee.tangly1024.com/tracker.js' + ANALYTICS_ACKEE_DATA_SERVER: process.env.NEXT_PUBLIC_ANALYTICS_ACKEE_DATA_SERVER || '', // e.g https://ackee.tangly1024.com , don't end with a slash + ANALYTICS_ACKEE_DOMAIN_ID: process.env.NEXT_PUBLIC_ANALYTICS_ACKEE_DOMAIN_ID || '', // e.g '82e51db6-dec2-423a-b7c9-b4ff7ebb3302' SEO_GOOGLE_SITE_VERIFICATION: process.env.NEXT_PUBLIC_SEO_GOOGLE_SITE_VERIFICATION || '', // Remove the value or replace it with your own google site verification code diff --git a/components/Ackee.js b/components/Ackee.js index ab22c969..dd9467f0 100644 --- a/components/Ackee.js +++ b/components/Ackee.js @@ -1,21 +1,83 @@ -import { useRouter } from 'next/router' -import useAckee from 'use-ackee' -import BLOG from '@/blog.config' +'use strict' +import { useEffect } from 'react' +import BLOG from '@/blog.config' +import { loadExternalResource } from '@/lib/utils' +import { useRouter } from 'next/router' const Ackee = () => { const router = useRouter() - useAckee( - router.asPath, - { - server: BLOG.ANALYTICS_ACKEE_DATA_SERVER, - domainId: BLOG.ANALYTICS_ACKEE_DOMAIN_ID - }, - { - detailed: false, - ignoreLocalhost: true - } - ) + + // handleAckee 函数 + const handleAckeeCallback = () => { + handleAckee( + router.asPath, + { + server: BLOG.ANALYTICS_ACKEE_DATA_SERVER, + domainId: BLOG.ANALYTICS_ACKEE_DOMAIN_ID + }, + { + /* + * Enable or disable tracking of personal data. + * We recommend to ask the user for permission before turning this option on. + */ + detailed: true, + /* + * Enable or disable tracking when on localhost. + */ + ignoreLocalhost: false, + /* + * Enable or disable the tracking of your own visits. + * This is enabled by default, but should be turned off when using a wildcard Access-Control-Allow-Origin header. + * Some browsers strictly block third-party cookies. The option won't have an impact when this is the case. + */ + ignoreOwnVisits: false + } + ) + } + + // 或者使用其他依赖数组,根据需要执行 handleAckee + useEffect(() => { + handleAckeeCallback() + }, [router]) + return null } export default Ackee + +/** + * Function to use Ackee. + * Creates an instance once and a new record every time the pathname changes. + * Safely no-ops during server-side rendering. + * @param {?String} pathname - Current path. + * @param {Object} environment - Object containing the URL of the Ackee server and the domain id. + * @param {?Object} options - Ackee options. + */ +const handleAckee = async function(pathname, environment, options = {}) { + await loadExternalResource(BLOG.ANALYTICS_ACKEE_TRACKER, 'js') + const ackeeTracker = window.ackeeTracker + + const instance = ackeeTracker.create(environment.server, options) + + if (instance == null) { + console.warn('Skipped record creation because useAckee has been called in a non-browser environment') + return + } + + const hasPathname = ( + pathname != null && pathname !== '' + ) + + if (hasPathname === false) { + console.warn('Skipped record creation because useAckee has been called without pathname') + return + } + + const attributes = ackeeTracker.attributes(options.detailed) + const url = new URL(pathname, location) + + return instance.record(environment.domainId, { + ...attributes, + siteLocation: url.href + }).stop +} diff --git a/components/Comment.js b/components/Comment.js index 2baebfbb..1b335fe7 100644 --- a/components/Comment.js +++ b/components/Comment.js @@ -62,7 +62,7 @@ const ValineComponent = dynamic(() => import('@/components/ValineComponent'), { const Comment = ({ frontMatter, className }) => { const router = useRouter() - if (isBrowser() && ('giscus' in router.query || router.query.target === 'comment')) { + if (isBrowser && ('giscus' in router.query || router.query.target === 'comment')) { setTimeout(() => { const url = router.asPath.replace('?target=comment', '') history.replaceState({}, '', url) diff --git a/components/CommonScript.js b/components/CommonScript.js index 6912c3cc..4bef4da0 100644 --- a/components/CommonScript.js +++ b/components/CommonScript.js @@ -56,12 +56,12 @@ const CommonScript = () => { {/* 代码统计 */} {/* ackee统计脚本 */} - {BLOG.ANALYTICS_ACKEE_TRACKER && ( + {/* {BLOG.ANALYTICS_ACKEE_TRACKER && (