-
-
Notifications
You must be signed in to change notification settings - Fork 695
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Custom pages don't work with base_url setting #1238
Comments
Unpins Datasette and upgrades to the latest version. Adjusts custom page styles and command-line options for upstream changes. Removes the link-fixing JS hack now that base_url is properly supported. XXX FIXME pending <simonw/datasette#1238>
Sounds like a bug - thanks for reporting this. |
I added a debug line just before Lines 1148 to 1155 in afed51b
And it showed that for some reason |
Here's the test I wrote: git diff tests/test_custom_pages.py
diff --git a/tests/test_custom_pages.py b/tests/test_custom_pages.py
index 6a23192..5a71f56 100644
--- a/tests/test_custom_pages.py
+++ b/tests/test_custom_pages.py
@@ -2,11 +2,19 @@ import pathlib
import pytest
from .fixtures import make_app_client
+TEST_TEMPLATE_DIRS = str(pathlib.Path(__file__).parent / "test_templates")
+
@pytest.fixture(scope="session")
def custom_pages_client():
+ with make_app_client(template_dir=TEST_TEMPLATE_DIRS) as client:
+ yield client
+
+
+@pytest.fixture(scope="session")
+def custom_pages_client_with_base_url():
with make_app_client(
- template_dir=str(pathlib.Path(__file__).parent / "test_templates")
+ template_dir=TEST_TEMPLATE_DIRS, config={"base_url": "/prefix/"}
) as client:
yield client
@@ -23,6 +31,12 @@ def test_request_is_available(custom_pages_client):
assert "path:/request" == response.text
+def test_custom_pages_with_base_url(custom_pages_client_with_base_url):
+ response = custom_pages_client_with_base_url.get("/prefix/request")
+ assert 200 == response.status
+ assert "path:/prefix/request" == response.text
+
+
def test_custom_pages_nested(custom_pages_client):
response = custom_pages_client.get("/nested/nest")
assert 200 == response.status |
A custom |
Unpins Datasette and upgrades to the latest version. Adjusts custom page styles and command-line options for upstream changes. Removes the link-fixing JS hack now that base_url is properly supported. There is still one bug related to base_url¹ which affects custom pages like our barcode dialer. I've worked around this by symlinking dial.html under the expected production base_url. When the bug is resolved, the symlink can be removed. ¹ simonw/datasette#1238
Unpins Datasette and upgrades to the latest version. Adjusts custom page styles and command-line options for upstream changes. Removes the link-fixing JS hack now that base_url is properly supported.¹ There is still one bug related to base_url² which affects custom pages like our barcode dialer. I've worked around this by symlinking dial.html under the expected production base_url. When the bug is resolved, the symlink can be removed. ¹ simonw/datasette#838 ² simonw/datasette#1238
@rgieseke Ah, that's super helpful. Thank you for the workaround for now! |
This looks like the cause: Lines 1087 to 1092 in 6e9b07b
Note how Lines 1154 to 1155 in afed51b
|
Applying this fix worked when I manually tested it: base_url = self.ds.setting("base_url")
if base_url != "/" and path.startswith(base_url):
path = "/" + path[len(base_url) :]
+ scope = dict(scope, path=path, raw_path=path.encode("utf-8"))
request = Request(scope, receive) But... the test I wrote still failed. My hunch is that this is because deep within the test framework requests go through datasette/datasette/utils/testing.py Line 139 in 6e9b07b
|
Got the test to pass by ensuring the tests don't accidentally double-rewrite the path. Ran into a new problem:
That test confirms that messing around with the The question raised here is: should the ASGI scope stay unmodified when I think it should. It doesn't make sense to obscure the "real" path just to get custom pages to work properly. |
Alternative idea: populate |
It seems that custom pages aren't routing properly when the
base_url
setting is used.To reproduce, with Datasette 0.55.
Create a
templates/pages/custom.html
with some text.Start Datasette.
Visit http://localhost:8001/custom and see "Hello, world!".
Start Datasette with a
base_url
.Visit http://localhost:8001/prefix/custom and see a "Database not found: custom" 404.
Note that like all routes, http://localhost:8001/custom still works when run with
base_url
.The text was updated successfully, but these errors were encountered: