Skip to content

Commit 04d6b6b

Browse files
committed
feat(analytics): add page-view tracking
1 parent 8458dcb commit 04d6b6b

5 files changed

Lines changed: 41 additions & 19 deletions

File tree

‎docs/adr/0052-public-runtime-and-retention-architecture.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ The request log follows the same rule rather than a second one. `public-error-ob
7575

7676
Two absences are deliberate. `X-Frame-Options` is not sent, because `frame-ancestors 'none'` says the same thing to every browser that has shipped in a decade and two spellings of one rule drift. `Cross-Origin-Embedder-Policy` is not sent, because cross-origin isolation buys `SharedArrayBuffer`, which the measured render lane does not use, and `require-corp` would break cross-origin subresources for no gain.
7777

78-
The policy is composed at request time rather than built in, because the profile decides it and the profile is a runtime input. SvelteKit owns the nonce on the app shell's inline bootstrap script, so `kit.csp` declares `script-src` and the hook merges what SvelteKit emitted into the full policy — a CSP is enforced as a whole, so a second header would only intersect with the first and drop that nonce. `style-src` carries `'unsafe-inline'` on purpose: the engine positions and animates every composition element through inline `style` attributes, and there is no hash or nonce form for an attribute that changes every frame. `blob:` is allowed as an image, as media, and over `fetch`, because a visitor's own media never reaches the origin — it is read from a granted file handle into an object URL. The header layer covers every response the SvelteKit handler produces; `build/client` is served by the Node adapter's own static handler ahead of the hook and keeps that handler's immutable caching, which is the adapter's lane and has no configuration seam.
78+
The policy is composed at request time rather than built in, because the profile decides it and the profile is a runtime input. SvelteKit owns the nonce on the app shell's inline bootstrap script, so `kit.csp` declares `script-src` and the hook merges what SvelteKit emitted into the full policy — a CSP is enforced as a whole, so a second header would only intersect with the first and drop that nonce. `https://analytics.tolin.ski` is the one external script and connection origin admitted for page-view analytics; it receives no composition content. `style-src` carries `'unsafe-inline'` on purpose: the engine positions and animates every composition element through inline `style` attributes, and there is no hash or nonce form for an attribute that changes every frame. `blob:` is allowed as an image, as media, and over `fetch`, because a visitor's own media never reaches the origin — it is read from a granted file handle into an object URL. The header layer covers every response the SvelteKit handler produces; `build/client` is served by the Node adapter's own static handler ahead of the hook and keeps that handler's immutable caching, which is the adapter's lane and has no configuration seam.
7979

8080
**No durable content.** The public runtime persists nothing on behalf of a visitor: no accounts, no server-side composition store, no uploaded media, no output archive.
8181

‎src/app.html‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,11 @@
55
<meta name="viewport" content="width=device-width, initial-scale=1" />
66
<meta name="text-scale" content="scale" />
77
<meta name="gfx-release" content="%gfx.release%" />
8+
<script
9+
defer
10+
data-domain="gfx.computer"
11+
src="https://analytics.tolin.ski/js/script.js"
12+
></script>
813
%sveltekit.head%
914
</head>
1015
<body data-sveltekit-preload-data="hover">

‎src/lib/platform/public-response-headers.test.ts‎

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,11 @@ describe('composePublicContentSecurityPolicy', () => {
4343
composePublicContentSecurityPolicy("script-src 'self' 'nonce-Ab3=='")
4444
);
4545

46-
assert.deepEqual(directives.get('script-src'), ["'self'", "'nonce-Ab3=='"]);
46+
assert.deepEqual(directives.get('script-src'), [
47+
"'self'",
48+
'https://analytics.tolin.ski',
49+
"'nonce-Ab3=='"
50+
]);
4751
});
4852

