Skip to content

Download icon sets on demand instead of shipping them in the ZIP - #211

Draft
Contributolo wants to merge 2 commits into
inline0:mainfrom
Contributolo:feat/icon-sets-on-demand
Draft

Contributolo wants to merge 2 commits into
inline0:mainfrom
Contributolo:feat/icon-sets-on-demand

Conversation

@Contributolo

Copy link
Copy Markdown
Collaborator

Closes #8. This is the second step after #210 and contains its commit, so please look at #210 first; only the last commit is new. It is a draft because it changes the release workflow, which I cannot run from a fork.

Result

The release ZIP no longer contains the 44 icon sets (22 MB unpacked, 13.4 MB ZIP to about 7.6 MB). It ships includes/icons.json with one SHA-256 checksum per set. A set is downloaded into wp-content/blockstudio/icons when it is needed.

How a set gets there

  • Picker: Rest::icons() downloads a missing set in that request, stores it and returns it. The picker and its retry on refocus are unchanged.
  • Rendering: bs_render_icon() never makes a request. An icon of a set that is not stored renders nothing, as an unknown set does today, and the set is scheduled for WP-Cron (blockstudio/icons/fetch), so templates that call bs_render_icon() with a set nobody picked in the editor heal on the next request.
  • Update: an existing site with missing sets gets all of them scheduled once. A site that went through Read icon sets from a directory outside the plugin first #210 has every set stored already and schedules nothing. A new install schedules nothing.
  • Source: https://github.com/inline0/blockstudio/releases/download/v<version>/blockstudio-icons-<set>.json. Only names from the checksum file are requested, the response is streamed to a temporary file, and it is kept only if it matches its checksum.

When the download fails

The background run stops at the first failure, shows a warning notice to administrators and retries hourly, so a server without access to GitHub makes one request per hour. During that hour render misses schedule nothing. Two ways out, both documented: blockstudio/icons/url points the download at another host, and an empty blockstudio/icons/dir (from #210) turns storing and downloading off.

Build and release

  • ships() in scripts/build-release.ts excludes includes/icons/. The build copies the sets to build/icons/blockstudio-icons-<set>.json.
  • release.yml uploads build/icons/*.json next to blockstudio.zip. Not run, see above.
  • dist.yml requires includes/icons.json in the ZIP and fails if includes/icons leaked.
  • generate-icons.ts writes the checksum file. The committed one was computed from the committed sets, and a unit test fails when the two differ.

Source checkouts and Composer installs still contain the sets and download nothing. I did not add export-ignore for them, so Composer archives are unchanged. Say so if you want them slimmed too.

Open points

  • Sets a site already stores are not refreshed when a later release changes them. They keep working with the older icons.
  • The notice is one new string. It is in the .pot, de_DE and de_DE_formal; the other locales are untouched and i18n:check will report them until the release fills them.
  • CI jobs that install the built ZIP have no release to download from. If one of them renders an icon, it needs the sets mounted or the URL filter.

Tests

tests/unit/icons/IconFetchTest.php (12 tests, HTTP faked through pre_http_request): checksum file matches every committed set, picker download and second call without a request, URL filter, checksum mismatch discarded, failed response, set without checksum never requested, empty directory requests nothing, render miss schedules without a request and renders after the cron run, failure recorded with notice and hourly retry and cleared on success, update schedules missing sets, update with everything stored schedules nothing, first activation downloads nothing.

Run locally in WordPress Playground: tests/unit/icons (78), tests/unit/migrate (40), tests/unit/rest (25), tests/unit/i18n (13) pass. node --test scripts/tests/build.test.ts, npm run lint, the tooling typecheck and npm run docs:check pass, PHPCS and PHPStan are clean on the changed files. Not run: the full suites, E2E, npm run build:release, and a real download, since no release has the assets yet.

The icon picker and bs_render_icon() share one lookup: a set stored in wp-content/blockstudio/icons wins, the bundled set is the fallback. Activation and updates copy the bundled sets there, so a later release can stop shipping them without existing sites losing an icon. The blockstudio/icons/dir filter moves the directory, and an empty string keeps the sets in the plugin only.
The release ZIP ships a checksum per icon set and no set. A set is downloaded from the release of the installed version when its picker first asks for it, and by WP-Cron when a render or an update finds it missing. Rendering never waits for the network, a download that does not match its checksum is discarded, and a failed download shows an admin notice and is retried hourly. The sets are built as release assets next to blockstudio.zip.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

On-demand icon definitions

1 participant