Skip to content

feat: add build.getPublicHash option#1377

Closed
aleclarson wants to merge 3 commits into
vitejs:mainfrom
aleclarson:feat/getPublicHash
Closed

feat: add build.getPublicHash option#1377
aleclarson wants to merge 3 commits into
vitejs:mainfrom
aleclarson:feat/getPublicHash

Conversation

@aleclarson
Copy link
Copy Markdown
Contributor

@aleclarson aleclarson commented Jan 5, 2021

Generate a revision hash for each asset in the public directory.

import revHash from 'rev-hash'
import fs from 'fs'

export default {
  build: {
    getPublicHash: file => revHash(fs.readFileSync(file)),
  },
}

Features

  • Easy HTTP caching of public/ assets. Cache forever, effortless "invalidation".
  • Internal: Avoids checkPublicFile calls when building, thereby reducing FS calls.

Generate a revision hash for each asset in the public directory.
@aleclarson aleclarson requested a review from yyx990803 January 5, 2021 20:38
@yyx990803
Copy link
Copy Markdown
Member

yyx990803 commented Jan 5, 2021

I don't know about this. Assets referenced from imports are already auto-hashed. So this means anything under assets are hashed - you just need to configure cache headers for those files.

The whole point of public is for those files that need to preserve exact filenames. It doesn't make sense to hash them.

@aleclarson
Copy link
Copy Markdown
Contributor Author

aleclarson commented Jan 5, 2021

I've been using public/ for all of the .svg, .png, and fonts in my website, since it takes zero configuration for short import paths (eg: /svg/foo.svg), and I prefer keeping static assets out of the src directory (without having both a public directory and an assets directory).

The alternatives are:

  • use relative import paths (less elegant, harder to maintain)
  • use paths feature in tsconfig.json and install vite-tsconfig-paths
  • custom plugin that rewrites /svg/* imports to config.root + "/assets/svg/[name]"

- ensure JS bundle references to public/ assets are rewritten
- add `publicUrls` property to ResolvedConfig
- copy public assets before HTML transforms are applied
@aleclarson
Copy link
Copy Markdown
Contributor Author

@knightly-bot build this

@yyx990803
Copy link
Copy Markdown
Member

I would prefer this to be a 3rd party plugin to not break the originally intended usage of public.

@aleclarson
Copy link
Copy Markdown
Contributor Author

aleclarson commented Jan 5, 2021

I tried that approach first, but it's not currently possible. If we had a hook for rewriting public filenames (eg: resolvePublicId), it would be pretty easy, I think.

I still think having it built-in is better, since it lets us avoid checkPublicFile calls in build mode.

@aleclarson
Copy link
Copy Markdown
Contributor Author

Removed the assetsCopied hook, in favor of publicUrls object added to ResolvedConfig.

@aleclarson
Copy link
Copy Markdown
Contributor Author

Closed in favor of #1647 and #1648

@aleclarson aleclarson closed this Jan 22, 2021
@aleclarson aleclarson deleted the feat/getPublicHash branch June 24, 2021 23:56
@aleclarson aleclarson restored the feat/getPublicHash branch June 24, 2021 23:56
@aleclarson aleclarson deleted the feat/getPublicHash branch June 24, 2021 23:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants