From 6f68c0d65c68bcf22fad297cf10903210d680543 Mon Sep 17 00:00:00 2001 From: OutdatedGuy Date: Mon, 15 May 2023 04:34:52 +0530 Subject: [PATCH] perf: using inline css & js in index.html to decrease load time (#548) * perf: using inline css & js to decrease load time * refactor: remove unused constant variables (cherry picked from commit b996049c3853d0dfe1111636a17c74495abccc60) --- lib/constants.dart | 2 - lib/templates.dart | 123 +++++++++++++++++++++++---------------------- lib/web.dart | 82 +++++++++++++++++------------- 3 files changed, 109 insertions(+), 98 deletions(-) diff --git a/lib/constants.dart b/lib/constants.dart index 16911a6..314b89e 100644 --- a/lib/constants.dart +++ b/lib/constants.dart @@ -5,5 +5,3 @@ const String _webFolder = 'web/'; const String _webSplashFolder = '${_webFolder}splash/'; const String _webSplashImagesFolder = '${_webSplashFolder}img/'; const String _webIndex = '${_webFolder}index.html'; -const String _webRelativeStyleFile = 'splash/style.css'; -const String _webRelativeJSFile = 'splash/splash.js'; diff --git a/lib/templates.dart b/lib/templates.dart index 6908567..a19537d 100644 --- a/lib/templates.dart +++ b/lib/templates.dart @@ -406,73 +406,74 @@ const String _iOSBrandingRightBottomConstraints = ''' /// Web related templates const String _webCss = ''' -html { - height: 100% -} + \n'; + + // Add css as an inline style in head tag + final webIndex = File(_webIndex); + final document = html_parser.parse(webIndex.readAsStringSync()); + + // Update splash css style tag + document.head + ?..querySelector('style#splash-screen-style')?.remove() + ..append( + html_parser.parseFragment(cssContent, container: ''), + ); + + // Write the updated index.html + webIndex.writeAsStringSync(document.outerHtml); } void _createSplashJs() { - final file = File(_webFolder + _webRelativeJSFile); - file.createSync(recursive: true); - file.writeAsStringSync(_webJS); + // Add js as an inline script in head tag + final webIndex = File(_webIndex); + final document = html_parser.parse(webIndex.readAsStringSync()); + + // Update splash js script tag + document.head + ?..querySelector('script#splash-screen-script')?.remove() + ..append( + html_parser.parseFragment(_webJS, container: ''), + ); + + // Write the updated index.html + webIndex.writeAsStringSync(document.outerHtml); } void _updateHtml({ @@ -315,16 +337,12 @@ void _updateHtml({ final webIndex = File(_webIndex); final document = html_parser.parse(webIndex.readAsStringSync()); - // Add style sheet if it doesn't exist - document.querySelector( + // Remove previously used style sheet (migrating to inline style) + document + .querySelector( 'link[rel="stylesheet"][type="text/css"][href="splash/style.css"]', - ) ?? - document.head?.append( - html_parser.parseFragment( - ' \n', - container: '', - ), - ); + ) + ?.remove(); // Add meta viewport if it doesn't exist document.querySelector( @@ -337,31 +355,25 @@ void _updateHtml({ ), ); - // Add javascript if it doesn't exist - document.querySelector( + // Remove previously used src script tag (migrating to inline script) + document + .querySelector( 'script[src="splash/splash.js"]', - ) ?? - document.head?.append( - html_parser.parseFragment( - ' \n', - container: '', - ), - ); + ) + ?.remove(); // Update splash image document.querySelector('picture#splash')?.remove(); if (imagePath != null) { document.body?.insertBefore( html_parser.parseFragment( - _indexHtmlPicture - .replaceAll( + '\n${_indexHtmlPicture.replaceAll( '[IMAGEMODE]', imageMode, - ) - .replaceAll( + ).replaceAll( '[IMAGEEXTENSION]', imagePath.endsWith('.gif') ? 'gif' : 'png', - ), + )}', container: '', ), document.body?.firstChild, @@ -373,15 +385,13 @@ void _updateHtml({ if (brandingImagePath != null) { document.body?.insertBefore( html_parser.parseFragment( - _indexHtmlBrandingPicture - .replaceAll( + '\n${_indexHtmlBrandingPicture.replaceAll( '[BRANDINGMODE]', brandingMode, - ) - .replaceAll( + ).replaceAll( '[BRANDINGEXTENSION]', brandingImagePath.endsWith('.gif') ? 'gif' : 'png', - ), + )}', container: '', ), document.body?.firstChild,