@@ -3,12 +3,14 @@ import tutorialStore from 'tutorialkit:store';
33import { useRef , useEffect } from 'react' ;
44import { webcontainer } from 'tutorialkit:core' ;
55import type { WebContainer } from '@webcontainer/api' ;
6+ import { RAILS_WASM_PACKAGE_VERSION } from '../templates/default/lib/constants' ;
67
78export 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 ) ;
0 commit comments