Skip to content

Commit 95e7afe

Browse files
committed
Record URLs changed in redirect responses as loaded
But the E2E test fails.
1 parent f939f2d commit 95e7afe

File tree

3 files changed

+39
-0
lines changed

3 files changed

+39
-0
lines changed

ext/js/lib/js/require_remote.rb

+9
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ class RequireRemote
1010

1111
def initialize
1212
set_base_url
13+
14+
# The loaded URLs are saved as Ruby strings.
1315
@loaded_urls = Set.new
1416
end
1517

@@ -26,8 +28,15 @@ def load(relative_feature, use_maps: false)
2628
response = JS.global.fetch(location.url).await
2729

2830
if response[:status].to_i == 200
31+
# Check if the redirected URL has already loaded.
32+
return false if @loaded_urls.include?(response[:url].to_s)
33+
2934
code = response.text().await.to_s
3035
eval_code(code, location)
36+
37+
# The redirect that occurred may have been temporary.
38+
# The original URL is not recorded.
39+
# Only the URL after the redirect is recorded.
3140
@loaded_urls << response[:url].to_s
3241
true
3342
else

packages/npm-packages/ruby-wasm-wasi/test-e2e/integrations/browser-script.spec.ts

+22
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,28 @@ if (!process.env.RUBY_NPM_PACKAGE_ROOT) {
9595
expect(await resolve()).toBe(false);
9696
});
9797

98+
test("JS::RequireRemote#load Identify by URL after redirect", async ({
99+
page,
100+
}) => {
101+
// Stop tests immediately when an error occurs in the page.
102+
page.on("pageerror", (error) => {throw error;});
103+
104+
const resolve = await resolveBinding(page, "checkResolved");
105+
await page.goto(
106+
"https://cdn.jsdelivr.net/npm/ruby-head-wasm-wasi@latest/dist/",
107+
);
108+
await page.setContent(`
109+
<script src="browser.script.iife.js"></script>
110+
<script type="text/ruby" data-eval="async">
111+
require 'js/require_remote'
112+
JS::RequireRemote.instance.load 'redirect_to_error_on_load_twice'
113+
JS.global.checkResolved JS::RequireRemote.instance.load 'error_on_load_twice'
114+
</script>
115+
`);
116+
117+
expect(await resolve()).toBe(false);
118+
});
119+
98120
test("JS::RequireRemote#load throws error when gem is not found", async ({
99121
page,
100122
}) => {

packages/npm-packages/ruby-wasm-wasi/test-e2e/support.ts

+8
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ export const setupDebugLog = (context: BrowserContext) => {
1515
console.log("<<", response.status(), response.url()),
1616
);
1717
context.on("console", (msg) => console.log("LOG:", msg.text()));
18+
context.on("weberror", (error) => console.log("ERROR!!!!!:", error.message))
1819
}
1920
};
2021

@@ -36,6 +37,13 @@ export const setupProxy = (context: BrowserContext) => {
3637
ALREADY_LOADED = true`,
3738
contentType: "text/ruby",
3839
});
40+
} else if (relativePath.match("redirect_to_error_on_load_twice.rb")) {
41+
route.fulfill({
42+
status: 302,
43+
headers: {
44+
location: "error_on_load_twice.rb",
45+
},
46+
});
3947
} else if (fs.existsSync(mockedPath)) {
4048
route.fulfill({
4149
path: mockedPath,

0 commit comments

Comments
 (0)