Skip to content

[AssetMapper] Truncate public digests to 8 characters #57879

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 our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 4, 2024

Conversation

smnandre
Copy link
Member

@smnandre smnandre commented Jul 29, 2024

Q A
Branch? 7.2
Bug fix? no
New feature? yes
Deprecations? no
Issues Fix #...
License MIT

⚠️ 2024-08-03 Update: Changes Made Based on Feedback

This pull request (PR) now shortens the public digest of mapped assets to 8 characters.

The digest is used in the filenames of deployed assets, leading to a tangible reduction in HTML size, particularly in the generated import map.

--

Original message:

I've been wanting to use shorter digests in my mapped assets / importmap URL's.
This PR adds an hashAlgorithm argument to the MappedAssetFactory to do so.

I'm not sure this is enough/good choice, as we need some requirement regarding the digests (7+ caracters, alnum only)
But adding an entire AssetHasherInterface would be probably overkill, so i'm very open for feedback / ideas there.

(I did not put FrameworkBundle modifications in this PR, as the discussion will probably have an impact on what's to do)

@smnandre smnandre requested review from xabbuh and OskarStark July 30, 2024 19:29
Copy link
Member

@derrabus derrabus left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Introducing an interface would've been the more flexible solution, but it's probably fine that we only allow hash algorithms that are built into PHP for now.

Copy link
Member

@nicolas-grekas nicolas-grekas left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the goal is to generate shorter hashes, then changing the algorithm is not going to be the right solution IMHO.
Using base64 (or base58) would have the most substantial impact to me.
And if this not enough, truncating the hash.
Considering these, the most flexible option would be to accept a closure instead. This doesn't require a full abstraction (aka interface) while still providing all the needed flexibility.

But are we sure we want to make this configurable? Can't we update the hash-generation logic so that everybody will get a shorter hash without being exposed to too-short hashes?

@smnandre
Copy link
Member Author

smnandre commented Aug 1, 2024

You're right, this implementation is in fact not required to do what i initially wanted.

I think i can find another method via the "public path" & the asset package... to keep unique digest locally while exposing shorter ones.

I'll rework this later today

@OskarStark
Copy link
Contributor

Shorter hashes for everyone 👌🏻

@smnandre
Copy link
Member Author

smnandre commented Aug 2, 2024

Webpack seems to have two options hashDigest and hashDigestLenth

I don't think there would be any problem if we allow to reduce digests down to 7 caracters (the risk of collision for one given file is practically null).

So instead of configure the hash, could we add an option "digestLength" / "hashLength" ?

@nicolas-grekas with this "hashLenght" idea, would a closure still be necessary ? I'd rather avoid it, as it would create difficulties: we need to enforce some things currently to extract/identify the digest part in file names.

@nicolas-grekas
Copy link
Member

Why make it configurable at all ? Base64+7chars and done, no ?

@javiereguiluz
Copy link
Member

I agree with Nicolas and I don't think we should make this configurable ... but if we think we can change this for all to improve it, let's change it. Thanks.

@smnandre
Copy link
Member Author

smnandre commented Aug 3, 2024

Let's shorten the public digest for everyone then :)

@nicolas-grekas i suggest we keep only hexa characters for now, as it's safer to catch (in dev) or identifiy (i'm thinking web servers or CDN using url patterns, log exclusion, ...) .. wdyt ?

If we truncate down to 7 caracters we keep the same "contract" than before.

(PR update incoming)

@smnandre smnandre changed the title [AssetMapper] Make digest hash algorithm configurable [AssetMapper] Truncate public digests to 8 characters Aug 3, 2024
@smnandre
Copy link
Member Author

smnandre commented Aug 3, 2024

PR Updated

I reverted what i did before (with hash algorithm)

It now only truncates the public digest down to 8 caracters

(8 feels more similar to what can be found in gulp/webpack/etc. and still way shorter than today)

This solution is fully compatible with the current behaviour and requires no change on config/framework/recipe/ or user code.

Thanks everyone for the feedback / ideas

--

PHP 8.2 high-deps failures unrelated (doctrine)

@nicolas-grekas
Copy link
Member

I'd do the base64 now because that allows a lot more entropy per character. We could then truncate to 7 bytes.

@smnandre
Copy link
Member Author

smnandre commented Aug 3, 2024

@nicolas-grekas added base64 encode & now truncate to 7 !

--

PHP 8.4 failures unrelated (mime)

@fabpot fabpot force-pushed the feat/asset-mapper-digest-algo branch from 3fa211d to e1ae985 Compare August 4, 2024 14:36
@fabpot
Copy link
Member

fabpot commented Aug 4, 2024

Thank you @smnandre.

@fabpot fabpot merged commit 0862610 into symfony:7.2 Aug 4, 2024
8 of 9 checks passed
@fabpot fabpot mentioned this pull request Oct 27, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

9 participants