Skip to content

Commit b36684c

Browse files
committed
feat: implement version-specific caching
1 parent 7d20ee3 commit b36684c

File tree

3 files changed

+17
-12
lines changed

3 files changed

+17
-12
lines changed

src/components/FileManager.tsx

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,14 @@ import tutorialStore from 'tutorialkit:store';
33
import { useRef, useEffect } from 'react';
44
import { webcontainer } from 'tutorialkit:core';
55
import type { WebContainer } from '@webcontainer/api';
6+
import { RAILS_WASM_PACKAGE_VERSION } from '../templates/default/lib/constants';
67

78
export function FileManager() {
89
const files = useStore(tutorialStore.files);
910
const processedFiles = useRef(new Set<string>());
1011
const wasmCached = useRef(false);
1112
const cachingInterval = useRef<number | null>(null);
13+
const VERSIONED_RAILS_WASM_FILE_NAME = `rails-${RAILS_WASM_PACKAGE_VERSION}.wasm`;
1214

1315
async function chmodx(wc: WebContainer, path: string) {
1416
const process = await wc.spawn('chmod', ['+x', path]);
@@ -25,8 +27,9 @@ export function FileManager() {
2527
async function fetchCachedWasmFile(): Promise<Uint8Array | null> {
2628
try {
2729
const opfsRoot = await navigator.storage.getDirectory();
28-
const fileHandle = await opfsRoot.getFileHandle('rails.wasm');
30+
const fileHandle = await opfsRoot.getFileHandle(VERSIONED_RAILS_WASM_FILE_NAME);
2931
const file = await fileHandle.getFile();
32+
console.log(`Found cached WASM version ${RAILS_WASM_PACKAGE_VERSION}`);
3033
return new Uint8Array(await file.arrayBuffer());
3134
} catch {
3235
return null;
@@ -36,11 +39,11 @@ export function FileManager() {
3639
async function persistWasmFile(wasmData: Uint8Array): Promise<void> {
3740
try {
3841
const opfsRoot = await navigator.storage.getDirectory();
39-
const fileHandle = await opfsRoot.getFileHandle('rails.wasm', { create: true });
42+
const fileHandle = await opfsRoot.getFileHandle(VERSIONED_RAILS_WASM_FILE_NAME, { create: true });
4043
const writable = await fileHandle.createWritable();
4144
await writable.write(wasmData);
4245
await writable.close();
43-
console.log('Rails WASM cached');
46+
console.log(`Rails WASM v${RAILS_WASM_PACKAGE_VERSION} cached`);
4447
} catch (error) {
4548
console.error('Failed to persist Rails WASM:', error);
4649
}
@@ -49,7 +52,7 @@ export function FileManager() {
4952
async function cacheWasmFile(wc: WebContainer): Promise<void> {
5053
if (cachingInterval.current) return;
5154

52-
console.log('Caching WASM file...');
55+
console.log(`Caching WASM file v${RAILS_WASM_PACKAGE_VERSION}...`);
5356

5457
cachingInterval.current = window.setInterval(async () => {
5558
try {
@@ -76,8 +79,8 @@ export function FileManager() {
7679
if (!wasmCached.current) {
7780
const cachedWasm = await fetchCachedWasmFile();
7881
if (cachedWasm) {
79-
await wc.fs.writeFile('rails.wasm', cachedWasm);
80-
console.log('Rails WASM loaded from cache');
82+
await wc.fs.writeFile(VERSIONED_RAILS_WASM_FILE_NAME, cachedWasm);
83+
console.log(`Rails WASM v${RAILS_WASM_PACKAGE_VERSION} loaded from cache`);
8184
wasmCached.current = true;
8285
} else {
8386
await cacheWasmFile(wc);
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export const RAILS_WASM_PACKAGE_VERSION = '8.0.2-rc.1';

src/templates/default/scripts/preinstall.js

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
import fs from 'fs/promises';
44
import path from 'path';
55
import { execSync } from 'child_process';
6-
7-
const RAILS_WASM_PATH = './rails.wasm';
6+
import { RAILS_WASM_PACKAGE_VERSION } from '../lib/constants.js';
7+
const VERSIONED_RAILS_WASM_PATH = `./rails-${RAILS_WASM_PACKAGE_VERSION}.wasm`;
88
const TARGET_DIR = 'node_modules/@rails-tutorial/wasm/dist';
99
const TARGET_FILE = path.join(TARGET_DIR, 'rails.wasm');
1010

@@ -21,6 +21,7 @@ async function moveFile(src, dest) {
2121
try {
2222
await fs.mkdir(path.dirname(dest), { recursive: true });
2323
await fs.rename(src, dest);
24+
console.log(`✓ Moved ${src} to ${dest}`);
2425
return true;
2526
} catch (error) {
2627
console.error(`✗ Failed to move ${src} to ${dest}:`, error.message);
@@ -41,15 +42,15 @@ async function installPackage(packageName, version) {
4142
}
4243

4344
async function main() {
44-
const railsWasmExists = await checkIfFileExists(RAILS_WASM_PATH);
45+
const versionedWasmExists = await checkIfFileExists(VERSIONED_RAILS_WASM_PATH);
4546

46-
if (railsWasmExists) {
47-
const success = await moveFile(RAILS_WASM_PATH, TARGET_FILE);
47+
if (versionedWasmExists) {
48+
const success = await moveFile(VERSIONED_RAILS_WASM_PATH, TARGET_FILE);
4849
if (!success) {
4950
process.exit(1);
5051
}
5152
} else {
52-
const success = await installPackage('@rails-tutorial/wasm', '8.0.2-pre.2');
53+
const success = await installPackage('@rails-tutorial/wasm', RAILS_WASM_PACKAGE_VERSION);
5354
if (!success) {
5455
process.exit(1);
5556
}

0 commit comments

Comments
 (0)