question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Make it possible to use file hashes in query strings instead of file name

See original GitHub issue

Clear and concise description of the problem

In our environment, there’s a specific whitelist for files permitted to be served. At the same time, I’d like to employ hashes to serve the most recent versions of the files.

I’d like to use hashes as query strings (eg. fileX.js?v=87654321) instead of part of the file name (fileX.87654321.js), as that won’t necessitate an update of the whitelist.

Updating the config with eg. build.rollupOptions.output.assetFileNames to assets/[name].[ext]?v=[hash] will just literally append ?v=87654321 to the actual file name.

Suggested solution

Config option (maybe it already exists in some form)

Alternative

No response

Additional context

No response

Validations

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:5 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
rubjocommented, Mar 31, 2022

@bluwy Thanks for the input!

Absolutely agree with you, although what’s frustrating is that one doesn’t always have complete control over the deployment chain / environment policies.

Anyway, you’re right that this is the domain of a custom plugin, and it was really quite easy to implement once I did a bit more research:

vite.config.js:

// Cache busting: append timestamp as hash
const htmlPlugin = () => {
  return {
    name: 'html-transform',
    transformIndexHtml(html) {
      const hash = Date.now()

      return html
        .replaceAll('.js', '.js?v=' + hash)
        .replaceAll('.css', '.css?v=' + hash)
    },
  }
}

…along with

export default defineConfig({
  plugins: [vue(), htmlPlugin()],
  build: {
    rollupOptions: {
      output: {
        entryFileNames: 'assets/[name].js',
        chunkFileNames: 'assets/[name].js',
        assetFileNames: 'assets/[name].[ext]',
      },
    },
  },
})
0reactions
ryoji-sagaracommented, Apr 1, 2022

Understood. I will comment again when I figure out the implementation. Thanks for the reply.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Put the hash in the query part of the filename rather than ...
Concerning the contents of the script - there's no universal answer, as it depends on your build configuration (i.e. which files do you...
Read more >
Adding Hashes to Filenames - SurviveJS
Cache invalidation can be achieved by including a hash to the filenames. Starting from version 5, webpack is using a deterministic way of...
Read more >
Use a querystring hash to defeat cache instead of filename hash
I need to change the cachebusting from [name].[contenthash:8].css' to [name].css?[contenthash:8]' because of the way our deploys happen.
Read more >
Query string hashing - Xperience 13 Documentation - Kentico
The hash function is calculated from the query string parameters using SHA-2. The hashing is used in various parts of the system: Dialogs...
Read more >
Bust That Cache Through A Content Hash - Alain Schlesser
The query string appended to the end of the URL makes some proxy servers assume that the content of that file is dynamic...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found