Skip to content

Commit 8206a8c

Browse files
fix: persist document.referrer when doing page refresh (#28)
1 parent d75471a commit 8206a8c

2 files changed

Lines changed: 52 additions & 1 deletion

File tree

src/index.ts

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,12 @@ function checkClientHints() {
116116
if (cookieChanged) {
117117
// Increment reload attempts counter
118118
sessionStorage.setItem('clientHintReloadAttempts', String(reloadAttempts + 1));
119-
119+
120+
// Preserve document.referrer before reload so analytics tools can access it
121+
if (document.referrer) {
122+
sessionStorage.setItem('clientHintReferrer', document.referrer);
123+
}
124+
120125
// Hide the page content immediately to prevent visual flicker
121126
const style = document.createElement('style');
122127
style.textContent = 'html { visibility: hidden !important; }';
@@ -127,6 +132,16 @@ function checkClientHints() {
127132
} else {
128133
// Reset reload attempts counter if no reload was needed
129134
sessionStorage.removeItem('clientHintReloadAttempts');
135+
136+
// Restore document.referrer if it was preserved from a client-hint reload
137+
const savedReferrer = sessionStorage.getItem('clientHintReferrer');
138+
if (savedReferrer) {
139+
sessionStorage.removeItem('clientHintReferrer');
140+
Object.defineProperty(document, 'referrer', {
141+
value: savedReferrer,
142+
configurable: true,
143+
});
144+
}
130145
}
131146
}
132147

test/index.test.ts

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,3 +191,39 @@ test('client script includes infinite refresh prevention', () => {
191191
assert.ok(checkScript.includes('catch'))
192192
assert.ok(checkScript.includes('decodeURIComponent'))
193193
})
194+
195+
test('client script preserves document.referrer across reloads', () => {
196+
const hints = getHintUtils({
197+
colorScheme: colorSchemeHint,
198+
timeZone: timeZoneHint,
199+
reducedMotion: reducedMotionHint,
200+
})
201+
202+
const checkScript = hints.getClientHintCheckScript()
203+
204+
// Should save document.referrer to sessionStorage before reload
205+
assert.ok(
206+
checkScript.includes('clientHintReferrer'),
207+
'script should use clientHintReferrer sessionStorage key',
208+
)
209+
assert.ok(
210+
checkScript.includes("sessionStorage.setItem('clientHintReferrer'"),
211+
'script should save referrer to sessionStorage before reload',
212+
)
213+
assert.ok(
214+
checkScript.includes("sessionStorage.getItem('clientHintReferrer')"),
215+
'script should read saved referrer from sessionStorage after reload',
216+
)
217+
218+
// Should restore document.referrer via Object.defineProperty
219+
assert.ok(
220+
checkScript.includes("Object.defineProperty(document, 'referrer'"),
221+
'script should restore document.referrer via Object.defineProperty',
222+
)
223+
224+
// Should clean up sessionStorage after restoring
225+
assert.ok(
226+
checkScript.includes("sessionStorage.removeItem('clientHintReferrer')"),
227+
'script should remove saved referrer from sessionStorage after restoring',
228+
)
229+
})

0 commit comments

Comments
 (0)