Skip to content

Commit

Permalink
Merge pull request #4530 from storybooks/tech/smarter-update-check
Browse files Browse the repository at this point in the history
ADD timeout to update check
  • Loading branch information
ndelangen authored Oct 23, 2018
2 parents c3aa283 + c7bf9f2 commit 76616c1
Showing 1 changed file with 13 additions and 5 deletions.
18 changes: 13 additions & 5 deletions lib/core/src/server/build-dev.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,15 +77,19 @@ function applyStatic(app, options) {
}
}

const updateCheck = async () => {
const updateCheck = async version => {
let result;
const time = Date.now();
try {
const fromCache = await cache.get('lastUpdateCheck', { success: false, time: 0 });

// if last check was more then 24h ago
if (time - 86400000 > fromCache.time) {
const fromFetch = await fetch('https://storybooks.netlify.com/versions.json');
const fromFetch = await Promise.race([
fetch(`https://storybook.js.org/versions.json?current=${version}`),
// if fetch is too slow, we won't wait for it
new Promise((res, rej) => global.setTimeout(rej, 1500)),
]);
const data = await fromFetch.json();
result = { success: true, data, time };
await cache.set('lastUpdateCheck', result);
Expand Down Expand Up @@ -140,7 +144,11 @@ export async function buildDevStandalone(options) {

const serverListening = listenToServer(server, listenAddr);

const [stats, updateInfo] = await Promise.all([webpackValid, updateCheck(), serverListening]);
const [stats, updateInfo] = await Promise.all([
webpackValid,
updateCheck(options.packageJson.version),
serverListening,
]);
const proto = options.https ? 'https' : 'http';
const address = `${proto}://${options.host || 'localhost'}:${port}/`;
const networkAddress = `${proto}://${ip.address()}:${port}/`;
Expand Down Expand Up @@ -189,8 +197,8 @@ export async function buildDevStandalone(options) {
});

serveMessage.push(
['Local', chalk.cyan(address)],
['On your network', chalk.cyan(networkAddress)]
['Local:', chalk.cyan(address)],
['On your network:', chalk.cyan(networkAddress)]
);

// eslint-disable-next-line no-console
Expand Down

0 comments on commit 76616c1

Please sign in to comment.