4953
it('keeps a prerendered page script hash', () => {
@@ -79,6 +83,17 @@ describe('composePublicContentSecurityPolicy', () => {
7983
assert.deepEqual(directives.get('worker-src'), ["'self'", 'blob:']);
8084
});
8185

86+
it('admits only the analytics origin for an external script and connection', () => {
87+
const directives = parsePolicy(composePublicContentSecurityPolicy(null));
88+
89+
assert.deepEqual(directives.get('script-src'), ["'self'", 'https://analytics.tolin.ski']);
90+
assert.deepEqual(directives.get('connect-src'), [
91+
"'self'",
92+
'blob:',
93+
'https://analytics.tolin.ski'
94+
]);
95+
});
96+
8297
it('carries through a directive the app shell declared and this contract does not', () => {
8398
const directives = parsePolicy(
8499
composePublicContentSecurityPolicy("script-src 'self'; style-src-elem 'sha256-xyz='")
@@ -90,7 +105,7 @@ describe('composePublicContentSecurityPolicy', () => {
90105
it('emits no duplicate token when the app shell repeats one', () => {
91106
const directives = parsePolicy(composePublicContentSecurityPolicy("script-src 'self'"));
92107

93-
assert.deepEqual(directives.get('script-src'), ["'self'"]);
108+
assert.deepEqual(directives.get('script-src'), ["'self'", 'https://analytics.tolin.ski']);
94109
});
95110
});
96111

‎src/lib/platform/public-response-headers.ts‎

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -108,10 +108,10 @@ export const PUBLIC_PERMISSIONS_POLICY: string = [
108108
/**
109109
* The Content Security Policy a public HTML document carries.
110110
*
111-
* `script-src` is `'self'` here and gains SvelteKit's per-response nonce in
112-
* `composePublicContentSecurityPolicy` — SvelteKit owns that nonce because it
113-
* owns the inline bootstrap script it belongs to (`kit.csp` in
114-
* `svelte.config.js`).
111+
* `script-src` admits this origin plus the analytics script origin and gains
112+
* SvelteKit's per-response nonce in `composePublicContentSecurityPolicy` —
113+
* SvelteKit owns that nonce because it owns the inline bootstrap script it
114+
* belongs to (`kit.csp` in `svelte.config.js`).
115115
*
116116
* `'unsafe-inline'` on `style-src` is load-bearing rather than lazy: the engine
117117
* positions and animates every composition element through inline `style`
@@ -122,21 +122,20 @@ export const PUBLIC_PERMISSIONS_POLICY: string = [
122122
* visitor's own media never reaches the origin — it is read from a granted file
123123
* handle into an object URL and fetched back from there.
124124
*
125-
* `connect-src` names no external address, which is the point: the browser talks
126-
* to this origin and to its own object URLs, and to nothing else. Browser-side
127-
* Sentry would be a cross-origin connect and is therefore not allowed — which
128-
* costs nothing, because `PUBLIC_SENTRY_DSN` is not one of the deployment inputs
129-
* a public host is given (`PUBLIC_RUNTIME_DEPLOYMENT_INPUTS`). Server-side
130-
* Sentry is unaffected: it reports from the origin, not from the page.
125+
* `connect-src` admits one external address: the analytics origin that receives
126+
* page-view events from its own script. Browser-side Sentry remains disallowed —
127+
* `PUBLIC_SENTRY_DSN` is not one of the deployment inputs a public host is given
128+
* (`PUBLIC_RUNTIME_DEPLOYMENT_INPUTS`). Server-side Sentry is unaffected: it
129+
* reports from the origin, not from the page.
131130
*
132131
* `worker-src` admits `blob:` because the browser export lane
133132
* (`browser-webm-export.ts`, the hosted origin's only lane) encodes alpha in a
134133
* worker that Mediabunny builds from a Blob URL — the colour/alpha splitter a
135134
* transparent VP9 export needs. Without it the directive falls back to
136-
* `script-src`, which names only `'self'`, and a transparent export fails with
135+
* `script-src`, which does not admit `blob:`, and a transparent export fails with
137136
* "Color/alpha splitter worker error" while an opaque one, which needs no
138-
* splitter, succeeds. A blob worker is still this page's own code; nothing
139-
* cross-origin is admitted by it.
137+
* splitter, succeeds. A blob worker is still this page's own code; no
138+
* cross-origin worker is admitted.
140139
*/
141140
export const PUBLIC_CONTENT_SECURITY_POLICY_DIRECTIVES: Readonly<
142141
Record<string, readonly string[]>
@@ -146,13 +145,13 @@ export const PUBLIC_CONTENT_SECURITY_POLICY_DIRECTIVES: Readonly<
146145
'object-src': ["'none'"],
147146
'frame-ancestors': ["'none'"],
148147
'form-action': ["'self'"],
149-
'script-src': ["'self'"],
148+
'script-src': ["'self'", 'https://analytics.tolin.ski'],
150149
'worker-src': ["'self'", 'blob:'],
151150
'style-src': ["'self'", "'unsafe-inline'"],
152151
'img-src': ["'self'", 'blob:', 'data:'],
153152
'media-src': ["'self'", 'blob:'],
154153
'font-src': ["'self'"],
155-
'connect-src': ["'self'", 'blob:'],
154+
'connect-src': ["'self'", 'blob:', 'https://analytics.tolin.ski'],
156155
'upgrade-insecure-requests': []
157156
};
158157

‎svelte.config.js‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,10 @@ const config = {
2222
// emit one; the public origin then merges that nonce into the full policy
2323
// at request time, because the rest of the policy depends on the runtime
2424
// profile rather than the build (see public-response-headers.ts).
25-
csp: { mode: 'auto', directives: { 'script-src': ['self'] } },
25+
csp: {
26+
mode: 'auto',
27+
directives: { 'script-src': ['self', 'https://analytics.tolin.ski'] }
28+
},
2629
// Every integration rebuilds gfx.robo.online and drops the previous build's
2730
// hashed chunks. An open tab polls for the new build so its next navigation
2831
// is a full page load (root layout) rather than an import the origin can no

0 commit comments

Comments
 (0)