Home > Software design >  Why is Next.js adding a ?ts query parameter to static JS files, preventing loading them from cache?
Why is Next.js adding a ?ts query parameter to static JS files, preventing loading them from cache?

Time:12-17

On every page load, Next.js requests the same JS files with a query parameter such as ?ts=1234.

E.g:

/_next/static/chunks/pages/_app.js?ts=1671033077175
/_next/static/chunks/main.js?ts=1671033077175

This is obviously done to prevent reusing these files from the browser cache. But I want to reuse them since they are the same until a rebuild is deployed.

Why is this parameter added?

If it is to prevent using outdated code after a rebuild, then why not have a build version as the value instead of a timestamp?

Are there any available configuration options for caching static JS files?

CodePudding user response:

The timestamp query parameter is added only in development mode, i.e. if you start the next.js app with:

npx next dev

To start a production build, run:

npx next build
npx next start

This will allow caching of all JS files, as well as make them much leaner and faster by removing all of the development and debug tools.

  • Related