Skip to content
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

Add Link: rel="alternate" header pointing to JSON for a table/query #1533

Closed
simonw opened this issue Nov 28, 2021 · 4 comments
Closed

Add Link: rel="alternate" header pointing to JSON for a table/query #1533

simonw opened this issue Nov 28, 2021 · 4 comments

Comments

@simonw
Copy link
Owner

simonw commented Nov 28, 2021

Originally explored in simonw/datasette-notebook#2 (comment) - I wanted an efficient way to scan a list of URLs and figure out which if any of those corresponded to Datasette tables, canned queries or SQL output that could be represented as a table on a page.

It looks like a neat way to do that is with Link: header like this:

Link: http://127.0.0.1:8058/fixtures/compound_three_primary_keys.json; rel="alternate"; type="application/datasette+json"

I can put a <link href=... in the page header too.

@simonw
Copy link
Owner Author

simonw commented Nov 28, 2021

I built an initial prototype of this in a branch: e0a8469 - which exposed even more flaws in the way TableView is structured (adding custom HTTP headers to the response is way harder than it should be) which I should address in the refactor in #617.

@simonw
Copy link
Owner Author

simonw commented Feb 2, 2022

I'm going to apply the hack, then fix it again in:

@simonw simonw closed this as completed in 3ef47a0 Feb 2, 2022
@simonw
Copy link
Owner Author

simonw commented Feb 2, 2022

Documentation: https://docs.datasette.io/en/latest/json_api.html#discovering-the-json-for-a-page

https://docs.datasette.io/en/latest/json_api.html top --cors section mentions the new Access-Control-Expose-Headers: Link header.

@simonw
Copy link
Owner Author

simonw commented Feb 2, 2022

Demos - these pages both have <link rel=... if you view source on them:

And you can hit them with curl like so:

% curl -I 'https://latest.datasette.io/fixtures/sortable'    
HTTP/1.1 200 OK
link: https://latest.datasette.io/fixtures/sortable.json; rel="alternate"; type="application/json+datasette"
cache-control: max-age=5
referrer-policy: no-referrer
access-control-allow-origin: *
access-control-allow-headers: Authorization
access-control-expose-headers: Link
content-type: text/html; charset=utf-8
x-databases: _memory, _internal, fixtures, extra_database
Date: Wed, 02 Feb 2022 07:56:17 GMT
Server: Google Frontend
Transfer-Encoding: chunked

% curl -I 'https://latest.datasette.io/fixtures/sortable/a,a'
HTTP/1.1 200 OK
link: https://latest.datasette.io/fixtures/sortable/a,a.json; rel="alternate"; type="application/json+datasette"
cache-control: max-age=5
referrer-policy: no-referrer
access-control-allow-origin: *
access-control-allow-headers: Authorization
access-control-expose-headers: Link
content-type: text/html; charset=utf-8
x-databases: _memory, _internal, fixtures, extra_database
Date: Wed, 02 Feb 2022 07:56:24 GMT
Server: Google Frontend
Transfer-Encoding: chunked

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant