Replace the CACHE_BUST in the init with a gulp-managed one so we can update the value with gulp watch

This commit is contained in:
Kevin MacMartin 2020-04-24 22:09:23 -04:00
parent a063aaa1a1
commit 65bc4d8f4b
10 changed files with 70 additions and 29 deletions

View file

@ -9,8 +9,6 @@ APP_URL=http://localhost
LOG_CHANNEL=stack LOG_CHANNEL=stack
CACHE_BUST=
BS_HOST=localhost BS_HOST=localhost
DB_CONNECTION=mysql DB_CONNECTION=mysql

30
app/Utilities/Version.php Normal file
View file

@ -0,0 +1,30 @@
<?php
namespace App\Utilities;
class Version
{
/**
* The version file
*
* @var string
*/
private static $version_file_path = '/storage/app/__version.txt';
/**
* Returns the current version (or 0 if none is set)
*
* @return string
*/
public static function get()
{
$full_version_file_path = base_path() . self::$version_file_path;
$version = '0';
if (file_exists($full_version_file_path)) {
$version = trim(file_get_contents($full_version_file_path));
}
return $version;
}
}

View file

@ -239,6 +239,7 @@ return [
*/ */
'Image' => Intervention\Image\Facades\Image::class, 'Image' => Intervention\Image\Facades\Image::class,
'Language' => App\Utilities\Language::class, 'Language' => App\Utilities\Language::class,
'Version' => App\Utilities\Version::class,
] ]

25
gulpfile.js vendored
View file

