Commits (2)
{
"presets": ["@babel/preset-typescript", "@babel/preset-env"],
"plugins": ["@babel/plugin-proposal-class-properties"]
}
...@@ -5,12 +5,12 @@ ...@@ -5,12 +5,12 @@
"dockerComposeFile": ["../docker-compose.yml", "./docker-compose.yml"], "dockerComposeFile": ["../docker-compose.yml", "./docker-compose.yml"],
"service": "app", "service": "app",
"workspaceFolder": "/castopod-host", "workspaceFolder": "/castopod-host",
"postCreateCommand": "composer install && npm install && npm run build:dev", "postCreateCommand": "composer install && npm install && npm run build:static",
"postStartCommand": "crontab ./crontab && cron && php spark serve --host 0.0.0.0", "postStartCommand": "crontab ./crontab && cron && php spark serve --host 0.0.0.0 & npm run dev",
"postAttachCommand": "crontab ./crontab && service cron reload", "postAttachCommand": "crontab ./crontab && service cron reload",
"shutdownAction": "stopCompose", "shutdownAction": "stopCompose",
"settings": { "settings": {
"terminal.integrated.defaultProfile.linux": "/bin/bash", "terminal.integrated.defaultProfile.linux": "bash",
"editor.formatOnSave": true, "editor.formatOnSave": true,
"[php]": { "[php]": {
"editor.defaultFormatter": "bmewburn.vscode-intelephense-client", "editor.defaultFormatter": "bmewburn.vscode-intelephense-client",
......
# [1.0.0-alpha.63](https://code.podlibre.org/podlibre/castopod-host/compare/v1.0.0-alpha.62...v1.0.0-alpha.63) (2021-07-12)
### Features
- build hashed static files to renew browser cache
([37c54d2](https://code.podlibre.org/podlibre/castopod-host/commit/37c54d247749bdf8f528babd4a78f24d48051063)),
closes [#107](https://code.podlibre.org/podlibre/castopod-host/issues/107)
# [1.0.0-alpha.62](https://code.podlibre.org/podlibre/castopod-host/compare/v1.0.0-alpha.61...v1.0.0-alpha.62) (2021-07-02) # [1.0.0-alpha.62](https://code.podlibre.org/podlibre/castopod-host/compare/v1.0.0-alpha.61...v1.0.0-alpha.62) (2021-07-02)
### Bug Fixes ### Bug Fixes
......
...@@ -47,7 +47,8 @@ performance improvements ⚡. ...@@ -47,7 +47,8 @@ performance improvements ⚡.
- cf. - cf.
[I haven't updated my instance in a long time… What should I do?](#i-havent-updated-my-instance-in-a-long-time-what-should-i-do) [I haven't updated my instance in a long time… What should I do?](#i-havent-updated-my-instance-in-a-long-time-what-should-i-do)
5. ✨ Enjoy your fresh instance, you're all done! 5. If you are using redis, clear your cache.
6. ✨ Enjoy your fresh instance, you're all done!
## Automatic update instructions ## Automatic update instructions
......
...@@ -11,7 +11,7 @@ declare(strict_types=1); ...@@ -11,7 +11,7 @@ declare(strict_types=1);
| |
| NOTE: this constant is updated upon release with Continuous Integration. | NOTE: this constant is updated upon release with Continuous Integration.
*/ */
defined('CP_VERSION') || define('CP_VERSION', '1.0.0-alpha.62'); defined('CP_VERSION') || define('CP_VERSION', '1.0.0-alpha.63');
/* /*
| -------------------------------------------------------------------- | --------------------------------------------------------------------
......
...@@ -10,6 +10,7 @@ use App\Authorization\PermissionModel; ...@@ -10,6 +10,7 @@ use App\Authorization\PermissionModel;
use App\Libraries\Breadcrumb; use App\Libraries\Breadcrumb;
use App\Libraries\Negotiate; use App\Libraries\Negotiate;
use App\Libraries\Router; use App\Libraries\Router;
use App\Libraries\Vite;
use App\Models\UserModel; use App\Models\UserModel;
use CodeIgniter\Config\BaseService; use CodeIgniter\Config\BaseService;
use CodeIgniter\HTTP\Request; use CodeIgniter\HTTP\Request;
...@@ -138,4 +139,13 @@ class Services extends BaseService ...@@ -138,4 +139,13 @@ class Services extends BaseService
return new Breadcrumb(); return new Breadcrumb();
} }
public static function vite(bool $getShared = true): Vite
{
if ($getShared) {
return self::getSharedInstance('vite');
}
return new Vite();
}
} }
<?php
declare(strict_types=1);
namespace App\Libraries;
class Vite
{
protected string $manifestPath = 'assets/manifest.json';
protected string $manifestCSSPath = 'assets/manifest-css.json';
/**
* @var array<string, mixed>
*/
protected ?array $manifestData = null;
/**
* @var array<string, mixed>
*/
protected ?array $manifestCSSData = null;
public function asset(string $path, string $type): string
{
if (ENVIRONMENT !== 'production') {
return $this->loadDev($path, $type);
}
// @phpstan-ignore-next-line
return $this->loadProd($path, $type);
}
private function loadDev(string $path, string $type): string
{
return $this->getHtmlTag("http://localhost:3000/assets/{$path}", $type);
}
private function loadProd(string $path, string $type): string
{
if ($type === 'css') {
if ($this->manifestCSSData === null) {
$cacheName = 'vite-manifest-css';
if (! ($cachedManifestCSS = cache($cacheName))) {
if (($manifestCSSContent = file_get_contents($this->manifestCSSPath)) !== false) {
$cachedManifestCSS = json_decode($manifestCSSContent, true);
cache()
->save($cacheName, $cachedManifestCSS, DECADE);
} else {
// ERROR when getting the manifest-css file
$manifestCSSPath = $this->manifestCSSPath;
die("Could not load Vite's <pre>{$manifestCSSPath}</pre> file.");
}
}
$this->manifestCSSData = $cachedManifestCSS;
}
if (array_key_exists($path, $this->manifestCSSData)) {
return $this->getHtmlTag('/assets/' . $this->manifestCSSData[$path]['file'], 'css');
}
}
if ($this->manifestData === null) {
$cacheName = 'vite-manifest';
if (! ($cachedManifest = cache($cacheName))) {
if (($manifestContents = file_get_contents($this->manifestPath)) !== false) {
$cachedManifest = json_decode($manifestContents, true);
cache()
->save($cacheName, $cachedManifest, DECADE);
} else {
// ERROR when retrieving the manifest file
$manifestPath = $this->manifestPath;
die("Could not load Vite's <pre>{$manifestPath}</pre> file.");
}
}
$this->manifestData = $cachedManifest;
}
$html = '';
if (array_key_exists($path, $this->manifestData)) {
$manifestElement = $this->manifestData[$path];
// import css dependencies if any
if (array_key_exists('css', $manifestElement)) {
foreach ($manifestElement['css'] as $cssFile) {
$html .= $this->getHtmlTag('/assets/' . $cssFile, 'css');
}
}
// import dependencies first for faster js loading
if (array_key_exists('imports', $manifestElement)) {
foreach ($manifestElement['imports'] as $importPath) {
if (array_key_exists($importPath, $this->manifestData)) {
$html .= $this->getHtmlTag('/assets/' . $this->manifestData[$importPath]['file'], 'js');
}
}
}
$html .= $this->getHtmlTag('/assets/' . $manifestElement['file'], $type);
}
return $html;
}
private function getHtmlTag(string $assetUrl, string $type): string
{
return match ($type) {
'css' => <<<CODE_SAMPLE
<link rel="stylesheet" href="{$assetUrl}"/>
CODE_SAMPLE
,
'js' => <<<CODE_SAMPLE
<script type="module" src="{$assetUrl}"></script>
CODE_SAMPLE
,
default => '',
};
}
}