I am currently working on an angularJS component that utilizes ui-router with 2 straightforward route states.
export default function Routes($stateProvider, $urlRouterProvider, $locationProvider) {
$stateProvider
.state('details', {
url: '/',
template: '<div>...</div>'
})
.state('pdf', {
url: '/viewpdf',
template: '<pdf-viewer></pdf-viewer>'
});
$urlRouterProvider.otherwise(function ($injector, $location) {
var $state = $injector.get("$state");
$state.go("details");
});
}
Within the details
view, there is a controller responsible for retrieving a PDF document. Upon fetching the document successfully, the route state is updated within the callback using $state.go('pdf');
In the pdf view, there is a link with ui-sref
functionality that redirects back to the details view:
<a ui-sref="details">Back to Details</a>
Sporadically, when clicking the Back to Details button, an error is triggered by page.js and does not change the route state.
Uncaught TypeError: Cannot read property '0' of undefined at new Context (page.js:208) at Function.page.replace (page.js:154) at onpopstate (page.js:347) at B (history.min.js:21) at history.min.js:22 Context @ page.js:208 page.replace @ page.js:154 onpopstate @ page.js:347 B @ history.min.js:21 (anonymous) @ history.min.js:22
Upon investigating the source of the error, it points to line 208 in page.js:
/**
* Initialize a new "request" `Context`
* with the given `path` and optional initial `state`.
*
* @param {String} path
* @param {Object} state
* @api public
*/
function Context(path, state) {
/* ERROR STACK ENDS ON THIS LINE */
if ('/' == path[0] && 0 != path.indexOf(base)) path = base + path;
/* END */
var i = path.indexOf('?');
this.canonicalPath = path;
this.path = path.replace(base, '') || '/';
this.title = document.title;
this.state = state || {};
this.state.path = path;
this.querystring = ~i ? path.slice(i + 1) : '';
this.pathname = ~i ? path.slice(0, i) : path;
this.params = [];
// fragment
this.hash = '';
if (!~this.path.indexOf('#')) return;
var parts = this.path.split('#');
this.path = parts[0];
this.hash = parts[1] || '';
this.querystring = this.querystring.split('#')[0];
}
Despite encountering the error, the URL and view remain at /viewpdf
. Interestingly, if I wait a few seconds and click the back button again, it functions correctly.
What could be causing this issue, and how can it be resolved?
Edit:
To clarify, the reference to the back button pertains to the Back to Details button within the /viewpdf
view, not the browser's built-in back button. The browser's back button does not experience this bug.