Repository navigation
Lighten the public pages and clean their sitemap and meta tags #486
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
+320
−37
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
68b1af5
Lighten the public pages and clean their sitemap and meta tags
fabiodalez-dev 5743f40
Serve the translation script without a .js extension
fabiodalez-dev b5a4a97
Load the admin translation script in the head
fabiodalez-dev 47c1fa1
Filter unroutable names before the sitemap LIMIT, version the transla…
fabiodalez-dev ed1cfbb
test: cover translation script safety and canonical CMS URLs
fabiodalez-dev 1ae4aff
Put back a locale file the translation-script probes would overwrite
fabiodalez-dev File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,70 @@ | ||
| <?php | ||
| declare(strict_types=1); | ||
|
|
||
| namespace App\Support; | ||
|
|
||
| /** | ||
| * The JavaScript translation table, served as one cacheable script per locale. | ||
| * | ||
| * The layouts used to inline the whole locale file in every page: about 600 KB of | ||
| * HTML on each request (8,000+ strings), and on an Italian install every entry | ||
| * mapped a string to itself. As a script versioned by the file's content, the | ||
| * browser downloads it once per release and reuses it on every page. | ||
| */ | ||
| final class TranslationScript | ||
| { | ||
| /** Route path, without the locale. */ | ||
| public const ROUTE_PREFIX = '/i18n/'; | ||
|
|
||
| /** | ||
| * URL of the script for a locale, versioned so a new translation file is | ||
| * fetched at once while an unchanged one stays cached for a year. | ||
| * | ||
| * The version hashes the file's content, not its mtime and size: a deploy that | ||
| * keeps timestamps can change a translation without changing the size, and the | ||
| * immutable URL would then serve the old table for a year. xxh3 reads the | ||
| * ~650 KB file in about 0.1 ms. | ||
| */ | ||
| public static function url(string $locale): string | ||
| { | ||
| $file = self::file($locale); | ||
| $hash = $file !== null ? hash_file('xxh3', $file) : false; | ||
| $version = $hash !== false ? substr($hash, 0, 12) : '0'; | ||
| // No ".js" extension: see the route in app/Routes/web.php. | ||
| return url(self::ROUTE_PREFIX . rawurlencode($locale)) . '?v=' . $version; | ||
| } | ||
|
|
||
| /** | ||
| * Body of the script, or null for a locale with no translation file. | ||
| */ | ||
| public static function body(string $locale): ?string | ||
| { | ||
| $file = self::file($locale); | ||
| if ($file === null) { | ||
| return null; | ||
| } | ||
| $decoded = json_decode((string) file_get_contents($file), true); | ||
| // The keys are the Italian source strings and __() falls back to the key, | ||
| // so an entry that maps a string to itself only adds weight. | ||
| $translations = array_filter( | ||
| is_array($decoded) ? $decoded : [], | ||
| static fn ($value, $key): bool => $value !== $key, | ||
| ARRAY_FILTER_USE_BOTH | ||
| ); | ||
| $json = json_encode( | ||
| (object) $translations, | ||
| JSON_UNESCAPED_UNICODE | JSON_HEX_TAG | JSON_HEX_AMP | JSON_HEX_QUOT | JSON_HEX_APOS | ||
| ); | ||
| return 'window.i18nTranslations = ' . ($json !== false ? $json : '{}') . ";\n"; | ||
| } | ||
|
|
||
| private static function file(string $locale): ?string | ||
| { | ||
| // Only a locale code reaches the filesystem: no path from the URL. | ||
| if (preg_match('/^[a-z]{2}_[A-Z]{2}$/D', $locale) !== 1) { | ||
| return null; | ||
| } | ||
| $file = dirname(__DIR__, 2) . '/locale/' . $locale . '.json'; | ||
| return is_file($file) ? $file : null; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.