@reforma/site 0.0.3 → 0.0.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -4,17 +4,24 @@ type AnalyticsOptions = {
4
4
  /** Default false — skip localhost / 127.0.0.1. */
5
5
  captureOnLocalhost?: boolean;
6
6
  };
7
- type TrackProps = {
7
+ type TrackOptions = {
8
8
  url?: string;
9
9
  referrer?: string;
10
+ props?: Record<string, string>;
10
11
  };
11
12
  /**
12
13
  * Start first-party pageview collection for a published Reforma site.
13
- * Reads `NEXT_PUBLIC_REFORMA_ANALYTICS*` env baked on publish. Safe to call
14
- * multiple times — only the first call installs history hooks.
14
+ * Reads the project site config. Only the first call installs history hooks.
15
15
  */
16
- export declare function initAnalytics(options?: AnalyticsOptions): void;
17
- /** Send a named event. Today only `pageview` is stored by the API. */
18
- export declare function track(name: string, props?: TrackProps): void;
16
+ export declare function initAnalytics(config: {
17
+ analyticsEnabled: boolean;
18
+ analyticsWriteKey: string;
19
+ analyticsEndpoint: string;
20
+ }, options?: AnalyticsOptions): void;
21
+ /**
22
+ * Send a named event. `pageview` is traffic (deduped by path); any other name
23
+ * is a custom event / goal conversion. Optional `props` are string key/values.
24
+ */
25
+ export declare function track(name: string, options?: TrackOptions): void;
19
26
  export {};
20
27
  //# sourceMappingURL=analytics.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"analytics.d.ts","sourceRoot":"","sources":["../src/analytics.ts"],"names":[],"mappings":"AAAA,KAAK,gBAAgB,GAAG;IACpB,wDAAwD;IACxD,KAAK,CAAC,EAAE,OAAO,CAAC;IAChB,kDAAkD;IAClD,kBAAkB,CAAC,EAAE,OAAO,CAAC;CAChC,CAAC;AAEF,KAAK,UAAU,GAAG;IACd,GAAG,CAAC,EAAE,MAAM,CAAC;IACb,QAAQ,CAAC,EAAE,MAAM,CAAC;CACrB,CAAC;AASF;;;;GAIG;AACH,wBAAgB,aAAa,CAAC,OAAO,GAAE,gBAAqB,GAAG,IAAI,CAqClE;AAED,sEAAsE;AACtE,wBAAgB,KAAK,CAAC,IAAI,EAAE,MAAM,EAAE,KAAK,GAAE,UAAe,GAAG,IAAI,CA6ChE"}
1
+ {"version":3,"file":"analytics.d.ts","sourceRoot":"","sources":["../src/analytics.ts"],"names":[],"mappings":"AAAA,KAAK,gBAAgB,GAAG;IACpB,wDAAwD;IACxD,KAAK,CAAC,EAAE,OAAO,CAAC;IAChB,kDAAkD;IAClD,kBAAkB,CAAC,EAAE,OAAO,CAAC;CAChC,CAAC;AAEF,KAAK,YAAY,GAAG;IAChB,GAAG,CAAC,EAAE,MAAM,CAAC;IACb,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,KAAK,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;CAClC,CAAC;AASF;;;GAGG;AACH,wBAAgB,aAAa,CACzB,MAAM,EAAE;IAAE,gBAAgB,EAAE,OAAO,CAAC;IAAC,iBAAiB,EAAE,MAAM,CAAC;IAAC,iBAAiB,EAAE,MAAM,CAAA;CAAE,EAC3F,OAAO,GAAE,gBAAqB,GAC/B,IAAI,CAqCN;AAED;;;GAGG;AACH,wBAAgB,KAAK,CAAC,IAAI,EAAE,MAAM,EAAE,OAAO,GAAE,YAAiB,GAAG,IAAI,CA6DpE"}
package/dist/analytics.js CHANGED
@@ -6,10 +6,9 @@ let lastPath = '';
6
6
  let unhook = null;
7
7
  /**
8
8
  * Start first-party pageview collection for a published Reforma site.
9
- * Reads `NEXT_PUBLIC_REFORMA_ANALYTICS*` env baked on publish. Safe to call
10
- * multiple times — only the first call installs history hooks.
9
+ * Reads the project site config. Only the first call installs history hooks.
11
10
  */
12
- export function initAnalytics(options = {}) {
11
+ export function initAnalytics(config, options = {}) {
13
12
  if (typeof window === 'undefined') {
14
13
  return;
15
14
  }
@@ -23,9 +22,9 @@ export function initAnalytics(options = {}) {
23
22
  return;
24
23
  }
25
24
  started = true;
26
- enabled = process.env.NEXT_PUBLIC_REFORMA_ANALYTICS === '1';
27
- writeKey = process.env.NEXT_PUBLIC_REFORMA_ANALYTICS_KEY?.trim() ?? '';
28
- endpoint = process.env.NEXT_PUBLIC_REFORMA_ANALYTICS_ENDPOINT?.trim() ?? '';
25
+ enabled = config.analyticsEnabled;
26
+ writeKey = config.analyticsWriteKey.trim();
27
+ endpoint = config.analyticsEndpoint.trim();
29
28
  if (!enabled || !writeKey || !endpoint) {
30
29
  enabled = false;
31
30
  return;
@@ -39,25 +38,39 @@ export function initAnalytics(options = {}) {
39
38
  track('pageview');
40
39
  });
41
40
  }
42
- /** Send a named event. Today only `pageview` is stored by the API. */
43
- export function track(name, props = {}) {
44
- if (!enabled || name !== 'pageview' || typeof window === 'undefined') {
41
+ /**
42
+ * Send a named event. `pageview` is traffic (deduped by path); any other name
43
+ * is a custom event / goal conversion. Optional `props` are string key/values.
44
+ */
45
+ export function track(name, options = {}) {
46
+ if (!enabled || typeof window === 'undefined') {
45
47
  return;
46
48
  }
47
- const url = props.url ?? window.location.href;
48
- const path = pathOf(url);
49
- if (path === lastPath) {
49
+ const eventName = name.trim();
50
+ if (!eventName) {
50
51
  return;
51
52
  }
52
- lastPath = path;
53
- const payload = JSON.stringify({
53
+ const url = options.url ?? window.location.href;
54
+ const path = pathOf(url);
55
+ if (eventName === 'pageview') {
56
+ if (path === lastPath) {
57
+ return;
58
+ }
59
+ lastPath = path;
60
+ }
61
+ const payload = {
54
62
  key: writeKey,
63
+ name: eventName,
55
64
  url,
56
- referrer: props.referrer ?? (document.referrer || undefined),
57
- });
65
+ referrer: options.referrer ?? (document.referrer || undefined),
66
+ };
67
+ if (eventName !== 'pageview' && options.props && typeof options.props === 'object') {
68
+ payload.props = options.props;
69
+ }
70
+ const bodyText = JSON.stringify(payload);
58
71
  const body = typeof Blob === 'undefined'
59
- ? payload
60
- : new Blob([payload], { type: 'application/json' });
72
+ ? bodyText
73
+ : new Blob([bodyText], { type: 'application/json' });
61
74
  try {
62
75
  if (typeof navigator !== 'undefined' && typeof navigator.sendBeacon === 'function') {
63
76
  const ok = navigator.sendBeacon(endpoint, body);
@@ -67,7 +80,7 @@ export function track(name, props = {}) {
67
80
  }
68
81
  void fetch(endpoint, {
69
82
  method: 'POST',
70
- body: payload,
83
+ body: bodyText,
71
84
  headers: { 'Content-Type': 'application/json' },
72
85
  keepalive: true,
73
86
  mode: 'cors',
@@ -113,6 +126,6 @@ function pathOf(url) {
113
126
  return new URL(url, window.location.origin).pathname;
114
127
  }
115
128
  catch {
116
- return url;
129
+ return '/';
117
130
  }
118
131
  }
@@ -1,6 +1,9 @@
1
- export declare function projectMetadata(fallback: {
1
+ export declare function projectMetadata(defaults: {
2
2
  title: string;
3
3
  description: string;
4
+ }, config: {
5
+ faviconUrl: string;
6
+ indexingEnabled: boolean;
4
7
  }): {
5
8
  title: {
6
9
  template: string;
@@ -18,7 +21,9 @@ export declare function projectMetadata(fallback: {
18
21
  follow: boolean;
19
22
  };
20
23
  };
21
- export declare function projectRobots(): {
24
+ export declare function projectRobots(config: {
25
+ indexingEnabled: boolean;
26
+ }): {
22
27
  rules: {
23
28
  userAgent: string;
24
29
  disallow: string;
@@ -1 +1 @@
1
- {"version":3,"file":"metadata.d.ts","sourceRoot":"","sources":["../src/metadata.ts"],"names":[],"mappings":"AAAA,wBAAgB,eAAe,CAAC,QAAQ,EAAE;IAAE,KAAK,EAAE,MAAM,CAAC;IAAC,WAAW,EAAE,MAAM,CAAA;CAAE;;QAMpE,QAAQ;QACR,OAAO;;;;QAIP,IAAI;;;;;;;;;EAQf;AAED,wBAAgB,aAAa;IAIrB,KAAK;;;;;;;EAKZ"}
1
+ {"version":3,"file":"metadata.d.ts","sourceRoot":"","sources":["../src/metadata.ts"],"names":[],"mappings":"AAAA,wBAAgB,eAAe,CAC3B,QAAQ,EAAE;IAAE,KAAK,EAAE,MAAM,CAAC;IAAC,WAAW,EAAE,MAAM,CAAA;CAAE,EAChD,MAAM,EAAE;IAAE,UAAU,EAAE,MAAM,CAAC;IAAC,eAAe,EAAE,OAAO,CAAA;CAAE;;QAOhD,QAAQ;QACR,OAAO;;;;QAIP,IAAI;;;;;;;;;EAQf;AAED,wBAAgB,aAAa,CAAC,MAAM,EAAE;IAAE,eAAe,EAAE,OAAO,CAAA;CAAE;IAI1D,KAAK;;;;;;;EAKZ"}
package/dist/metadata.js CHANGED
@@ -1,12 +1,12 @@
1
- export function projectMetadata(fallback) {
2
- const faviconUrl = process.env.REFORMA_FAVICON_URL || '/favicon.svg';
3
- const indexingOn = process.env.REFORMA_INDEXING !== '0';
1
+ export function projectMetadata(defaults, config) {
2
+ const faviconUrl = config.faviconUrl || '/favicon.svg';
3
+ const indexingOn = config.indexingEnabled;
4
4
  return {
5
5
  title: {
6
6
  template: '%s',
7
- default: process.env.REFORMA_DEFAULT_TITLE || fallback.title,
7
+ default: defaults.title,
8
8
  },
9
- description: process.env.REFORMA_DEFAULT_DESCRIPTION || fallback.description,
9
+ description: defaults.description,
10
10
  icons: {
11
11
  icon: faviconUrl.endsWith('.svg')
12
12
  ? { url: faviconUrl, type: 'image/svg+xml' }
@@ -17,8 +17,8 @@ export function projectMetadata(fallback) {
17
17
  : { index: false, follow: false },
18
18
  };
19
19
  }
20
- export function projectRobots() {
21
- const blocked = process.env.REFORMA_INDEXING === '0';
20
+ export function projectRobots(config) {
21
+ const blocked = !config.indexingEnabled;
22
22
  return {
23
23
  rules: {
24
24
  userAgent: '*',
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@reforma/site",
3
- "version": "0.0.3",
3
+ "version": "0.0.5",
4
4
  "description": "Visitor runtime for a Reforma site: mode, publish metadata, and the PostCSS url guard.",
5
5
  "type": "module",
6
6
  "license": "UNLICENSED",