base_url now works for static assets as well

pull/708/head
Simon Willison 2020-03-24 15:08:45 -07:00
rodzic 0b37a104fd
commit b1f953b5de
5 zmienionych plików z 38 dodań i 28 usunięć

Wyświetl plik

@ -1,7 +1,7 @@
<script src="/-/static/sql-formatter-2.3.3.min.js" defer></script> <script src="{{ base_url }}-/static/sql-formatter-2.3.3.min.js" defer></script>
<script src="/-/static/codemirror-5.31.0.js"></script> <script src="{{ base_url }}-/static/codemirror-5.31.0.js"></script>
<link rel="stylesheet" href="/-/static/codemirror-5.31.0-min.css" /> <link rel="stylesheet" href="{{ base_url }}-/static/codemirror-5.31.0-min.css" />
<script src="/-/static/codemirror-5.31.0-sql.min.js"></script> <script src="{{ base_url }}-/static/codemirror-5.31.0-sql.min.js"></script>
<style> <style>
.CodeMirror { height: auto; min-height: 70px; width: 80%; border: 1px solid #ddd; } .CodeMirror { height: auto; min-height: 70px; width: 80%; border: 1px solid #ddd; }
.CodeMirror-scroll { max-height: 200px; } .CodeMirror-scroll { max-height: 200px; }

Wyświetl plik

@ -2,7 +2,7 @@
<html> <html>
<head> <head>
<title>{% block title %}{% endblock %}</title> <title>{% block title %}{% endblock %}</title>
<link rel="stylesheet" href="/-/static/app.css?{{ app_css_hash }}"> <link rel="stylesheet" href="{{ base_url }}-/static/app.css?{{ app_css_hash }}">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no"> <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
{% for url in extra_css_urls %} {% for url in extra_css_urls %}
<link rel="stylesheet" href="{{ url.url }}"{% if url.sri %} integrity="{{ url.sri }}" crossorigin="anonymous"{% endif %}> <link rel="stylesheet" href="{{ url.url }}"{% if url.sri %} integrity="{{ url.sri }}" crossorigin="anonymous"{% endif %}>

Wyświetl plik

@ -351,7 +351,7 @@ def prepare_connection(conn, database, datasette):
@hookimpl @hookimpl
def extra_css_urls(template, database, table, datasette): def extra_css_urls(template, database, table, datasette):
return ['https://example.com/{}/extra-css-urls-demo.css'.format( return ['https://plugin-example.com/{}/extra-css-urls-demo.css'.format(
base64.b64encode(json.dumps({ base64.b64encode(json.dumps({
"template": template, "template": template,
"database": database, "database": database,
@ -363,9 +363,9 @@ def extra_css_urls(template, database, table, datasette):
@hookimpl @hookimpl
def extra_js_urls(): def extra_js_urls():
return [{ return [{
'url': 'https://example.com/jquery.js', 'url': 'https://plugin-example.com/jquery.js',
'sri': 'SRIHASH', 'sri': 'SRIHASH',
}, 'https://example.com/plugin1.js'] }, 'https://plugin-example.com/plugin1.js']
@hookimpl @hookimpl
@ -421,9 +421,9 @@ import json
@hookimpl @hookimpl
def extra_js_urls(): def extra_js_urls():
return [{ return [{
'url': 'https://example.com/jquery.js', 'url': 'https://plugin-example.com/jquery.js',
'sri': 'SRIHASH', 'sri': 'SRIHASH',
}, 'https://example.com/plugin2.js'] }, 'https://plugin-example.com/plugin2.js']
@hookimpl @hookimpl

Wyświetl plik

@ -1174,19 +1174,29 @@ def test_base_url_config(base_url, path):
for client in make_app_client(config={"base_url": base_url}): for client in make_app_client(config={"base_url": base_url}):
response = client.get(base_url + path.lstrip("/")) response = client.get(base_url + path.lstrip("/"))
soup = Soup(response.body, "html.parser") soup = Soup(response.body, "html.parser")
for a in soup.findAll("a"): for el in soup.findAll(["a", "link", "script"]):
href = a["href"] if "href" in el.attrs:
if not href.startswith("#") and href not in { href = el["href"]
"https://github.com/simonw/datasette", elif "src" in el.attrs:
"https://github.com/simonw/datasette/blob/master/LICENSE", href = el["src"]
"https://github.com/simonw/datasette/blob/master/tests/fixtures.py", else:
}: continue # Could be a <script>...</script>
if (
not href.startswith("#")
and href
not in {
"https://github.com/simonw/datasette",
"https://github.com/simonw/datasette/blob/master/LICENSE",
"https://github.com/simonw/datasette/blob/master/tests/fixtures.py",
}
and not href.startswith("https://plugin-example.com/")
):
# If this has been made absolute it may start http://localhost/ # If this has been made absolute it may start http://localhost/
if href.startswith("http://localhost/"): if href.startswith("http://localhost/"):
href = href[len("http://localost/") :] href = href[len("http://localost/") :]
assert href.startswith(base_url), { assert href.startswith(base_url), {
"base_url": base_url, "base_url": base_url,
"path": path, "path": path,
"href_in_document": href, "href_or_src": href,
"link_parent": str(a.parent), "element_parent": str(el.parent),
} }

Wyświetl plik

@ -64,7 +64,7 @@ def test_plugin_extra_js_urls(app_client):
== { == {
"integrity": "SRIHASH", "integrity": "SRIHASH",
"crossorigin": "anonymous", "crossorigin": "anonymous",
"src": "https://example.com/jquery.js", "src": "https://plugin-example.com/jquery.js",
} }
] ]
@ -74,7 +74,7 @@ def test_plugins_with_duplicate_js_urls(app_client):
response = app_client.get("/fixtures") response = app_client.get("/fixtures")
# This test is a little tricky, as if the user has any other plugins in # This test is a little tricky, as if the user has any other plugins in
# their current virtual environment those may affect what comes back too. # their current virtual environment those may affect what comes back too.
# What matters is that https://example.com/jquery.js is only there once # What matters is that https://plugin-example.com/jquery.js is only there once
# and it comes before plugin1.js and plugin2.js which could be in either # and it comes before plugin1.js and plugin2.js which could be in either
# order # order
scripts = Soup(response.body, "html.parser").findAll("script") scripts = Soup(response.body, "html.parser").findAll("script")
@ -82,16 +82,16 @@ def test_plugins_with_duplicate_js_urls(app_client):
# No duplicates allowed: # No duplicates allowed:
assert len(srcs) == len(set(srcs)) assert len(srcs) == len(set(srcs))
# jquery.js loaded once: # jquery.js loaded once:
assert 1 == srcs.count("https://example.com/jquery.js") assert 1 == srcs.count("https://plugin-example.com/jquery.js")
# plugin1.js and plugin2.js are both there: # plugin1.js and plugin2.js are both there:
assert 1 == srcs.count("https://example.com/plugin1.js") assert 1 == srcs.count("https://plugin-example.com/plugin1.js")
assert 1 == srcs.count("https://example.com/plugin2.js") assert 1 == srcs.count("https://plugin-example.com/plugin2.js")
# jquery comes before them both # jquery comes before them both
assert srcs.index("https://example.com/jquery.js") < srcs.index( assert srcs.index("https://plugin-example.com/jquery.js") < srcs.index(
"https://example.com/plugin1.js" "https://plugin-example.com/plugin1.js"
) )
assert srcs.index("https://example.com/jquery.js") < srcs.index( assert srcs.index("https://plugin-example.com/jquery.js") < srcs.index(
"https://example.com/plugin2.js" "https://plugin-example.com/plugin2.js"
) )