- Notifications
You must be signed in to change notification settings - Fork1k
feat: add caching headers for Next.js static assets#330
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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Uh oh!
There was an error while loading.Please reload this page.
Changes fromall commits
File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -49,10 +49,7 @@ func Handler(fileSystem fs.FS, options *Options) (http.Handler, error) { | ||
} | ||
// Fallback to static file server for non-HTML files | ||
router.NotFound(FileHandler(fileSystem)) | ||
// Finally, if there is a 404.html available, serve that | ||
err = register404(fileSystem, router, *options) | ||
@@ -64,6 +61,31 @@ func Handler(fileSystem fs.FS, options *Options) (http.Handler, error) { | ||
return router, nil | ||
} | ||
// FileHandler serves static content, additionally adding immutable | ||
// cache-control headers for Next.js content | ||
func FileHandler(fileSystem fs.FS) func(writer http.ResponseWriter, request *http.Request) { | ||
// Non-HTML files don't have special routing rules, so we can just leverage | ||
// the built-in http.FileServer for it. | ||
fileHandler := http.FileServer(http.FS(fileSystem)) | ||
return func(writer http.ResponseWriter, request *http.Request) { | ||
// From the Next.js documentation: | ||
// | ||
// "Caching improves response times and reduces the number | ||
// of requests to external services. Next.js automatically | ||
// adds caching headers to immutable assets served from | ||
// /_next/static including JavaScript, CSS, static images, | ||
// and other media." | ||
// | ||
// See: https://nextjs.org/docs/going-to-production | ||
if strings.HasPrefix(request.URL.Path, "/_next/static/") { | ||
writer.Header().Add("Cache-Control", "public, max-age=31536000, immutable") | ||
} | ||
Comment on lines +81 to +83 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more. Looks great! Thanks for bringing this over@jawnsy 👍 | ||
fileHandler.ServeHTTP(writer, request) | ||
} | ||
} | ||
// registerRoutes recursively traverses the file-system, building routes | ||
// as appropriate for respecting NextJS dynamic rules. | ||
func registerRoutes(rtr chi.Router, fileSystem fs.FS, options Options) error { | ||
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -376,6 +376,42 @@ func TestNextRouter(t *testing.T) { | ||
require.EqualValues(t, "test-create", body) | ||
}) | ||
t.Run("Caching headers for _next resources", func(t *testing.T) { | ||
t.Parallel() | ||
rootFS := fstest.MapFS{ | ||
"index.html": &fstest.MapFile{ | ||
Data: []byte("test-root"), | ||
}, | ||
"_next/static/test.js": &fstest.MapFile{ | ||
Data: []byte("test.js cached forever"), | ||
}, | ||
"_next/static/chunks/app/test.css": &fstest.MapFile{ | ||
Data: []byte("test.css cached forever"), | ||
}, | ||
} | ||
router, err := nextrouter.Handler(rootFS, nil) | ||
require.NoError(t, err) | ||
server := httptest.NewServer(router) | ||
t.Cleanup(server.Close) | ||
res, err := request(server, "/index.html") | ||
require.NoError(t, err) | ||
require.NoError(t, res.Body.Close()) | ||
Comment on lines +400 to +402 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more. Nice tests 👍 | ||
require.Equal(t, http.StatusOK, res.StatusCode) | ||
require.Empty(t, res.Header.Get("Cache-Control")) | ||
res, err = request(server, "/_next/static/test.js") | ||
require.NoError(t, err) | ||
require.NoError(t, res.Body.Close()) | ||
require.Equal(t, http.StatusOK, res.StatusCode) | ||
require.Equal(t, "public, max-age=31536000, immutable", res.Header.Get("Cache-Control")) | ||
}) | ||
t.Run("Injects template parameters", func(t *testing.T) { | ||
t.Parallel() | ||