diff --git a/docs/introspection.rst b/docs/introspection.rst index 08006529c2..698ba95f6c 100644 --- a/docs/introspection.rst +++ b/docs/introspection.rst @@ -1,3 +1,5 @@ +.. _introspection: + Introspection ============= diff --git a/docs/plugin_hooks.rst b/docs/plugin_hooks.rst index 5cfae22a0f..b2c62ccdb3 100644 --- a/docs/plugin_hooks.rst +++ b/docs/plugin_hooks.rst @@ -556,6 +556,8 @@ The view function can be a regular function or an ``async def`` function, depend The function can either return a :ref:`internals_response` or it can return nothing and instead respond directly to the request using the ASGI ``send`` function (for advanced uses only). +See :ref:`writing_plugins_designing_urls` for tips on designing the URL routes used by your plugin. + Examples: `datasette-auth-github `__, `datasette-psutil `__ .. _plugin_register_facet_classes: diff --git a/docs/writing_plugins.rst b/docs/writing_plugins.rst index f763f61775..29fcca13e5 100644 --- a/docs/writing_plugins.rst +++ b/docs/writing_plugins.rst @@ -202,6 +202,36 @@ The plugin configuration could also be set at the top level of ``metadata.json`` Now that ``datasette-cluster-map`` plugin configuration will apply to every table in every database. +.. _writing_plugins_designing_urls: + +Designing URLs for your plugin +------------------------------ + +You can register new URL routes within Datasette using the :ref:`plugin_register_routes` plugin hook. + +Datasette's default URLs include these: + +- ``/dbname`` - database page +- ``/dbname/tablename`` - table page +- ``/dbname/tablename/pk`` - row page + +See :ref:`pages` and :ref:`introspection` for more default URL routes. + +To avoid accidentally conflicting with a database file that may be loaded into Datasette, plugins should register URLs using a ``/-/`` prefix. For example, if your plugin adds a new interface for uploading Excel files you might register a URL route like this one: + +- ``/-/upload-excel`` + +Try to avoid registering URLs that clash with other plugins that your users might have installed. There is no central repository of reserved URL paths (yet) but you can review existing plugins by browsing the `datasette-plugin topic `__ on GitHub. + +If your plugin includes functionality that relates to a specific database you could also register a URL route like this: + +- ``/dbname/-/upload-excel`` + +Reserving routes under ``/dbname/tablename/-/...`` is not a good idea because a table could conceivably include a row with a primary key value of ``-``. Instead, you could use a pattern like this: + +- ``/dbname/-/upload-excel/tablename`` + + .. _writing_plugins_building_urls: Building URLs within plugins