Skip to content
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

Prevent Route Flickers #364

Closed
wants to merge 7 commits into from
Closed
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
15 changes: 12 additions & 3 deletions src/components/async.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,25 @@
import { h, Component } from 'preact';

export default function(load) {
function emit(name) {
window.dispatchEvent( new Event(name) );
}

export default function (load) {
function Async() {
Component.call(this);
emit('async-loading');
let done = child => {
this.setState({ child: child && child.default || child });
child = child && child.default;
this.setState({ child, ready:!!child });
};
let r = load(done);
if (r && r.then) r.then(done);
}
Async.prototype = new Component;
Async.prototype.constructor = Async;
Async.prototype.render = (props, state) => h(state.child, props);
Async.prototype.componentDidUpdate = function (_, state) {
this.state.ready && !state.ready && emit('async-loaded');
};
Async.prototype.render = (props, state) => state.child && h(state.child, props);
return Async;
}
38 changes: 28 additions & 10 deletions src/lib/entry.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,38 @@ else if (process.env.ADD_SW && 'serviceWorker' in navigator && location.protocol
navigator.serviceWorker.register('/sw.js');
}

function getApp() {
let x = require('preact-cli-entrypoint');
return h(x && x.default || x);
}

let inProgress = false;
let body = document.body;
let root = body.firstElementChild;

const interopDefault = m => m && m.default ? m.default : m;
function init(first) {
if (inProgress) return;

let app = interopDefault(require('preact-cli-entrypoint'));
let app = getApp();

if (typeof app==='function') {
let root = document.body.firstElementChild;
if (first) {
render(app, body.cloneNode(true));
} else {
root = render(app, body, root);
}
}

let init = () => {
let app = interopDefault(require('preact-cli-entrypoint'));
root = render(h(app), document.body, root);
};
if (module.hot) {
module.hot.accept('preact-cli-entrypoint', init);
}

if (module.hot) module.hot.accept('preact-cli-entrypoint', init);
addEventListener('async-loading', () => {
inProgress = true;
});

addEventListener('async-loaded', () => {
inProgress = false;
init();
}
});

init(true);
10 changes: 5 additions & 5 deletions src/lib/webpack/async-component-loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,14 @@ module.exports.pitch = function(remainingRequest) {
}

return `
import async from 'preact-cli/async-component';
import Async from 'preact-cli/async-component';

function load(cb) {
require.ensure([], function(require) {
cb(require(${ loaderUtils.stringifyRequest(this, "!!" + remainingRequest) }));
}${name ? (', '+JSON.stringify(name)) : ''});
require.ensure([], function () {
cb( require(${loaderUtils.stringifyRequest(this, '!!' + remainingRequest)}) );
}, ${JSON.stringify(name || '')});
}

export default async(load);
export default Async(load);
`;
};