Skip to content

Commit

Permalink
Added cache invalidation when the playground have a new build. (#316)
Browse files Browse the repository at this point in the history
* Added cache invalidation when the playground have a new build.

* Be more defensive when setting the cache date.

* Update src/Draco.Editor.Web/app/ts/cache.ts

Co-authored-by: LPeter1997 <[email protected]>

* Update cache.ts

---------

Co-authored-by: LPeter1997 <[email protected]>
  • Loading branch information
Kuinox and LPeter1997 authored Sep 2, 2023
1 parent ab43ddf commit 3a759b5
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 8 deletions.
9 changes: 8 additions & 1 deletion src/Draco.Editor.Web/app/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import JSON5 from 'json5';
import YAML from 'yaml';
import { defineConfig, build as viteBuild } from 'vite';
import { convertTheme } from './theme-converter.js';
import { esbuildPluginVersionInjector } from 'esbuild-plugin-version-injector';
// This file manage the build process of the webapp.

const distDir = '../wwwroot';
Expand Down Expand Up @@ -84,7 +85,13 @@ await build({
'.png': 'dataurl'
},
inject: ['ts/process.ts'],
plugins: [wasmPlugin],
plugins: [wasmPlugin,
esbuildPluginVersionInjector(
{
versionOrCurrentDate: 'current-date',
filter: /metadata.ts$/,
})
],
external: ['dotnet.wasm']
});

Expand Down
39 changes: 39 additions & 0 deletions src/Draco.Editor.Web/app/package-lock.json

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

1 change: 1 addition & 0 deletions src/Draco.Editor.Web/app/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
"@typescript-eslint/eslint-plugin": "^5.42.1",
"@typescript-eslint/parser": "^5.42.1",
"esbuild": "^0.15.13",
"esbuild-plugin-version-injector": "^1.2.0",
"eslint": "^8.27.0",
"json5": "^2.2.1",
"node-ts": "^5.1.2",
Expand Down
30 changes: 23 additions & 7 deletions src/Draco.Editor.Web/app/ts/cache.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { blobToBase64 } from './helpers.js';
import { buildDate } from './metadata.js';

const elements = [];

Expand All @@ -15,8 +16,23 @@ export function getDownloadViewElement() {
}

export async function downloadAssemblies(cfg: unknown) {
try {
const cache = await caches.open('assembly-cache');
const result = await cache.match('appBuildDate');
const cachedDate = await result.text();
console.log(`Current build: ${buildDate} cache build: ${cachedDate}`);

if (result == null || cachedDate != buildDate) {
console.log('Cache nuked.');
await caches.delete('assembly-cache');
const newCache = await caches.open('assembly-cache');
await newCache.put('appBuildDate', new Response(buildDate));
}
} catch (e) {
console.log('Could not open cache: ', e);
}
const assets = cfg['assets'];
if (assets != null) {
if (assets != null) {
const promises = assets.map(async (asset) => {
if (asset['buffer'] == null) {
await downloadAssembly(cfg['assemblyRootFolder'], asset);
Expand All @@ -40,9 +56,9 @@ async function downloadAssembly(dlPath: string, asset: unknown): Promise<void> {
} catch (e) {
console.log('Could not open cache: ', e);
}
console.log('Cache miss:'+dlPath);
console.log(`Cache miss: ${dlPath}`);
setDownloadViewVisible(true);
const progresses = elements.map( (element) => {
const progresses = elements.map((element) => {
const progressContainer = document.createElement('div');
const progressText = document.createElement('span');
progressText.classList.add('monaco-editor');
Expand All @@ -55,7 +71,7 @@ async function downloadAssembly(dlPath: string, asset: unknown): Promise<void> {
return progress;
});
const assemblyBlob = await progressFetch(dlPath + '/' + asset['name'], (loaded, total) => {
progresses.forEach(progress=> {
progresses.forEach(progress => {
progress.max = total;
progress.value = loaded;
});
Expand All @@ -66,7 +82,7 @@ async function downloadAssembly(dlPath: string, asset: unknown): Promise<void> {
if (cache != null) {
await cache.put(asset['name'], response);
}
progresses.forEach(progress=> {
progresses.forEach(progress => {
progress.parentElement.remove();
});
}
Expand All @@ -75,7 +91,7 @@ function wait(milliseconds) {
return new Promise(resolve => setTimeout(resolve, milliseconds));
}

async function progressFetch(url: string, onProgress: (loaded: number, total: number) => void) : Promise<Blob> {
async function progressFetch(url: string, onProgress: (loaded: number, total: number) => void): Promise<Blob> {
const response = await fetch(url);
const contentLength = response.headers.get('content-length');
const total = parseInt(contentLength, 10);
Expand All @@ -85,7 +101,7 @@ async function progressFetch(url: string, onProgress: (loaded: number, total: nu
async start(controller) {
const reader = response.body.getReader();
while (true) {
const {done, value} = await reader.read();
const { done, value } = await reader.read();
if (done) break;
loaded += value.byteLength;
onProgress(loaded, total);
Expand Down
7 changes: 7 additions & 0 deletions src/Draco.Editor.Web/app/ts/metadata.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
/**
* The current version that you are currently using.
*
* Note to developers: This needs to explicitly be `string` so it is not typed as a "const string" that gets injected by esbuild
*/
// eslint-disable-next-line @typescript-eslint/no-inferrable-types
export const buildDate: string = '[VI]{{inject}}[/VI]';

0 comments on commit 3a759b5

Please sign in to comment.