Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 20 additions & 7 deletions src/Umbraco.Community.BlockPreview.UI/vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,20 @@ import { glob } from 'glob';
import { readFileSync, writeFileSync } from 'fs';
import { resolve } from 'path';

// Use the project version (version.json at the repo root) as the cache-bust token.
// It is bumped for every release, so the ?v= query changes each version and browsers
// re-fetch the entry bundle. Reading public/umbraco-package.json instead left ?v= frozen
// at a stale value, so upgrades served cached JS.
function getVersion(): string {
const versionJsonPath = resolve(__dirname, '../../version.json');
return JSON.parse(readFileSync(versionJsonPath, 'utf-8')).version;
}

function cacheBustImports(): Plugin {
return {
name: 'cache-bust-imports',
generateBundle(_, bundle) {
// Read version from umbraco-package.json
const packageJsonPath = resolve(__dirname, 'public/umbraco-package.json');
const packageJson = JSON.parse(readFileSync(packageJsonPath, 'utf-8'));
const version = packageJson.version;
const version = getVersion();

for (const file of Object.values(bundle)) {
if (file.type === 'chunk' && file.code) {
Expand All @@ -27,13 +33,20 @@ function cacheBustImports(): Plugin {
}
},
writeBundle() {
// Transform umbraco-package.json after it's copied from public/
// Transform umbraco-package.json after it's copied from public/.
// Always (re)set ?v= to the current project version so the entry bundle URL
// changes on every release.
const version = getVersion();
const distPath = resolve(__dirname, '../Umbraco.Community.BlockPreview/wwwroot/App_Plugins/Umbraco.Community.BlockPreview/umbraco-package.json');
const json = JSON.parse(readFileSync(distPath, 'utf-8'));

// Keep the manifest version in sync with version.json (the build's
// UpdatePackageManifestVersion target re-sets this to the same value at pack time).
json.version = version;

for (const ext of json.extensions || []) {
if (ext.js && !ext.js.includes('?v=')) {
ext.js = `${ext.js}?v=${json.version}`;
if (ext.js) {
ext.js = `${ext.js.replace(/\?v=.*$/, '')}?v=${version}`;
}
}

Expand Down

Large diffs are not rendered by default.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading