Skip to content

Commit

Permalink
Switch CDN from skypack to jsdelivr (Fixes #42)
Browse files Browse the repository at this point in the history
  • Loading branch information
miguelgrinberg committed Oct 18, 2022
1 parent deaebe9 commit 0292e58
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 13 deletions.
16 changes: 9 additions & 7 deletions src/turbo_flask/turbo.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
from markupsafe import Markup


_CDN = 'https://cdn.skypack.dev'
_CDN = 'https://cdn.jsdelivr.net'
_PKG = '@hotwired/turbo'
_VER = 'v7.2.2-RBjb2wnkmosSQVoP27jT'
_VER = '7.2.2'


class Turbo:
Expand Down Expand Up @@ -52,14 +52,16 @@ def turbo(self, version=_VER, url=None):
default version and CDN.
"""
if url is None:
url = f'{_CDN}/pin/{_PKG}@{version}/min/{_PKG}.js'
v = ''
if version is not None:
v = f'@{version}'
url = f'{_CDN}/npm/{_PKG}{v}/dist/turbo.es2017-umd.js'
ws_route = current_app.config.get('TURBO_WEBSOCKET_ROUTE',
'/turbo-stream')
if ws_route:
return Markup(f'''<script type="module">
import * as Turbo from "{url}";
Turbo.connectStreamSource(new WebSocket(`ws${{location.protocol.substring(4)}}//${{location.host}}{ws_route}`));
</script>''') # noqa: E501
return Markup(f'''<script src="{url}"></script>
<script>Turbo.connectStreamSource(new WebSocket(`ws${{location.protocol.substring(4)}}//${{location.host}}{ws_route}`));</script>
''') # noqa: E501
else:
return Markup(f'<script type="module" src="{url}"></script>')

Expand Down
28 changes: 22 additions & 6 deletions tests/test_turbo.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ def test():
('__flask_sock.turbo_stream', {})

rv = app.test_client().get('/test')
assert b'@hotwired/turbo.js' in rv.data
assert b'@hotwired/turbo@' in rv.data
assert b'Turbo.connectStreamSource' in rv.data

def test_indirect_create(self):
Expand All @@ -37,7 +37,7 @@ def test():
('__flask_sock.turbo_stream', {})

rv = app.test_client().get('/test')
assert b'@hotwired/turbo.js' in rv.data
assert b'@hotwired/turbo@' in rv.data
assert b'Turbo.connectStreamSource' in rv.data

def test_create_custom_ws(self):
Expand All @@ -56,7 +56,7 @@ def test():
('__flask_sock.turbo_stream', {})

rv = app.test_client().get('/test')
assert b'@hotwired/turbo.js' in rv.data
assert b'@hotwired/turbo@' in rv.data
assert b'Turbo.connectStreamSource' in rv.data

def test_create_no_ws(self):
Expand All @@ -73,7 +73,7 @@ def test():
url_adapter.match('/turbo-stream', websocket=True)

rv = app.test_client().get('/test')
assert b'@hotwired/turbo.js' in rv.data
assert b'@hotwired/turbo@' in rv.data
assert b'Turbo.connectStreamSource' not in rv.data

def test_create_custom_turbo_version(self):
Expand All @@ -82,14 +82,30 @@ def test_create_custom_turbo_version(self):

@app.route('/test')
def test():
return render_template_string('{{ turbo(version="v1.2.3") }}')
return render_template_string('{{ turbo(version="1.2.3") }}')

url_adapter = app.url_map.bind('localhost', '/')
assert url_adapter.match('/turbo-stream', websocket=True) == \
('__flask_sock.turbo_stream', {})

rv = app.test_client().get('/test')
assert b'@hotwired/turbo@v1.2.3' in rv.data
assert b'@hotwired/turbo@1.2.3/dist' in rv.data
assert b'Turbo.connectStreamSource' in rv.data

def test_create_latest_turbo_version(self):
app = Flask(__name__)
turbo_flask.Turbo(app)

@app.route('/test')
def test():
return render_template_string('{{ turbo(version=None) }}')

url_adapter = app.url_map.bind('localhost', '/')
assert url_adapter.match('/turbo-stream', websocket=True) == \
('__flask_sock.turbo_stream', {})

rv = app.test_client().get('/test')
assert b'@hotwired/turbo/dist' in rv.data
assert b'Turbo.connectStreamSource' in rv.data

def test_create_custom_turbo_url(self):
Expand Down

0 comments on commit 0292e58

Please sign in to comment.