Skip to content

Commit

Permalink
Fixes #420 - Switch to staticfiles_storage.url() to reference JS files
Browse files Browse the repository at this point in the history
  • Loading branch information
claudep authored Dec 7, 2023
1 parent dbcc930 commit 2c254ae
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 15 deletions.
10 changes: 4 additions & 6 deletions tinymce/compressor.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,10 @@ def gzip_compressor(request):
response = HttpResponse()
response["Content-Type"] = "text/javascript"

js_url = tinymce.settings.get_js_url()
js_base_url = js_url[: js_url.rfind("/")]
if not isJS:
response.write(
render_to_string(
"tinymce/tiny_mce_gzip.js", {"base_url": tinymce.settings.JS_BASE_URL}
)
)
response.write(render_to_string("tinymce/tiny_mce_gzip.js", {"base_url": js_base_url}))
return response

patch_vary_headers(response, ["Accept-Encoding"])
Expand Down Expand Up @@ -94,7 +92,7 @@ def gzip_compressor(request):
return response

tinyMCEPreInit = {
"base": tinymce.settings.JS_BASE_URL,
"base": js_base_url,
"suffix": "",
}
content.append(f"var tinyMCEPreInit={json.dumps(tinyMCEPreInit)};")
Expand Down
15 changes: 7 additions & 8 deletions tinymce/settings.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import os

from django.conf import settings
from django.contrib.staticfiles.storage import staticfiles_storage

DEFAULT_CONFIG = getattr(
settings,
Expand Down Expand Up @@ -29,10 +28,10 @@
settings, "TINYMCE_FILEBROWSER", "filebrowser" in settings.INSTALLED_APPS
)

JS_URL = getattr(
settings,
"TINYMCE_JS_URL",
os.path.join(settings.STATIC_URL, "tinymce/tinymce.min.js"),
)

JS_BASE_URL = JS_URL[: JS_URL.rfind("/")]
def get_js_url():
return getattr(
settings,
"TINYMCE_JS_URL",
staticfiles_storage.url("tinymce/tinymce.min.js"),
)
2 changes: 1 addition & 1 deletion tinymce/widgets.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ def _media(self):
if tinymce.settings.USE_COMPRESSOR:
js = [reverse("tinymce-compressor")]
else:
js = [tinymce.settings.JS_URL]
js = [tinymce.settings.get_js_url()]
if tinymce.settings.USE_FILEBROWSER:
js.append(reverse("tinymce-filebrowser"))
if tinymce.settings.USE_EXTRA_MEDIA:
Expand Down

2 comments on commit 2c254ae

@elisescu
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@claudep This is a great step in the right direction :), but there are some JS files requested by tinymce.min.js itself (like the theme js file), and those fail for example in a setup where whitenoise serves the static files using hashes in their names. Any suggestions how to bypass that?

@claudep
Copy link
Contributor Author

@claudep claudep commented on 2c254ae Dec 9, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not the first situation in a Django package where JS files called from other JS files are causing issues in Django. Unfortunately, I don't have any suggestion for now, I guess it might be very difficult to solve (if possible at all). Any ideas welcome.

Please sign in to comment.