Skip to content

Commit

Permalink
throttle calls to replaceState to fix security error when navigating …
Browse files Browse the repository at this point in the history
…quickly in Safari hakimel#3147
  • Loading branch information
hakimel committed Mar 9, 2022
1 parent 853764b commit fc861fc
Showing 1 changed file with 28 additions and 2 deletions.
30 changes: 28 additions & 2 deletions js/controllers/location.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,19 @@
*/
export default class Location {

// The minimum number of milliseconds that must pass between
// calls to history.replaceState
MAX_REPLACE_STATE_FREQUENCY = 250

constructor( Reveal ) {

this.Reveal = Reveal;

// Delays updates to the URL due to a Chrome thumbnailer bug
this.writeURLTimeout = 0;

this.replaceStateTimestamp = 0;

this.onWindowHashChange = this.onWindowHashChange.bind( this );

}
Expand Down Expand Up @@ -142,10 +148,10 @@ export default class Location {
else if( config.hash ) {
// If the hash is empty, don't add it to the URL
if( hash === '/' ) {
window.history.replaceState( null, null, window.location.pathname + window.location.search );
this.debouncedReplaceState( window.location.pathname + window.location.search );
}
else {
window.history.replaceState( null, null, '#' + hash );
this.debouncedReplaceState( '#' + hash );
}
}
// UPDATE: The below nuking of all hash changes breaks
Expand All @@ -163,6 +169,26 @@ export default class Location {

}

replaceState( url ) {

window.history.replaceState( null, null, url );
this.replaceStateTimestamp = Date.now();

}

debouncedReplaceState( url ) {

clearTimeout( this.replaceStateTimeout );

if( Date.now() - this.replaceStateTimestamp > this.MAX_REPLACE_STATE_FREQUENCY ) {
this.replaceState( url );
}
else {
this.replaceStateTimeout = setTimeout( () => this.replaceState( url ), this.MAX_REPLACE_STATE_FREQUENCY );
}

}

/**
* Return a hash URL that will resolve to the given slide location.
*
Expand Down

0 comments on commit fc861fc

Please sign in to comment.