MCPcopy Index your code
hub / github.com/ampproject/amphtml / removeViewerQueryBeforeNavigation_

Method removeViewerQueryBeforeNavigation_

src/service/navigation.js:555–588  ·  view source on GitHub ↗

* Temporarily remove viewer query params from iframe (e.g. amp_js_v, usqp) * to prevent document.referrer from being reduced to eTLD+1 (e.g. ampproject.org). * @param {!Window} win * @param {!Location} fromLocation * @param {string} target * @private

(win, fromLocation, target)

Source from the content-addressed store, hash-verified

553 * @private
554 */
555 removeViewerQueryBeforeNavigation_(win, fromLocation, target) {
556 dev().info(
557 TAG,
558 'Removing iframe query string before navigation:',
559 fromLocation.search
560 );
561 const original = fromLocation.href;
562 const noQuery = `${fromLocation.origin}${fromLocation.pathname}${fromLocation.hash}`;
563 win.history.replaceState(null, '', noQuery);
564
565 const restoreQuery = () => {
566 const currentHref = win.location.href;
567 if (currentHref == noQuery) {
568 dev().info(TAG, 'Restored iframe URL with query string:', original);
569 win.history.replaceState(null, '', original);
570 } else {
571 dev().error(TAG, 'Unexpected iframe URL change:', currentHref, noQuery);
572 }
573 };
574
575 // For blank_, restore query params after the new page opens.
576 if (target === '_blank') {
577 win.setTimeout(restoreQuery, 0);
578 } else {
579 // For _top etc., wait until page is restored from page cache (bfcache).
580 // https://webkit.org/blog/516/webkit-page-cache-ii-the-unload-event/
581 win.addEventListener('pageshow', function onPageShow(e) {
582 if (e.persisted) {
583 restoreQuery();
584 win.removeEventListener('pageshow', onPageShow);
585 }
586 });
587 }
588 }
589
590 /**
591 * Handles clicking on an internal link

Callers 1

handleNavigation_Method · 0.95

Calls 5

devFunction · 0.90
infoMethod · 0.80
replaceStateMethod · 0.80
removeEventListenerMethod · 0.80
addEventListenerMethod · 0.45

Tested by

no test coverage detected