8000 Update compiled views only if they actually changed by pizkaz · Pull Request #55450 · laravel/framework · GitHub
[go: up one dir, main page]

Skip to content

Update compiled views only if they actually changed #55450

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

Conversation

pizkaz
Copy link
Contributor
@pizkaz pizkaz commented Apr 17, 2025

When running in a read-only filesystem container with views pre-cached at build time, Laravel checks if the cached view's timestamp is older than the source's to decide when to recompile. This is fine as long as we do not make OCI image builds reproducible.
If we do however, the timestamps match (because timestamps of all files are set to 01.01.1970), so Laravel recompiles the view, tries to write to a read-only filesystem and the container crashes.

This patch computes SHA256 hashes over existing and newly compiled file contents, compares those and writes only if the file actually changes.

@pizkaz pizkaz force-pushed the impr/view-compiler-write-only-if-changed branch 4 times, most recently from 663fa79 to fe0de84 Compare April 17, 2025 14:01
When running in a read-only filesystem container with views pre-cached
at build time, Laravel checks if the cached view's timestamp is older
than the source's to decide when to recompile. This is fine as long as
we do not make OCI image builds reproducible.
If we do however, the timestamps match (because timestamps of all files
are set to 01.01.1970), so Laravel recompiles the view, tries to write
to a read-only filesystem and the container crashes.

This patch computes SHA256 hashes over existing and newly compiled file
contents, compares those and writes only if the file actually changes.
@pizkaz pizkaz force-pushed the impr/view-compiler-write-only-if-changed branch from fe0de84 to 5f39764 Compare April 17, 2025 14:09
@pizkaz
Copy link
Contributor Author
pizkaz commented Apr 17, 2025

Now with proper Unit tests. \o/

@rodrigopedra
Copy link
Contributor
rodrigopedra commented Apr 17, 2025

Question: will this calculate hashes for every view on every request?

Never mind, figure out already. It only checks the hash when it needs to compile. In other words, after checking for the timestamp.

return;
}

$contentHash = hash('sha256', $contents);
Copy link
Contributor

Choose a reason for hiding this comment

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

Copy link
Contributor Author

Choose a reason for hiding this comment

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

xxh128 seems like a better choice. I will change it.

@taylorotwell
Copy link
Member

Yeah, could use xxh128 since we're using that in other areas.

@taylorotwell taylorotwell marked this pull request as draft April 18, 2025 12:53
@taylorotwell taylorotwell marked this pull request as ready for review April 21, 2025 14:09
@taylorotwell taylorotwell merged commit cea87be into laravel:12.x Apr 21, 2025
58 checks passed
@pizkaz
Copy link
Contributor Author
pizkaz commented Apr 24, 2025

Sorry, I was on vacation. Actually, I realized that this effectively defeats the purpose of view caching as every view is compiled every time it is accessed.

I noticed the shouldCache flag but it does the opposite of what I need. I will open a new PR with a new config flag view.ignore_cache_timestamps.

@pizkaz pizkaz deleted the impr/view-compiler-write-only-if-changed branch April 24, 2025 08:25
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.

4 participants
0