Point of fairly idle curiosity about the presentation of the article: why do you put a trailing slash on your empty elements (img, link) in your code samples? Some aren’t aware that the trailing slash is useless in the HTML syntax, simply being ignored by the parser and not doing anything. (Except for in inline SVG and MathML content, which switch the parser into a more XML-like mode where the trailing slash behaves…
Author here! I used to have strong feelings about formatting stuff like this, but I since realised there are better things to spend effort on. For formatting, I just let https://prettier.io/ do it's thing, and it added the />. Although I do configure it to use single quotes in JS, so I guess I still have some opinion there. In terms of HTML, how far does your "but it isn't necessary" opinion go? Lots of closing eleme…
How to win at CORS
31–40 of 128 posts
Re: How to win at CORS
#32Earlier quoted context omitted.
Aren't they optional 100% of the time though?
Not 100%. There are a small handful of really wicked gotchas. I think there’s a lot of articles on them. I can’t find the one I like and don’t want to share one I haven’t read yet.
In this first example, ASI inserts an undesired semicolon:
return
{a: 0}
This returns undefined, and doesn’t continue on to execute the block containing a statement 0 with label a. (Change it to {a: 0, b: 0} and you get a syntax error because of this reinterpretation of what was intended as an object literal.)In this second example, ASI doesn’t insert a desired semicolon:
f()
[].forEach.call(…)
This becomes a syntax error, because the [] has become subscripting rather than an array literal. (Incidentally, [].forEach is smelly anyway; prefer Array.prototype.forEach, maybe assign that to a constant if you’re doing it much.)Re: How to win at CORS
#33How to win at CORS: Don't use it. Just put the apis you need on the same domain, use a reverse proxy. Same-site just works, always.
Re: How to win at CORS
#34Earlier quoted context omitted.
Well, for my own personal stuff I omit just about all that I can—head/body start and end tags, html end tag (not start tag because it has at least a lang attribute), tbody start tag where possible, thead/tbody/tfoot/tr/th/td/li/dt/dd/p end tags almost all of the time, attribute value quotes where valid… mostly just because it’s fun doing so, and in some cases because it makes things decidedly cleaner (especially tabl…
Seems like your HTML formatting opinions are very similar to the owner of the fetch spec! Yeah, I don't always agree with Prettier, but ugh, I wasted hours in my early career arguing about formatting with teammates, but now I just let Prettier do it's thing, get over it, and spend the time on something else.
Re: How to win at CORS
#35Earlier quoted context omitted.
The name is literally a "self-closing tag", isn't it? And it's better for someone else reading: you may not recall what the tag is, but you know you don't have to look for a closing tag below.
But that’s the thing— it doesn’t do that . If you want an empty div, you can’t write , because that’s equivalent to just ; you’ll have to write instead.
Re: How to win at CORS
#36Earlier quoted context omitted.
Author here! The post covers this detail. This happens because your response is missing a Vary header. Getting Vary right isn't just important for Chrome, it's important for CDNs too.
You're right, the real issue is CloudFront won't include Origin in the Vary response header if it wasn't included in the initial request. And if you change your HTML attributes, you're changing your request, but you essentially end up with a poisoned local cache. Rolling out crossorigin="anonymous" on previously cached assets is a subtlety you won't know about (even if you think you know CORS) until your site breaks…
Hmm, I think I'll add a section to the article on this when I'm back at my laptop.
Re: How to win at CORS
#37When I'm writing some frontend that is hosted on localhost, with an API that is hosted on its domain somewhere, it always is some sort of PITA to get the dev environ started.
There's a plugin for firefox that ignores CORS which is helpful for this. It's becoming less useful for me as my APIs now usually have a toggle to add a cross origin header which allows localhost. Still useful.
Re: How to win at CORS
#38Re: How to win at CORS
#39Re: How to win at CORS
#40Earlier quoted context omitted.
To be fair, there are many web devs who have never needed to worry about cross origin resource sharing
Usually the same ones who then download plugins like https://addons.mozilla.org/en-US/firefox/addon/access-contro... because properly configuring the backend is ¨too complicated¨. Been there, done that.
But I was referring to legacy code (or those whose SPA is stored on the same domain as API endpoints).