Rename js/run.mjs to js/run.js to survive strict MIME checking#217
Merged
Conversation
docs.ruby-lang.org serves .mjs as application/octet-stream (its nginx MIME table predates the mjs entry added in nginx 1.21.4), and browsers enforce strict MIME checking for module scripts, so the RUN button failed to load in production. What makes the script a module is the <script type="module"> attribute, not the file extension, so ship the file as .js, which every MIME table maps to a JavaScript type. The QuickJS test file keeps its .mjs name (qjs needs it to pick module mode for the top-level file); imported files are always compiled as modules, so importing run.js works unchanged. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This was referenced Jul 13, 2026
znz
added a commit
that referenced
this pull request
Jul 13, 2026
RUN ボタンは ruby.wasm をメインスレッドで同期実行しており、無限ループや 長い sleep を含むサンプルではタブの UI が完全に固まっていた(fix #239)。 - 実行を新設の theme/default/js/run-worker.js (module Worker) に移動。 wasm のコンパイルはこれまでどおりメインスレッド側で一度だけ行いキャッシュ し、WebAssembly.Module を実行ごとに使い捨ての Worker へ postMessage で 渡す。VM は実行のたびに作り直すので、terminate() 後の再実行やコンパイル やり直しの問題は生じない - 実行中は RUN ボタンを STOP に切り替え、クリックで Worker.terminate() して中断できるようにした。30 秒(RUN_TIMEOUT_MS)経過でも自動的に terminate し、出力欄にその旨を追記する - $stdout/$stderr を StringIO に溜めて eval 完了後に一括取得していたのを やめ、Worker 内で書き込みのたびに JS.global.call(:postOutput, text) で メインスレッドへ逐次 postMessage するようにした。表示側の 64KB 打ち切り (旧 truncateOutput 相当)は accumulateOutput として維持しつつ、打ち切り 後の追加チャンクは即座に捨てるようにして無限出力ループでも O(1) で済む ようにした - Worker スクリプトの URL は new URL('run-worker.js', import.meta.url) で run.js 自身の URL から解決し、ページの階層やテンプレート側の custom_js_url() の実装に依存しないようにした - run-worker.js も run.js 同様に .mjs ではなく .js 命名にした(#217 と同じ MIME 判定の理由)。statichtml のテーマファイルコピー処理 (copy_run_ruby_wasm_script)を RUN_RUBY_WASM_JS_FILES 経由で2ファイルとも コピーするよう拡張し、片方だけ欠けているテーマも許容する - test/js/test_run.mjs に純粋関数のテスト(accumulateOutput・ STOPPED_NOTE・timeoutNote・run-worker.js の PRELUDE/formatRunError)を 追加。Worker/DOM は QuickJS では扱えないため、テストはこれまでどおり 純粋関数部分のみを対象にしている Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
問題
#216 の RUN ボタンが本番 docs.ruby-lang.org で動きません。ブラウザコンソールに:
原因は配信側の MIME 設定です。
.mjs拡張子が nginx の mime.types に入ったのは 1.21.4(2021-11)からで、本番サーバーの MIME テーブルには
mjsのエントリがなくapplication/octet-streamで配信されます。通常の
<script>なら MIME が多少おかしくても実行されますが、<script type="module">は HTML 仕様で厳格な MIME チェックが必須のため、ロードが拒否されます。
方針
js/run.mjsをjs/run.jsにリネームします。<script type="module">属性であって拡張子ではないため、
.jsにしても ES モジュールとしての動作は不変です.jsはどのサーバーの MIME テーブルでも JavaScript にマップされるため、docs.ruby-lang.org に限らず、statichtml の出力をどこでホストしても
動作します(サーバー側の nginx 設定変更や Fastly のキャッシュパージに
依存しない、bitclust 内で完結する修正です)
application/octet-streamでキャッシュされた旧 URL の影響も受けません
QuickJS テストランナー
test/js/test_run.mjsは.mjsのままです(qjs がトップレベルファイルをモジュールとして実行するのに拡張子が必要。
import されるファイルは常にモジュールとしてコンパイルされるため、
run.jsの import は問題なく動きます)。テスト
test/test_run_ruby_wasm.rb/test/test_statichtml_command.rbをrun.js期待に更新(先に red を確認してからリネーム)qjs test/js/test_run.mjsパスデプロイ
マージ後、generated-documents の daily build で 3.2〜4.1 の全ページが
js/run.js参照で再生成され、copy_run_ruby_wasm_scriptが新ファイルを配置します。ビルド側(doctree Rakefile / generated-documents)は
--run-ruby-wasmフラグしか参照しておらず、ファイル名には依存していないため変更不要です。
🤖 Generated with Claude Code