{"html_url": "https://github.com/simonw/datasette/issues/1238#issuecomment-855278998", "issue_url": "https://api.github.com/repos/simonw/datasette/issues/1238", "id": 855278998, "node_id": "MDEyOklzc3VlQ29tbWVudDg1NTI3ODk5OA==", "user": {"value": 9599, "label": "simonw"}, "created_at": "2021-06-05T18:37:16Z", "updated_at": "2021-06-05T18:37:16Z", "author_association": "OWNER", "body": "Alternative idea: populate `request.scope` with a new `route_path` which is the base-url-stripped version, which we then use for other routing operations.", "reactions": "{\"total_count\": 0, \"+1\": 0, \"-1\": 0, \"laugh\": 0, \"hooray\": 0, \"confused\": 0, \"heart\": 0, \"rocket\": 0, \"eyes\": 0}", "issue": {"value": 813899472, "label": "Custom pages don't work with base_url setting"}, "performed_via_github_app": null} {"html_url": "https://github.com/simonw/datasette/issues/1238#issuecomment-855278540", "issue_url": "https://api.github.com/repos/simonw/datasette/issues/1238", "id": 855278540, "node_id": "MDEyOklzc3VlQ29tbWVudDg1NTI3ODU0MA==", "user": {"value": 9599, "label": "simonw"}, "created_at": "2021-06-05T18:33:25Z", "updated_at": "2021-06-05T18:33:25Z", "author_association": "OWNER", "body": "Got the test to pass by ensuring the tests don't accidentally double-rewrite the path.\r\n\r\nRan into a new problem:\r\n```\r\n @pytest.mark.asyncio\r\n @pytest.mark.parametrize(\r\n \"prefix,expected_path\", [(None, \"/asgi-scope\"), (\"/prefix/\", \"/prefix/asgi-scope\")]\r\n )\r\n async def test_client_path(datasette, prefix, expected_path):\r\n original_base_url = datasette._settings[\"base_url\"]\r\n try:\r\n if prefix is not None:\r\n datasette._settings[\"base_url\"] = prefix\r\n response = await datasette.client.get(\"/asgi-scope\")\r\n path = response.json()[\"path\"]\r\n> assert path == expected_path\r\nE AssertionError: assert '/asgi-scope' == '/prefix/asgi-scope'\r\nE - /prefix/asgi-scope\r\nE ? -------\r\nE + /asgi-scope\r\n```\r\nThat test confirms that messing around with the `base_url` doesn't modify the ASGI scope... but the fix I'm using for this issue DOES modify the ASGI scope.\r\n\r\nThe question raised here is: should the ASGI scope stay unmodified when `base_url` is used?\r\n\r\nI think it should. It doesn't make sense to obscure the \"real\" path just to get custom pages to work properly.", "reactions": "{\"total_count\": 0, \"+1\": 0, \"-1\": 0, \"laugh\": 0, \"hooray\": 0, \"confused\": 0, \"heart\": 0, \"rocket\": 0, \"eyes\": 0}", "issue": {"value": 813899472, "label": "Custom pages don't work with base_url setting"}, "performed_via_github_app": null} {"html_url": "https://github.com/simonw/datasette/issues/1238#issuecomment-855272693", "issue_url": "https://api.github.com/repos/simonw/datasette/issues/1238", "id": 855272693, "node_id": "MDEyOklzc3VlQ29tbWVudDg1NTI3MjY5Mw==", "user": {"value": 9599, "label": "simonw"}, "created_at": "2021-06-05T17:45:42Z", "updated_at": "2021-06-05T17:45:42Z", "author_association": "OWNER", "body": "Applying this fix worked when I manually tested it:\r\n```diff\r\n base_url = self.ds.setting(\"base_url\")\r\n if base_url != \"/\" and path.startswith(base_url):\r\n path = \"/\" + path[len(base_url) :]\r\n+ scope = dict(scope, path=path, raw_path=path.encode(\"utf-8\"))\r\n request = Request(scope, receive)\r\n```\r\nBut... the test I wrote still failed. My hunch is that this is because deep within the test framework requests go through `ds.client` which may be applying its own changes relevant to `base_url`:\r\n\r\nhttps://github.com/simonw/datasette/blob/6e9b07be92905011211d8df7a872fb7c1f2737b2/datasette/utils/testing.py#L139", "reactions": "{\"total_count\": 0, \"+1\": 0, \"-1\": 0, \"laugh\": 0, \"hooray\": 0, \"confused\": 0, \"heart\": 0, \"rocket\": 0, \"eyes\": 0}", "issue": {"value": 813899472, "label": "Custom pages don't work with base_url setting"}, "performed_via_github_app": null} {"html_url": "https://github.com/simonw/datasette/issues/1238#issuecomment-855270917", "issue_url": "https://api.github.com/repos/simonw/datasette/issues/1238", "id": 855270917, "node_id": "MDEyOklzc3VlQ29tbWVudDg1NTI3MDkxNw==", "user": {"value": 9599, "label": "simonw"}, "created_at": "2021-06-05T17:32:29Z", "updated_at": "2021-06-05T17:32:29Z", "author_association": "OWNER", "body": "This looks like the cause: https://github.com/simonw/datasette/blob/6e9b07be92905011211d8df7a872fb7c1f2737b2/datasette/app.py#L1087-L1092\r\n\r\nNote how `path` is modified... but then we create a new `Request()` that uses the old scope, which has unmodified `scope[\"path\"]` - and then the code later on looks at `request.scope[\"path\"]` when deciding if the request matches:\r\n\r\nhttps://github.com/simonw/datasette/blob/afed51b1e36cf275c39e71c7cb262d6c5bdbaa31/datasette/app.py#L1154-L1155", "reactions": "{\"total_count\": 0, \"+1\": 0, \"-1\": 0, \"laugh\": 0, \"hooray\": 0, \"confused\": 0, \"heart\": 0, \"rocket\": 0, \"eyes\": 0}", "issue": {"value": 813899472, "label": "Custom pages don't work with base_url setting"}, "performed_via_github_app": null} {"html_url": "https://github.com/simonw/datasette/issues/1238#issuecomment-790857004", "issue_url": "https://api.github.com/repos/simonw/datasette/issues/1238", "id": 790857004, "node_id": "MDEyOklzc3VlQ29tbWVudDc5MDg1NzAwNA==", "user": {"value": 79913, "label": "tsibley"}, "created_at": "2021-03-04T19:06:55Z", "updated_at": "2021-03-04T19:06:55Z", "author_association": "NONE", "body": "@rgieseke Ah, that's super helpful. Thank you for the workaround for now!", "reactions": "{\"total_count\": 1, \"+1\": 1, \"-1\": 0, \"laugh\": 0, \"hooray\": 0, \"confused\": 0, \"heart\": 0, \"rocket\": 0, \"eyes\": 0}", "issue": {"value": 813899472, "label": "Custom pages don't work with base_url setting"}, "performed_via_github_app": null} {"html_url": "https://github.com/simonw/datasette/issues/1238#issuecomment-789186458", "issue_url": "https://api.github.com/repos/simonw/datasette/issues/1238", "id": 789186458, "node_id": "MDEyOklzc3VlQ29tbWVudDc4OTE4NjQ1OA==", "user": {"value": 198537, "label": "rgieseke"}, "created_at": "2021-03-02T20:19:30Z", "updated_at": "2021-03-02T20:19:30Z", "author_association": "CONTRIBUTOR", "body": "A custom `templates/index.html` seems to work and custom `pages` as a workaround with moving them to `pages/base_url_dir`.", "reactions": "{\"total_count\": 0, \"+1\": 0, \"-1\": 0, \"laugh\": 0, \"hooray\": 0, \"confused\": 0, \"heart\": 0, \"rocket\": 0, \"eyes\": 0}", "issue": {"value": 813899472, "label": "Custom pages don't work with base_url setting"}, "performed_via_github_app": null} {"html_url": "https://github.com/simonw/datasette/issues/1238#issuecomment-786849095", "issue_url": "https://api.github.com/repos/simonw/datasette/issues/1238", "id": 786849095, "node_id": "MDEyOklzc3VlQ29tbWVudDc4Njg0OTA5NQ==", "user": {"value": 9599, "label": "simonw"}, "created_at": "2021-02-26T19:29:38Z", "updated_at": "2021-02-26T19:29:38Z", "author_association": "OWNER", "body": "Here's the test I wrote:\r\n```diff\r\ngit diff tests/test_custom_pages.py\r\ndiff --git a/tests/test_custom_pages.py b/tests/test_custom_pages.py\r\nindex 6a23192..5a71f56 100644\r\n--- a/tests/test_custom_pages.py\r\n+++ b/tests/test_custom_pages.py\r\n@@ -2,11 +2,19 @@ import pathlib\r\n import pytest\r\n from .fixtures import make_app_client\r\n \r\n+TEST_TEMPLATE_DIRS = str(pathlib.Path(__file__).parent / \"test_templates\")\r\n+\r\n \r\n @pytest.fixture(scope=\"session\")\r\n def custom_pages_client():\r\n+ with make_app_client(template_dir=TEST_TEMPLATE_DIRS) as client:\r\n+ yield client\r\n+\r\n+\r\n+@pytest.fixture(scope=\"session\")\r\n+def custom_pages_client_with_base_url():\r\n with make_app_client(\r\n- template_dir=str(pathlib.Path(__file__).parent / \"test_templates\")\r\n+ template_dir=TEST_TEMPLATE_DIRS, config={\"base_url\": \"/prefix/\"}\r\n ) as client:\r\n yield client\r\n \r\n@@ -23,6 +31,12 @@ def test_request_is_available(custom_pages_client):\r\n assert \"path:/request\" == response.text\r\n \r\n \r\n+def test_custom_pages_with_base_url(custom_pages_client_with_base_url):\r\n+ response = custom_pages_client_with_base_url.get(\"/prefix/request\")\r\n+ assert 200 == response.status\r\n+ assert \"path:/prefix/request\" == response.text\r\n+\r\n+\r\n def test_custom_pages_nested(custom_pages_client):\r\n response = custom_pages_client.get(\"/nested/nest\")\r\n assert 200 == response.status\r\n```", "reactions": "{\"total_count\": 0, \"+1\": 0, \"-1\": 0, \"laugh\": 0, \"hooray\": 0, \"confused\": 0, \"heart\": 0, \"rocket\": 0, \"eyes\": 0}", "issue": {"value": 813899472, "label": "Custom pages don't work with base_url setting"}, "performed_via_github_app": null} {"html_url": "https://github.com/simonw/datasette/issues/1238#issuecomment-786848654", "issue_url": "https://api.github.com/repos/simonw/datasette/issues/1238", "id": 786848654, "node_id": "MDEyOklzc3VlQ29tbWVudDc4Njg0ODY1NA==", "user": {"value": 9599, "label": "simonw"}, "created_at": "2021-02-26T19:28:48Z", "updated_at": "2021-02-26T19:28:48Z", "author_association": "OWNER", "body": "I added a debug line just before `for regex, wildcard_template` here:\r\n\r\nhttps://github.com/simonw/datasette/blob/afed51b1e36cf275c39e71c7cb262d6c5bdbaa31/datasette/app.py#L1148-L1155\r\n\r\nAnd it showed that for some reason `request.path` is `/prefix/prefix/request` here - the prefix got doubled somehow.", "reactions": "{\"total_count\": 0, \"+1\": 0, \"-1\": 0, \"laugh\": 0, \"hooray\": 0, \"confused\": 0, \"heart\": 0, \"rocket\": 0, \"eyes\": 0}", "issue": {"value": 813899472, "label": "Custom pages don't work with base_url setting"}, "performed_via_github_app": null} {"html_url": "https://github.com/simonw/datasette/issues/1238#issuecomment-786841261", "issue_url": "https://api.github.com/repos/simonw/datasette/issues/1238", "id": 786841261, "node_id": "MDEyOklzc3VlQ29tbWVudDc4Njg0MTI2MQ==", "user": {"value": 9599, "label": "simonw"}, "created_at": "2021-02-26T19:13:44Z", "updated_at": "2021-02-26T19:13:44Z", "author_association": "OWNER", "body": "Sounds like a bug - thanks for reporting this.", "reactions": "{\"total_count\": 0, \"+1\": 0, \"-1\": 0, \"laugh\": 0, \"hooray\": 0, \"confused\": 0, \"heart\": 0, \"rocket\": 0, \"eyes\": 0}", "issue": {"value": 813899472, "label": "Custom pages don't work with base_url setting"}, "performed_via_github_app": null}