News for hackers. Stuff that matters.
Re:Why I Tend Not to Use Content Negotiation by LeonB on Sunday November 19, 2023 @08:34PM (#38341006)
A wisened old web developer named Rik told me that in a wiki he wrote long ago he performed his own cache-busting form of content negotiation by having urls ends with a file type —

E.g.

    /fred.html - returns html 
    /fred.md   - returns markdown
    /fred.json - returns json
(I’m guessing that also —

    /fred  - defaults to html
)

Though he was a strict fundamentalist restafarian in other ways he held that this had the benefits of

1. Working

2. Being readily understood

I’ve seen worse ideas. Only downside is that the urls look a bit ugly.

Re:Why I Tend Not to Use Content Negotiation by giancarlostoro on Sunday November 19, 2023 @09:18PM (#38341368)
I think if this is the URL behind any sort of "api" prefix in the URL (subdomain, or subdirectory) I don't see why not? It's always worked out pretty darned fantastic.
Re:Why I Tend Not to Use Content Negotiation by djbusby on Sunday November 19, 2023 @09:35PM (#38341511)
Who cares if urls are ugly?
Re:Why I Tend Not to Use Content Negotiation by dasmith91 on Sunday November 19, 2023 @10:41PM (#38342005)
Front-End Developers
Re:Why I Tend Not to Use Content Negotiation by quectophoton on Sunday November 19, 2023 @11:38PM (#38342320)
Also that's why single page applications are no longer technically single page.

    https://example.com/#!/
    https://example.com/#!/about/
    https://example.com/#!/users/
Are all the same page as far as the browser concerns, and if you move from `/#!/` to `/#!/users/`, and reload the page, you still load the same page (i.e. `/`).

But that was too ugly for URLs, so modern websites now use browser history APIs just so they can remove 3 characters from the URL and do stuff like:

    https://example.com/
    https://example.com/about/
    https://example.com/users/
Sure, if you load `/` and move to `/about/` you don't load a new page because of browser history APIs; but if you then refresh, now you load a different, uncached page (`/about/`, instead of `/`) even if the HTML in the response is exactly the same as in `/`.

Sure, the difference is not much, but to me it still seems like a waste when the response could have been cached already like in the first example.

Re:Why I Tend Not to Use Content Negotiation by alex_smart on Monday November 20, 2023 @12:32AM (#38342792)
> But that was too ugly for URLs, so modern websites now use browser history APIs just so they can remove 3 characters from the URL and do stuff like

Would back/forward buttons continue to work if we decided not use browser history APIs?

Re:Why I Tend Not to Use Content Negotiation by quectophoton on Monday November 20, 2023 @01:14AM (#38343248)
Yes, and a quick test with Mithril.js confirms it.

I could try with something like React, but that's too much for a quick test.

Re:Why I Tend Not to Use Content Negotiation by mewpmewp2 on Monday November 20, 2023 @07:21AM (#38346959)
Mithril.js also probably uses the history API?

.replaceState() will replace current url without putting it in the history, so back won't work, while .pushState() does.

Re:Why I Tend Not to Use Content Negotiation by PH95VuimJjqBqy on Monday November 20, 2023 @02:59PM (#38353677)
The other poster is wrong, the entire reason the history API exists is specifically because when these SPA frameworks first came onto the scene one of the things that was broken was history.

So they invented the history API's so SPA's could stop breaking some of the user expectations they were breaking.

Re:Why I Tend Not to Use Content Negotiation by prewk on Monday November 20, 2023 @02:01AM (#38343672)
The first one always requests / from the server when landed on/refreshed. The other one does not. This fact can be acted upon.
Re:Why I Tend Not to Use Content Negotiation by cylemons on Monday November 20, 2023 @03:30AM (#38344680)
What does the ! symbol do in url fragments?
Re:Why I Tend Not to Use Content Negotiation by scq on Monday November 20, 2023 @06:13AM (#38346346)
It was part of a scheme to make SPAs indexable by search engines. Google deprecated it back in 2015, as Googlebot can now execute JS.

https://developers.google.com/search/blog/2009/10/proposal-f...

Re:Why I Tend Not to Use Content Negotiation by vhcr on Monday November 20, 2023 @07:49AM (#38347220)
That issue can be solved with service workers.
Re:Why I Tend Not to Use Content Negotiation by account42 on Monday November 20, 2023 @11:28AM (#38350096)
The real reason for moving (back) from fragment URLs to real URLs is because the latter can serve different content on the initial load - specifically, they can act as actual webpages that use javascipt for progressive enhancement but work immediately whereas your / needs to display a spinner while you send of additional network requests to retrieve the content and build the page.
Re:Why I Tend Not to Use Content Negotiation by teeray on Sunday November 19, 2023 @10:46PM (#38342033)
Marketing
Re:Why I Tend Not to Use Content Negotiation by bobbylarrybobby on Sunday November 19, 2023 @11:35PM (#38342299)
These are the same people who make every link in the emails they send 1kB of tracking parameters?
Re:Why I Tend Not to Use Content Negotiation by DANmode on Sunday November 19, 2023 @11:38PM (#38342318)
Those navigating with a keyboard.
Re:Why I Tend Not to Use Content Negotiation by MenhirMike on Sunday November 19, 2023 @10:57PM (#38342083)
> Only downside is that the urls look a bit ugly.

If /fred returns HTML, then the URL is perfectly fine, since the only real consumer of fred.md or fred.json are automated systems/API clients, and they couldn't care less what the URL looks like, only that it's predictable.

Re:Why I Tend Not to Use Content Negotiation by manwe150 on Sunday November 19, 2023 @11:04PM (#38342120)
It is a good trick with GitHub though that you can add .patch or .diff to the url to fetch the content without styling
Re:Why I Tend Not to Use Content Negotiation by amake on Monday November 20, 2023 @01:04AM (#38343124)
It's not just styling. The content is actually a `git format-patch` patch for the former a unified-format diff for the latter.
Re:Why I Tend Not to Use Content Negotiation by going_north on Monday November 20, 2023 @01:35AM (#38343434)
Another good one for GitHub is that you can do

    github.com/[username].png
to get that username's profile picture.
Re:Why I Tend Not to Use Content Negotiation by theblazehen on Monday November 20, 2023 @02:54AM (#38344185)
And

        github.com/[username].keys
to get their ssh keys
Re:Why I Tend Not to Use Content Negotiation by barosl on Monday November 20, 2023 @03:01AM (#38344250)
We have

  github.com/[username].gpg
to get their OpenPGP keys, too!
Re:Why I Tend Not to Use Content Negotiation by rapnie on Monday November 20, 2023 @03:58AM (#38345074)
Just go for .secrets to get it all in one request.
Re:Why I Tend Not to Use Content Negotiation by withinboredom on Monday November 20, 2023 @06:17AM (#38346375)
2 things.

What about any of that is "secret?"

This endpoint does not work.

Re:Why I Tend Not to Use Content Negotiation by mewpmewp2 on Monday November 20, 2023 @07:16AM (#38346911)
Try .credit-card instead. Pretty sure that one works.
Re:Why I Tend Not to Use Content Negotiation by moondev on Monday November 20, 2023 @03:48AM (#38344939)
Adding .pibb to a gist url is another nice trick
Re:Why I Tend Not to Use Content Negotiation by trevor-e on Monday November 20, 2023 @12:10AM (#38342543)
Reddit does this too. You can add a ".json" suffix to any post and get the JSON response.
Re:Why I Tend Not to Use Content Negotiation by navanchauhan on Monday November 20, 2023 @02:44AM (#38344082)
also ".rss" to get a RSS feed
Re:Why I Tend Not to Use Content Negotiation by kagevf on Monday November 20, 2023 @02:53AM (#38344167)
I use that with a custom feed; works well.
Re:Why I Tend Not to Use Content Negotiation by paulddraper on Monday November 20, 2023 @12:51AM (#38342999)
Shopify does that.

Well, at least the appending .json to everything.

Re:Why I Tend Not to Use Content Negotiation by arrowsmith on Monday November 20, 2023 @01:15AM (#38343262)
Shopify is written in Rails, no? That's how Rails does it.
Re:Why I Tend Not to Use Content Negotiation by euroderf on Monday November 20, 2023 @02:09AM (#38343755)
But doesn't << /fred.md >> beat the heck out of something like << /fred?format=md >>?
Re:Why I Tend Not to Use Content Negotiation by incrudible on Monday November 20, 2023 @04:35AM (#38345446)
From a URL aesthetics perspective, yes. Otherwise it just adds yet another dimension of how to pass a parameter.
Re:Why I Tend Not to Use Content Negotiation by capableweb on Monday November 20, 2023 @07:11AM (#38346880)
Depends. Including it in the location part of the URL could make it harder to understand if you have multiple parameters, instead of just one. Using query parameters makes it very explicit, which can be good or bad depending on your use case, while sticking it into the URL makes it implicit.

Together with that, you can also go for putting it in the `hash` part of the URL, if you want to keep it a secret from the server, as the server doesn't receive those parts, `/fred/#format=md`. In this case, it doesn't make much sense, but useful to know for other things, like keys and whatnot.

Re:Why I Tend Not to Use Content Negotiation by matt-attack on Wednesday November 22, 2023 @04:50PM (#38385637)
Is don’t think I that hash method would work since that part of the URL isn’t sent to the server. It’s strictly used by the client to decide which part of the response we to show.

Server has no need for it.

Re:Why I Tend Not to Use Content Negotiation by the_gipsy on Monday November 20, 2023 @02:43AM (#38344069)
The URLs are different, suffix instead of the typical prefix, that's not really content negotiation.
Re:Why I Tend Not to Use Content Negotiation by kevincox on Monday November 20, 2023 @10:35AM (#38349535)
It also works quite well with the browser save feature. Curl and wget will also save to a nice name by default. It may be nice if content-type tracking was universal but unfortunately file-extensions are probably the most robust way of tagging some data with a type (other than magic bytes). You can save that JSON, email it, archive it, upload it to a fileserver, someone downloads it again, decompresses it and uploads it in an HTTP form and the result will probably still be identified as JSON and highlighted by default in your editor.
Re:Why I Tend Not to Use Content Negotiation by divbzero on Monday November 20, 2023 @01:55PM (#38352629)
You can also easily serve *.html, *.json, and *.md as static files.