Skip to content

Conversation

@Bakaface
Copy link
Collaborator

@Bakaface Bakaface commented Jun 25, 2025

This PR implements the caching of rails.wasm in OPFS. The algorithm is the following:

  • In template (webcontainer environment):

    • If ./rails-<package_version>.wasm file is not present, install @rails-tutorial/wasm package.
    • Else, move the file into node_modules/@rails-tutorial/wasm/dist/rails.wasm.
  • In astro (browser environment):

    • If rails-<package_version>.wasm is present in OPFS, read the file and write it to webcontainer filesystem as rails-<package_version>.wasm.
    • Else, start the caching process:
      • Poll every 1 second for /node_modules/@rails-tutorial/wasm/dist/rails.wasm in webcontainer
      • Once the file is available, read it from webcontainer
      • Persist it to OPFS as rails-<package_version>.wasm for future use

Use the following script to clear the cache during testing:

(async () => {
	try {
		const opfsRoot = await navigator.storage.getDirectory();
		await opfsRoot.removeEntry('rails-8.0.2-rc.1.wasm');
	} catch (error) {
		console.log('No cached file to clear or error:', error);
	}
})();

Resolves #9

@Bakaface Bakaface requested a review from palkan June 26, 2025 00:40
@Bakaface Bakaface marked this pull request as ready for review June 26, 2025 00:40
@palkan
Copy link
Member

palkan commented Jun 28, 2025

Amazing!

Checked locally—no issues! Let's see how it does in production.

@palkan palkan merged commit 24a4bd8 into master Jun 28, 2025
@palkan palkan deleted the bakaface/cache-wasm-module-mount branch June 28, 2025 00:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Cache ruby.wasm

3 participants