@ -4,7 +4,9 @@ const gulp = require("gulp"),
log = require("fancy-log"), log = require("fancy-log"),
insert = require("gulp-insert"), insert = require("gulp-insert"),
plumber = require("gulp-plumber"), plumber = require("gulp-plumber"),
concat = require("gulp-concat"); concat = require("gulp-concat"),
fs = require("fs"),
crypto = require("crypto");
// Sass and CSS packages // Sass and CSS packages
const sass = require("gulp-sass"), const sass = require("gulp-sass"),
@ -176,6 +178,17 @@ function processJavaScript(outputFilename, inputFiles, es6) {
return javascript.pipe(gulp.dest("public/js/")); return javascript.pipe(gulp.dest("public/js/"));
} }
// Update the version string
function updateVersion() {
crypto.randomBytes(16, (err, buf) => {
if (err) { throw err; }
return fs.writeFile("storage/app/__version.txt", buf.toString("hex"), (err) => {
if (err) { throw err; }
});
});
}
// Task for error page styles // Task for error page styles
gulp.task("sass-error", () => { gulp.task("sass-error", () => {
return processSass("error"); return processSass("error");
@ -225,9 +238,16 @@ gulp.task("fonts", (done) => {
done(); done();
}); });
// Task to update the cache-bust version
gulp.task("version", (done) => {
updateVersion();
done();
});
// Task to watch files and run respective tasks when changes occur // Task to watch files and run respective tasks when changes occur
gulp.task("watch", () => { gulp.task("watch", () => {
const browserSyncReload = (done) => { const browserSyncReload = (done) => {
updateVersion();
browserSync.reload(); browserSync.reload();
done(); done();
}; };
@ -260,5 +280,6 @@ gulp.task("default", gulp.parallel(
"js-public-libs", "js-public-libs",
"js-dashboard", "js-dashboard",
"js-dashboard-libs", "js-dashboard-libs",
"fonts" "fonts",
"version"
)); ));

View file

@ -81,14 +81,6 @@ php artisan route:clear
msg "Running: ${c_m}php artisan view:clear" msg "Running: ${c_m}php artisan view:clear"
php artisan view:clear php artisan view:clear
grep -qe '^CACHE_BUST=' .env || {
msg "Adding the ${c_y}CACHE_BUST$c_w variable"
printf '\n%s\n' 'CACHE_BUST=' >> .env
}
msg "Updating ${c_y}CACHE_BUST$c_w variable"
sed -i 's|^CACHE_BUST=.*|CACHE_BUST='"$(LC_CTYPE=C LANG=C tr -dc A-Za-z0-9 </dev/urandom | head -c 32)"'|' .env
(( ! no_db )) && { (( ! no_db )) && {
msg "Running: ${c_m}php artisan migrate --force" msg "Running: ${c_m}php artisan migrate --force"
php artisan migrate --force || error "${c_m}php artisan migrate --force$c_w exited with an error status" php artisan migrate --force || error "${c_m}php artisan migrate --force$c_w exited with an error status"

View file

@ -37,11 +37,10 @@ The following steps are performed in this order when run:
4. Downloads and updates non-development composer dependencies. 4. Downloads and updates non-development composer dependencies.
5. Checks to see if the `APP_KEY` variable in the `.env` file is empty, and if it is, generates a value for it. 5. Checks to see if the `APP_KEY` variable in the `.env` file is empty, and if it is, generates a value for it.
6. Clears the route and blade cache to ensure everything will be build fresh against the current codebase and dependencies. 6. Clears the route and blade cache to ensure everything will be build fresh against the current codebase and dependencies.
7. Updates the `CACHE_BUST` variable in the `.env` file, which changes the value of a `version` query string appended to compiled assets and prevents clients from using a previous version in their cache. 7. (artisan) Run new database migrations.
8. (artisan) Run new database migrations. 8. Cleans, downloads and updates npm dependencies.
9. Cleans, downloads and updates npm dependencies. 9. Runs `gulp --production` to build project files and copy fonts to `public/fonts` (uses the local version of gulp installed in `node_modules`).
10. Runs `gulp --production` to build project files and copy fonts to `public/fonts` (uses the local version of gulp installed in `node_modules`). 10. (artisan) Takes the website out of maintenance mode.
11. (artisan) Takes the website out of maintenance mode.
**NOTE**: Items with `(artisan)` prepended to them won't be run if `init.sh` is run with the `--no-artisan` flag. **NOTE**: Items with `(artisan)` prepended to them won't be run if `init.sh` is run with the `--no-artisan` flag.

View file

@ -27,9 +27,9 @@
<meta name="twitter:description" content="{{ env('APP_DESC') }}" /> <meta name="twitter:description" content="{{ env('APP_DESC') }}" />
<meta name="twitter:image" content="{{ asset('/img/logo.png') }}" /> <meta name="twitter:image" content="{{ asset('/img/logo.png') }}" />
<link rel="shortcut icon" href="{{ URL::to('/') }}/favicon.ico?version={{ env('CACHE_BUST') }}" /> <link rel="shortcut icon" href="{{ URL::to('/') }}/favicon.ico?version={{ Version::get() }}" />
<link rel="icon" href="{{ URL::to('/') }}/favicon.ico?version={{ env('CACHE_BUST') }}" type="image/x-icon" /> <link rel="icon" href="{{ URL::to('/') }}/favicon.ico?version={{ Version::get() }}" type="image/x-icon" />
<link rel="icon" href="{{ URL::to('/') }}/favicon.png?version={{ env('CACHE_BUST') }}" type="image/png" /> <link rel="icon" href="{{ URL::to('/') }}/favicon.png?version={{ Version::get() }}" type="image/png" />
<link rel="canonical" href="{{ Request::url() }}" /> <link rel="canonical" href="{{ Request::url() }}" />
@yield('page-includes') @yield('page-includes')
@ -50,7 +50,7 @@
@if(Config::get('app.debug') && Config::get('app.env') === 'local') @if(Config::get('app.debug') && Config::get('app.env') === 'local')
<script id="__bs_script__">//<![CDATA[ <script id="__bs_script__">//<![CDATA[
document.write("<script async src='http://{{ env('BS_HOST', 'localhost') }}:3000/browser-sync/browser-sync-client.js?version={{ env('CACHE_BUST') }}'><\/script>".replace("HOST", location.hostname)); document.write("<script async src='http://{{ env('BS_HOST', 'localhost') }}:3000/browser-sync/browser-sync-client.js?version={{ Version::get() }}'><\/script>".replace("HOST", location.hostname));
//]]></script> //]]></script>
@endif @endif
</body> </body>

View file

@ -2,10 +2,10 @@
@set('current_page', preg_replace([ '/https?:\/\//', '/^[^\/]*\/[^\/]*\//', '/\/.*/' ], [ '', '', '' ], Request::url())) @set('current_page', preg_replace([ '/https?:\/\//', '/^[^\/]*\/[^\/]*\//', '/\/.*/' ], [ '', '', '' ], Request::url()))
@section('page-includes') @section('page-includes')
<script src="/js/lib-dashboard.js?version={{ env('CACHE_BUST') }}"></script> <script src="/js/lib-dashboard.js?version={{ Version::get() }}"></script>
<script src="/js/dashboard.js?version={{ env('CACHE_BUST') }}"></script> <script src="/js/dashboard.js?version={{ Version::get() }}"></script>
<link rel="stylesheet" href="/css/lib-dashboard.css?version={{ env('CACHE_BUST') }}" /> <link rel="stylesheet" href="/css/lib-dashboard.css?version={{ Version::get() }}" />
<link rel="stylesheet" href="/css/dashboard.css?version={{ env('CACHE_BUST') }}" /> <link rel="stylesheet" href="/css/dashboard.css?version={{ Version::get() }}" />
@endsection @endsection
@section('page-top') @section('page-top')

View file

@ -1,7 +1,7 @@
@extends('templates.base') @extends('templates.base')
@section('page-includes') @section('page-includes')
<link rel="stylesheet" href="/css/error.css?version={{ env('CACHE_BUST') }}" /> <link rel="stylesheet" href="/css/error.css?version={{ Version::get() }}" />
@endsection @endsection
@section('page-content') @section('page-content')

View file

@ -1,8 +1,8 @@
@extends('templates.base') @extends('templates.base')
@section('page-includes') @section('page-includes')
<script src="/js/lib.js?version={{ env('CACHE_BUST') }}"></script> <script src="/js/lib.js?version={{ Version::get() }}"></script>
<link rel="stylesheet" href="/css/app.css?version={{ env('CACHE_BUST') }}" /> <link rel="stylesheet" href="/css/app.css?version={{ Version::get() }}" />
<script> <script>
var env = { var env = {
@ -34,5 +34,5 @@
@endsection @endsection
@section('page-bottom') @section('page-bottom')
<script src="/js/app.js?version={{ env('CACHE_BUST') }}"></script> <script src="/js/app.js?version={{ Version::get() }}"></script>
@endsection @endsection