Skip to content

Commit e4ec71d

Browse files
committed
Only wrap JavaScript with the module wrapper under esm_
1 parent 5a3f38c commit e4ec71d

2 files changed

Lines changed: 14 additions & 2 deletions

File tree

‎tests/unit/viahtml/patch_test.py‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,14 @@ def test_it_reports_modules_as_such(self):
211211

212212
assert _resolve_text_type(response, "js") == ESM_TEXT_TYPE
213213

214+
@pytest.mark.parametrize("text_type", ["css", "html", None])
215+
def test_it_leaves_non_js_under_the_module_modifier_to_pywb(self, text_type):
216+
# A module resolves assets relative to `import.meta.url`, which carries
217+
# the module modifier, so CSS and images arrive under it too.
218+
response = self.response_for(f"{ESM_MOD}/https://example.com/style.css")
219+
220+
assert _resolve_text_type(response, text_type) == text_type
221+
214222
def test_it_leaves_everything_else_to_pywb(self):
215223
response = self.response_for("js_/https://example.com/app.js")
216224

‎viahtml/patch.py‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,10 +96,14 @@ def _resolve_text_type(self, text_type):
9696
module is left to the original.
9797
"""
9898

99-
if self.url_rewriter.wburl.mod == ESM_MOD:
99+
text_type = _PYWB_RESOLVE_TEXT_TYPE(self, text_type)
100+
101+
# Only JS gets the module wrapper: assets a module resolves relative to
102+
# `import.meta.url` (CSS, images) arrive under the module modifier too.
103+
if text_type == "js" and self.url_rewriter.wburl.mod == ESM_MOD:
100104
return ESM_TEXT_TYPE
101105

102-
return _PYWB_RESOLVE_TEXT_TYPE(self, text_type)
106+
return text_type
103107

104108

105109
def _patch_esm_rewriting():

0 commit comments

Comments
 (0)