Skip to content

Disable hydration after full page load #113

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/LazyHydrate.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ export default makeHydrationBlocker(Placeholder, {
triggerHydration: {
immediate: true,
handler(isTriggered) {
if (isTriggered) this.hydrate();
if (isTriggered && this.hydrate) this.hydrate();
},
},
},
Expand Down
16 changes: 16 additions & 0 deletions src/utils/disabled.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
const isServer = typeof window === `undefined`;
let isPageLodaded = false;

if (!isServer) {
// load event fires, when document with all scripts is loaded,
// so after hydration process is finished
window.addEventListener(`load`, () => {
isPageLodaded = true;
});
}

export function isHydrationDisabled() {
// Hydration may be disabled because we are in SSR context
// or page was fully loaded & hydrated, so it's not needed anymore
return isServer || isPageLodaded;
}
17 changes: 13 additions & 4 deletions src/utils/hydration-blocker.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,30 @@
import { makeHydrationObserver } from './hydration-observer';
import { makeHydrationPromise } from './hydration-promise';
import { makeNonce } from './nonce';
import { isHydrationDisabled } from './disabled';

export function makeHydrationBlocker(component, options) {
return Object.assign({
mixins: [{
beforeCreate() {
this.cleanupHandlers = [];
const { hydrate, hydrationPromise } = makeHydrationPromise();
this.Nonce = makeNonce({ component, hydrationPromise });
this.hydrate = hydrate;
this.hydrationPromise = hydrationPromise;

if (isHydrationDisabled()) {
this.Nonce = component;
} else {
const { hydrate, hydrationPromise } = makeHydrationPromise();
this.Nonce = makeNonce({ component, hydrationPromise });
this.hydrate = hydrate;
this.hydrationPromise = hydrationPromise;
}
},
beforeDestroy() {
this.cleanup();
},
mounted() {
// hydration is disabled
if (!this.hydrate) return;

if (this.$el.nodeType === Node.COMMENT_NODE) {
// No SSR rendered content, hydrate immediately.
this.hydrate();
Expand Down
4 changes: 0 additions & 4 deletions src/utils/nonce.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
const isServer = typeof window === `undefined`;

function isAsyncComponentFactory(componentOrFactory) {
return typeof componentOrFactory === `function`;
}
Expand All @@ -12,7 +10,5 @@ function resolveComponent(componentOrFactory) {
}

export function makeNonce({ component, hydrationPromise }) {
if (isServer) return component;
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because I check this in isHydrationDisabled it's not needed anymore.


return () => hydrationPromise.then(() => resolveComponent(component));
}