Skip to content

Airgap Tethys when STATICFILES_USE_NPM is true - #1300

Draft
James-Dolinar wants to merge 5 commits into
mainfrom
offline-static-deps
Draft

Airgap Tethys when STATICFILES_USE_NPM is true#1300
James-Dolinar wants to merge 5 commits into
mainfrom
offline-static-deps

Conversation

@James-Dolinar

Copy link
Copy Markdown
Contributor

Description

Remove hard coded requests to CDNs so the static dependencies are served through Tethys when STATICFILES_USE_NPM is true. Additionally, add convenience tags {% dependency_script %} and {% dependency_link %} so app creators don't have to use conditionals in templates to determine when STATICFILES_USE_NPM is true or false.

Changes Made to Code

  • tethys_portal/dependencies.py
    • Added select2-bootstrap-5-theme and Font Awesome (@fortawesome/fontawesome-free) to vendor_static_dependencies so they can be served locally.
    • Updated StaticDependency.get_custom_version_url to serve the locally-installed (default) version instead of falling back to a CDN when STATICFILES_USE_NPM is true and a non-installed version is requested (the old CDN fallback fails in an air-gapped environment). Online behavior is unchanged — the requested version is still fetched from the CDN.
  • tethys_layouts/MapLayout
    • map_layout.html: replaced the hard coded select2-bootstrap-5-theme CDN <link> with the vendored dependency; load Plotly locally (inlined from the installed Python package via the plotly_view gizmo dependency) when STATICFILES_USE_NPM is true, and from the CDN otherwise (still version-configurable in the controller).
    • map_layout.py: added staticfiles_use_npm to the template context to drive the Plotly conditional.
  • tethys_layouts/TethysLayout
    • tethys_layout.html: replaced the hard coded Font Awesome CDN <link> with the vendored dependency.
  • New convenience template tags
    • tethys_apps/templatetags/dependency.py: added {% dependency_script %} and {% dependency_link %}. They emit a <script>/<link> that resolves to the app's local node_modules when STATICFILES_USE_NPM is true and to the CDN otherwise. The local path is derived automatically from jsDelivr/unpkg npm URLs; an optional local_path argument covers other CDNs (e.g. cdnjs).
    • tethys_sdk/templatetags/tethys.py: registered the new tags in the aggregated tethys library so they're available via {% load tethys %}.

Related PRs, Issues, and Discussions

Additional Notes

Quality Checks

  • At least one new test has been written for new code
  • New code has 100% test coverage
  • Code has been formatted with Black
  • Code has been linted with flake8
  • Docstrings for new methods have been added
  • The documentation has been updated appropriately

- Register it in vendor_static_dependencies so it can be served locally
- Replace the hardcoded jsDelivr CDN link in map_layout.html with the vendored
  dependency, so the Select2 Bootstrap 5 theme works air-gapped
- Add staticfiles_use_npm to map layout's context dictionary
- Use staticfiles_use_npm to determine when to use the local plotly instead of the CDN provided plotly
- Continue to allow specifying the version of plotly in the controller when staticfiles_use_npm is False
- Local plotly is inlined from the installed Python package via the plotly_view gizmo dependency
- Register it in vendor_static_dependencies so it can be served locally
- Replace the hardcoded Font Awesome CDN link (use.fontawesome.com) in
  tethys_layout.html with the vendored dependency, so fontawesome works air-gapped
- Online, Font Awesome now loads from jsDelivr (the vendored dependency's CDN) instead of use.fontawesome.com
- Let app templates load third-party JS/CSS that is served from the app's local
  node_modules when STATICFILES_USE_NPM is True, and from the CDN otherwise, so
  apps work air-gapped without a manual local/CDN toggle
- Derive the local path automatically from jsDelivr and unpkg npm URLs, so authors
  only pass the CDN URL they would normally use
- Accept an optional local_path for CDNs that can't be auto-derived (e.g. cdnjs)
- Register the tags in the tethys template tag library so they're available via {% load tethys %}
…ailable offline

- In get_custom_version_url, serve the installed (default) version locally instead
  of the CDN when STATICFILES_USE_NPM is True and a different version is requested
- The old CDN fallback failed in air-gapped environments; npm only installs the
  default version, so that's the only one available locally
- Log a warning that the requested version isn't available and the installed one is used
- Online behavior is unchanged: with STATICFILES_USE_NPM False, the requested version
  is still served from the CDN
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant