From c9f75f70d1c0a5221c4b72efe485ba4f155d5270 Mon Sep 17 00:00:00 2001 From: Pig Fang Date: Mon, 8 Apr 2019 14:42:40 +0800 Subject: [PATCH] Defer checking updates --- resources/assets/src/scripts/check-updates.ts | 20 +++++++++++++------ resources/assets/src/scripts/index.ts | 1 - .../tests/scripts/check-updates.test.ts | 4 ++-- resources/views/admin/master.blade.php | 9 +-------- webpack.config.js | 1 + 5 files changed, 18 insertions(+), 17 deletions(-) diff --git a/resources/assets/src/scripts/check-updates.ts b/resources/assets/src/scripts/check-updates.ts index 58c55f31..835a8bc2 100644 --- a/resources/assets/src/scripts/check-updates.ts +++ b/resources/assets/src/scripts/check-updates.ts @@ -1,4 +1,11 @@ -import { init } from './net' +// KEEP THIS FILE DEPENDENCIES FREE! + +const init: RequestInit = { + credentials: 'same-origin', + headers: new Headers({ + Accept: 'application/json', + }), +} export async function checkForUpdates(): Promise { const response = await fetch(`${blessing.base_url}/admin/update/check`, init) @@ -9,7 +16,7 @@ export async function checkForUpdates(): Promise { if (data.available && el) { el.innerHTML += ` - v${data.latest} + 1 ` } } @@ -30,7 +37,8 @@ export async function checkForPluginUpdates(): Promise { } } -Object.assign(window, { - checkForUpdates, - checkForPluginUpdates, -}) +// istanbul ignore next +if (process.env.NODE_ENV !== 'test') { + checkForUpdates() + checkForPluginUpdates() +} diff --git a/resources/assets/src/scripts/index.ts b/resources/assets/src/scripts/index.ts index 497c97db..4eea5782 100644 --- a/resources/assets/src/scripts/index.ts +++ b/resources/assets/src/scripts/index.ts @@ -12,4 +12,3 @@ import './net' import './event' import './element' import './logout' -import './check-updates' diff --git a/resources/assets/tests/scripts/check-updates.test.ts b/resources/assets/tests/scripts/check-updates.test.ts index d3d71efb..19ddcd07 100644 --- a/resources/assets/tests/scripts/check-updates.test.ts +++ b/resources/assets/tests/scripts/check-updates.test.ts @@ -13,7 +13,7 @@ test('check for BS updates', async () => { }) .mockResolvedValueOnce({ ok: true, - json: () => Promise.resolve({ available: true, latest: '4.0.0' }), + json: () => Promise.resolve({ available: true }), }) document.body.innerHTML = '' @@ -26,7 +26,7 @@ test('check for BS updates', async () => { expect(document.querySelector('a')!.innerHTML).toBe('') await checkForUpdates() - expect(document.querySelector('a')!.innerHTML).toContain('4.0.0') + expect(document.querySelector('a')!.innerHTML).toContain('1') }) test('check for plugins updates', async () => { diff --git a/resources/views/admin/master.blade.php b/resources/views/admin/master.blade.php index aa4ff3c8..38d6b104 100644 --- a/resources/views/admin/master.blade.php +++ b/resources/views/admin/master.blade.php @@ -83,14 +83,7 @@ @include('common.dependencies.script') - - - + @yield('script') diff --git a/webpack.config.js b/webpack.config.js index fb62975a..019f6d75 100644 --- a/webpack.config.js +++ b/webpack.config.js @@ -13,6 +13,7 @@ const config = { mode: devMode ? 'development' : 'production', entry: { index: './resources/assets/src/index.js', + 'check-updates': './resources/assets/src/scripts/check-updates.ts', style: [ 'bootstrap/dist/css/bootstrap.min.css', 'admin-lte/dist/css/alt/AdminLTE-without-plugins.min.css',