From 3553530e6e2a26e627a14fc40db0e464881ea7d8 Mon Sep 17 00:00:00 2001 From: Robbie Antenesse Date: Thu, 6 Jun 2019 15:08:00 -0600 Subject: [PATCH] Automate UpUp asset reference via router.php --- README.md | 6 ------ package.json | 6 ++++-- src/php/router.php | 41 +++++++++++++++++++++++++++++++++++++++++ template-index.html | 24 +----------------------- 4 files changed, 46 insertions(+), 31 deletions(-) diff --git a/README.md b/README.md index 6c265c5..7e2c9dc 100644 --- a/README.md +++ b/README.md @@ -25,9 +25,3 @@ It's less useful, but `npm run serve-frontend-only` will bundle and serve _only_ ## Production `npm run bundle` bundles and minifies the frontend stuff and also copies the backend stuff to `dist`. Be sure to run `npm run clear` to delete the contents of `dist` and `.cache` before using `npm run bundle` to make sure you don't get old dev versions of the bundled code included in your upload. - -## UpUp Configuration - -[UpUp](https://github.com/TalAter/UpUp) is a tool that enables browsers to download an offline version of a website so users can still access it if they lose internet connection. Because Parcel Bundler hashes every file accessed via reference within the code, you need to ensure that the UpUp configuration at the bottom of `index.html` is kept up to date whenever you make changes to files. Typically the only file hashes that will change are `src.*.js` and `main.*.css`, but it's best to check all of them just to make sure. - -After bundling, update the files referenced in the configuration to make sure UpUp can download the files correctly, then bundle again so `dist/index.html` gets updated. I'm desperately hoping I can find a way to automate this in the build process, but I haven't figured it out just yet. Maybe I'll end up using `router.php` and `.htaccess` to do the heavy lifting for me. We'll see. \ No newline at end of file diff --git a/package.json b/package.json index c6c1dc4..f9cfe8d 100644 --- a/package.json +++ b/package.json @@ -10,8 +10,10 @@ "start": "concurrently \"npm run watch-js\" \"npm run watch-php\" \"npm run copy-files\"", "watch-js": "parcel watch template-index.html offline.html template-view.html template-passwordreset.html --no-hmr --public-url ./", "watch-php": "cpx \"src/php/**/{*,.*}\" dist -v -w", - "copy-files": "cpx \"node_modules/upup/dist/upup.sw.min.js\" dist -v", - "bundle": "parcel build template-index.html offline.html template-view.html template-passwordreset.html && npm run copy-files && cpx \"src/php/**/{*,.*}\" dist", + "bundle": "npm run bundle-js && npm run copy-files && npm run copy-php", + "bundle-js": "parcel build template-index.html offline.html template-view.html template-passwordreset.html", + "copy-files": "cpx \"node_modules/upup/dist/*.min.js\" dist -v", + "copy-php": "cpx \"src/php/**/{*,.*}\" dist", "serve-frontend-only": "parcel template-index.html", "clear": "npm run clear-dist && npm run clear-cache", "clear-dist": "rimraf dist/*", diff --git a/src/php/router.php b/src/php/router.php index 9f0c774..404dc70 100644 --- a/src/php/router.php +++ b/src/php/router.php @@ -83,6 +83,47 @@ switch ($view) { } } $html = str_replace('{{announcements}}', $announcements_html, $html); + + $upup_files = array( + 'src.js', + 'main.css', + 'help.html', + 'privacy.html', + 'terms.html', + 'usage.html', + 'ipa-table.html', + 'favicon.png', + ); + $files = array_map(function($file_name) use($upup_files) { + foreach($upup_files as $index => $upup_file) { + $file_pieces = explode('.', $upup_file); + if (substr($file_name, 0, strlen($file_pieces[0])) === $file_pieces[0] + && substr($file_name, -strlen($file_pieces[1])) === $file_pieces[1]) { + return str_replace('\\', '/', $file_name); + } + } + return false; + }, scandir('.')); + $files = array_filter($files); + + $upup_insert = " + "; + + $html = str_replace('{{upup_insert}}', $upup_insert, $html); + return Response::html($html); break; } diff --git a/template-index.html b/template-index.html index 2d399b9..26b7bda 100644 --- a/template-index.html +++ b/template-index.html @@ -370,28 +370,6 @@
- - + {{upup_insert}} \ No newline at end of file