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

ADD timeout to update check #4530

Merged
merged 2 commits into from
Oct 23, 2018
Merged
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
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)),
]);
Copy link
Member

Choose a reason for hiding this comment

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

Won't the const data = await fromFetch.json(); fail in case of a timeout?

Copy link
Member Author

Choose a reason for hiding this comment

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

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