Skip to content

Commit

Permalink
/-/ alternative URL for homepage, closes #2393
Browse files Browse the repository at this point in the history
  • Loading branch information
simonw committed Aug 15, 2024
1 parent bf95362 commit 9306766
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 1 deletion.
2 changes: 2 additions & 0 deletions datasette/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -1476,6 +1476,8 @@ def add_route(view, regex):
routes.append((regex, view))

add_route(IndexView.as_view(self), r"/(\.(?P<format>jsono?))?$")
add_route(IndexView.as_view(self), r"/-/(\.(?P<format>jsono?))?$")
add_route(permanent_redirect("/-/"), r"/-$")
# TODO: /favicon.ico and /-/static/ deserve far-future cache expires
add_route(favicon, "/favicon.ico")

Expand Down
4 changes: 4 additions & 0 deletions datasette/templates/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

{% block title %}{{ metadata.title or "Datasette" }}: {% for database in databases %}{{ database.name }}{% if not loop.last %}, {% endif %}{% endfor %}{% endblock %}

{% block extra_head %}
{% if noindex %}<meta name="robots" content="noindex">{% endif %}
{% endblock %}

{% block body_class %}index{% endblock %}

{% block content %}
Expand Down
4 changes: 3 additions & 1 deletion datasette/views/index.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,8 +152,9 @@ async def get(self, request):
extra_links = await await_me_maybe(hook)
if extra_links:
homepage_actions.extend(extra_links)
alternative_homepage = request.path == "/-/"
return await self.render(
["index.html"],
["default:index.html" if alternative_homepage else "index.html"],
request=request,
context={
"databases": databases,
Expand All @@ -166,5 +167,6 @@ async def get(self, request):
"top_homepage", self.ds, request
),
"homepage_actions": homepage_actions,
"noindex": request.path == "/-/",
},
)
2 changes: 2 additions & 0 deletions docs/pages.rst
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ Add ``/.json`` to the end of the URL for the JSON version of the underlying data
* `global-power-plants.datasettes.com/.json <https://global-power-plants.datasettes.com/.json>`_
* `register-of-members-interests.datasettes.com/.json <https://register-of-members-interests.datasettes.com/.json>`_

The index page can also be accessed at ``/-/``, useful for if the default index page has been replaced using an :ref:`index.html custom template <customization_custom_templates>`. The ``/-/`` page will always render the default Datasette ``index.html`` template.

.. _DatabaseView:

Database
Expand Down
22 changes: 22 additions & 0 deletions tests/test_html.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from bs4 import BeautifulSoup as Soup
from datasette.app import Datasette
from datasette.utils import allowed_pragmas
from .fixtures import ( # noqa
app_client,
Expand Down Expand Up @@ -51,6 +52,27 @@ def test_homepage(app_client_two_attached_databases):
] == table_links


@pytest.mark.asyncio
@pytest.mark.parametrize("path", ("/", "/-/"))
async def test_homepage_alternative_location(path, tmp_path_factory):
template_dir = tmp_path_factory.mktemp("templates")
(template_dir / "index.html").write_text("Custom homepage", "utf-8")
datasette = Datasette(template_dir=str(template_dir))
response = await datasette.client.get(path)
assert response.status_code == 200
html = response.text
if path == "/":
assert html == "Custom homepage"
else:
assert '<meta name="robots" content="noindex">' in html


@pytest.mark.asyncio
async def test_homepage_alternative_redirect(ds_client):
response = await ds_client.get("/-")
assert response.status_code == 301


@pytest.mark.asyncio
async def test_http_head(ds_client):
response = await ds_client.head("/")
Expand Down

0 comments on commit 9306766

Please sign in to comment.