Skip to content

Commit 62f3c47

Browse files
lhagenWPGitHub Enterprise
authored andcommitted
Merge pull request #1 from platform/CON-6966
CON-6966 Disable hydration after full page load
2 parents fc76681 + 1aca3a8 commit 62f3c47

File tree

4 files changed

+30
-9
lines changed

4 files changed

+30
-9
lines changed

src/LazyHydrate.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ export default makeHydrationBlocker(Placeholder, {
7878
triggerHydration: {
7979
immediate: true,
8080
handler(isTriggered) {
81-
if (isTriggered) this.hydrate();
81+
if (isTriggered && this.hydrate) this.hydrate();
8282
},
8383
},
8484
},

src/utils/disabled.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
const isServer = typeof window === 'undefined';
2+
let isPageLoaded = false;
3+
4+
if (!isServer) {
5+
// load event fires when document with all scripts is loaded,
6+
// so after hydration process is finished
7+
window.addEventListener('load', () => {
8+
isPageLoaded = true;
9+
});
10+
}
11+
12+
export function isHydrationDisabled() {
13+
// hydration may be disabled because we are in SSR context
14+
// or page was fully loaded & hydrated, so it's not needed anymore
15+
return isServer || isPageLoaded;
16+
}

src/utils/hydration-blocker.js

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,30 @@
11
import { makeHydrationObserver } from './hydration-observer';
22
import { makeHydrationPromise } from './hydration-promise';
33
import { makeNonce } from './nonce';
4+
import { isHydrationDisabled } from './disabled';
45

56
export function makeHydrationBlocker(component, options) {
67
return Object.assign({
78
mixins: [{
89
beforeCreate() {
910
this.cleanupHandlers = [];
10-
const { hydrate, hydrationPromise } = makeHydrationPromise();
11-
this.Nonce = makeNonce({ component, hydrationPromise });
12-
this.hydrate = hydrate;
13-
this.hydrationPromise = hydrationPromise;
11+
12+
if (isHydrationDisabled()) {
13+
this.Nonce = component;
14+
} else {
15+
const { hydrate, hydrationPromise } = makeHydrationPromise();
16+
this.Nonce = makeNonce({ component, hydrationPromise });
17+
this.hydrate = hydrate;
18+
this.hydrationPromise = hydrationPromise;
19+
}
1420
},
1521
beforeDestroy() {
1622
this.cleanup();
1723
},
1824
mounted() {
25+
// hydration is disabled
26+
if (!this.hydrate) return;
27+
1928
if (this.$el.nodeType === Node.COMMENT_NODE) {
2029
// No SSR rendered content, hydrate immediately.
2130
this.hydrate();

src/utils/nonce.js

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
const isServer = typeof window === `undefined`;
2-
31
function isAsyncComponentFactory(componentOrFactory) {
42
return typeof componentOrFactory === `function`;
53
}
@@ -12,7 +10,5 @@ function resolveComponent(componentOrFactory) {
1210
}
1311

1412
export function makeNonce({ component, hydrationPromise }) {
15-
if (isServer) return component;
16-
1713
return () => hydrationPromise.then(() => resolveComponent(component));
1814
}

0 commit comments

Comments
 (0)