Skip to content

Commit a7daa0d

Browse files
authored
Merge pull request #2730 from jowlo/feature/set-module-type-for-mjs-script-tags
Load asset files with ending ".mjs" as js modules
2 parents 6b31a8f + 545938b commit a7daa0d

File tree

5 files changed

+59
-3
lines changed

5 files changed

+59
-3
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ This project adheres to [Semantic Versioning](https://semver.org/).
1313
- [#2734](https://github.com/plotly/dash/pull/2734) Configure CI for Python 3.10 [#1863](https://github.com/plotly/dash/issues/1863)
1414
- [#2735](https://github.com/plotly/dash/pull/2735) Configure CI for Python 3.8 and 3.12, drop support for Python 3.6 and Python 3.7 [#2736](https://github.com/plotly/dash/issues/2736)
1515

16+
## Added
17+
- [#2730](https://github.com/plotly/dash/pull/2721) Load script files with `.mjs` ending as js modules
18+
1619
## [2.15.0] - 2024-01-31
1720

1821
## Added

dash/dash.py

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -855,9 +855,20 @@ def _relative_url_path(relative_package_path="", namespace=""):
855855
raise Exception("Serving files from absolute_path isn't supported yet")
856856
elif "asset_path" in resource:
857857
static_url = self.get_asset_url(resource["asset_path"])
858-
# Add a cache-busting query param
859-
static_url += f"?m={resource['ts']}"
860-
srcs.append(static_url)
858+
# Import .mjs files with type=module script tag
859+
if static_url.endswith(".mjs"):
860+
srcs.append(
861+
{
862+
"src": static_url
863+
+ f"?m={resource['ts']}", # Add a cache-busting query param
864+
"type": "module",
865+
}
866+
)
867+
else:
868+
srcs.append(
869+
static_url + f"?m={resource['ts']}"
870+
) # Add a cache-busting query param
871+
861872
return srcs
862873

863874
def _generate_css_dist_html(self):
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import {display} from "./clientsideModule.mjs";
2+
3+
window.dash_clientside = window.dash_clientside || {};
4+
window.dash_clientside.clientside_module = Object.assign({}, window.dash_clientside.clientside_module, {
5+
display
6+
});
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
const display = (value) => 'Client says "' + value + '"';
2+
3+
export {display};

tests/integration/clientside/test_clientside.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -868,3 +868,36 @@ def test_clsd020_clientside_callback_context_triggered_id(dash_duo):
868868
dash_duo.find_element("button[id*='btn1\":2']").click()
869869

870870
dash_duo.wait_for_text_to_equal("#output-clientside", "2")
871+
872+
873+
def test_clsd021_simple_clientside_module_serverside_callback(dash_duo):
874+
app = Dash(__name__, assets_folder="assets")
875+
876+
app.layout = html.Div(
877+
[
878+
dcc.Input(id="input"),
879+
html.Div(id="output-clientside"),
880+
html.Div(id="output-serverside"),
881+
]
882+
)
883+
884+
@app.callback(Output("output-serverside", "children"), [Input("input", "value")])
885+
def update_output(value):
886+
return 'Server says "{}"'.format(value)
887+
888+
app.clientside_callback(
889+
ClientsideFunction(namespace="clientside_module", function_name="display"),
890+
Output("output-clientside", "children"),
891+
[Input("input", "value")],
892+
)
893+
894+
dash_duo.start_server(app)
895+
896+
assert dash_duo.find_element("body > footer > script[type=module]")
897+
898+
dash_duo.wait_for_text_to_equal("#output-serverside", 'Server says "None"')
899+
dash_duo.wait_for_text_to_equal("#output-clientside", 'Client says "undefined"')
900+
901+
dash_duo.find_element("#input").send_keys("hello world")
902+
dash_duo.wait_for_text_to_equal("#output-serverside", 'Server says "hello world"')
903+
dash_duo.wait_for_text_to_equal("#output-clientside", 'Client says "hello world"')

0 commit comments

Comments
 (0)