rohanray / next-fonts

Import fonts in Next.js (supports woff, woff2, eot, ttf, otf & svg)
179 stars 12 forks source link

are fonts cached? #23

Closed dlcoffee closed 4 years ago

dlcoffee commented 4 years ago

hi, thanks for creating this package!

i was wondering if it the fonts are supposed to be cached, and if it was possible to set the headers manually?

i checked out the https://github.com/rohanray/next-fonts-example repo, and didn't see any caching headers set on the font.

curl http://localhost:3000/fonts/Montserrat-ExtraLight.ttf -I

HTTP/1.1 200 OK
Accept-Ranges: bytes
Cache-Control: public, max-age=0
Last-Modified: Tue, 07 Apr 2020 06:29:21 GMT
ETag: W/"3b1d0-17153543a99"
Content-Type: font/ttf
Vary: Accept-Encoding
Content-Encoding: gzip
Date: Tue, 07 Apr 2020 16:07:58 GMT
Connection: keep-alive
Transfer-Encoding: chunked

is caching handled by next or this application?

as a workaround (since i'm running nextjs behind a custom express server), i'm basically doing this:

  server.get('/fonts/*', function (req, res, next) {
    res.set('Cache-Control', 'public, max-age=31557600')
    next()
  }

to set the cache control headers manually. however, this doesn't help me if i ever decide to break away from express and go serverless.

rohanray commented 4 years ago

@dlcoffee : I have never handled cache in nextjs itself. For cases with nextjs export i.e. static build, I put all the resources (html, css, js) in nginx web server. Cache, HTTP headers are handled in nginx itself.

For cases with custom server i.e. nextjs in combination with express, Cache is still handled at Nginx. HTTP headers are passed onto express for further parsing and processing as required.

I hope that answers your query.