Skip to content
This repository was archived by the owner on Apr 4, 2025. It is now read-only.

Commit 75a7eec

Browse files
gary-bSomeKittens
authored andcommitted
fix(hint) support multi-page apps
If you visited a page on a website which did not have angular and subsequently visited a page which did, angular would not load on it. The second page had to be refreshed. Hint (or any other library) signals to angular to defer bootstrapping by setting window.name to a string starting with 'NG_DEFER_BOOTSTRAP!'. Angular then removes this prefix for you. Because angular was not on the initial page, the prefix was still on window.name when the second page loaded. Hint takes the presence of this to mean protractor is running. This causes hint to not initiate the bootstrap of angular, as protractor would do this if it were present. -Add a beforeunload event to remove the prefix if it is still present when page unloads -Update code where hint adds 'NG_DEFER_BOOTSTRAP!' to window.name so it just add sit as a prefix to whats already there. This means the contents of window.name are preserved for the client application -Update protractor detection code to just check for prefix and not do exact equals on whole string
1 parent 0f51832 commit 75a7eec

File tree

1 file changed

+13
-2
lines changed

1 file changed

+13
-2
lines changed

hint.js

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,13 @@ var AVAILABLE_MODULES = [
2525
];
2626

2727
var SEVERITY_WARNING = 2;
28+
var DEFER_LABEL = 'NG_DEFER_BOOTSTRAP!';
2829

30+
var deferRegex = new RegExp('^' + DEFER_LABEL + '.*');
2931
// Determine whether this run is by protractor.
3032
// If protractor is running, the bootstrap will already be deferred.
3133
// In this case `resumeBootstrap` should be patched to load the hint modules.
32-
if (window.name === 'NG_DEFER_BOOTSTRAP!') {
34+
if (deferRegex.test(window.name)) {
3335
var originalResumeBootstrap;
3436
Object.defineProperty(angular, 'resumeBootstrap', {
3537
get: function() {
@@ -44,10 +46,19 @@ if (window.name === 'NG_DEFER_BOOTSTRAP!') {
4446
}
4547
//If this is not a test, defer bootstrapping
4648
else {
47-
window.name = 'NG_DEFER_BOOTSTRAP!';
49+
window.name = DEFER_LABEL + window.name;
4850

4951
// determine which modules to load and resume bootstrap
5052
document.addEventListener('DOMContentLoaded', maybeBootstrap);
53+
54+
/* angular should remove DEFER_LABEL from window.name, but if angular is never loaded, we want
55+
to remove it ourselves, otherwise hint will incorrectly detect protractor as being present on
56+
the next page load */
57+
window.addEventListener('beforeunload', function() {
58+
if (deferRegex.test(window.name)) {
59+
window.name = window.name.substring(DEFER_LABEL.length);
60+
}
61+
});
5162
}
5263

5364
function maybeBootstrap() {

0 commit comments

Comments
 (0)