lwsjs / local-web-server

A lean, modular web server for rapid full-stack development.
MIT License
1.22k stars 86 forks source link

Content type for css fonts #102

Closed oliveti closed 6 years ago

oliveti commented 6 years ago

Hi,

The web server is not setting the content type for css fonts. This is resulting in issues with caching these fonts.

You can reproduce issue with this repo : https://github.com/oliveti/angular-sw-font-bug Run ng build --prod and run server from dist.

Or have a look at 'content type' missing in screen captures :

With http-server (OK)

screenshot 2018-10-01 at 10 05 32

With live-web-server (NOK)

screenshot 2018-10-01 at 09 57 00

Thank you

75lb commented 6 years ago

If the ws response is correct (first screenshot) and the service worker response is broken, then doesn't that indicate an issue with the service worker and not ws?

oliveti commented 6 years ago

Actually first screenshot is with another web server http-server, but the service worker call happen Because of the missing content type

75lb commented 6 years ago

I downloaded and tested your project, everything is working fine.. the right tab shows both a ttf and woff2 file responding with the correct content-type. Please clear all your service workers and try again.

screenshot 2018-10-01 at 10 09 33

oliveti commented 6 years ago

I tried also directly with curl, issue seems to come from cache :

MBP-de-Olivier:temp olsp$ curl -I 'http://localhost:4200/assets/fonts/Roboto-Regular.ttf' -H 'If-None-Match: W/"29e9c-1662e850f73"'
HTTP/1.1 304 Not Modified
Vary: Origin
Accept-Ranges: bytes
Last-Modified: Mon, 01 Oct 2018 07:26:04 GMT
Cache-Control: max-age=0
ETag: W/"29e9c-1662e850f73"
Date: Mon, 01 Oct 2018 09:21:24 GMT
Connection: keep-alive

MBP-de-Olivier:temp olsp$ curl -I 'http://localhost:4200/assets/fonts/Roboto-Regular.ttf' -H 'If-None-Match: W/"29e9c-1662e850f73-modified"'
$HTTP/1.1 200 OK
Vary: Origin
Accept-Ranges: bytes
Content-Length: 171676
Last-Modified: Mon, 01 Oct 2018 07:26:04 GMT
Cache-Control: max-age=0
Content-Type: font/ttf
ETag: W/"29e9c-1662e850f73"
Date: Mon, 01 Oct 2018 09:21:33 GMT
Connection: keep-alive

But in that case that's an expected behaviour. 304 should not contain any content type header.

75lb commented 6 years ago

it's not an issue, it's the correct behaviour.

oliveti commented 6 years ago

But still, something is preventing the service worker to cache the font with this web server, I will try to investigate a bit more on this.

oliveti commented 6 years ago

I think what is preventing the service worker from caching could be the behaviour to return an Access-Control-Allow-Origin whenever ws receive an Origin header. And it seems that Chrome is sending an Origin header for font calls made from css.

screenshot 2018-10-01 at 12 00 05
oliveti commented 6 years ago

Do you want me to open another ticket for this issue ? Would be great to be able to enjoy service workers with this tool.

75lb commented 6 years ago

it's not a bug, no need for another ticket thanks.. If you don't require blanket CORS behaviour, remove CORS from the stack.. Read this wiki page on using middleware then specify a stack which suits you better, e.g. just spa, static and index: ws --stack spa static index.

oliveti commented 6 years ago

Thank to you for your detailed explanations, that's working like a charm !

75lb commented 6 years ago

Good, pleased to hear that. If you feel the documentation could be more clear, feel free to edit the wiki - thanks. 👍