Skip to content

feat(core): opt-in self-hosted Inter font via experimental.optimizedFonts - #3351

Merged
renatomaurovtex merged 2 commits into
fix/vte-18-partytown-migrationfrom
feat/core-optimized-fonts-inter
Jun 1, 2026
Merged

renatomaurovtex merged 2 commits into
fix/vte-18-partytown-migrationfrom
feat/core-optimized-fonts-inter

Conversation

@renatomaurovtex

@renatomaurovtex renatomaurovtex commented May 26, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Adds a new opt-in config flag experimental.optimizedFonts (default false). When enabled, FastStore self-hosts the Inter font via @fontsource/inter, eliminating the render-blocking request to fonts.googleapis.com and improving FCP by ~660ms on Lighthouse mobile.

This is the fourth slice of #3345.

Stacked PR. Base branch is fix/vte-18-partytown-migration (#3344), so the pnpm-lock.yaml diff here only shows the @fontsource/inter entries instead of the full lockfile reformat that the partytown PR already includes. After #3344 lands, this PR will be rebased onto dev automatically and the diff against dev will remain minimal.

How it works

  • discovery.config.default.js exposes experimental.optimizedFonts: false.
  • packages/core/src/fonts/inter.ts is an empty stub. When the flag is true, next.config.js uses webpack's NormalModuleReplacementPlugin to rewrite imports of src/fonts/inter to packages/core/fonts/inter.ts (located outside src/, so Babel never scans it).
  • packages/core/fonts/inter.ts side-effect imports the @fontsource/inter CSS files (400/500/600/700/900), which ship the self-hosted Inter .woff2 assets.
  • _app.tsx imports src/fonts/inter because Next.js requires global CSS imports to live in _app.tsx.
  • When the flag is false (default), webpack never bundles the package — zero cost when off.

Why plain CSS imports instead of next/font/google

The first attempt used next/font/google, which fails at build time for stores shipping a custom .babelrc.js (Next.js disables SWC, and next/font requires SWC). @fontsource/inter is compiler-agnostic plain CSS + woff2, so it works with both Babel and SWC.

Dependency justification (per AGENTS.md > Dependency Discipline)

Recorded in detail in packages/core/CHANGELOG.md:

  • Need: powers the opt-in experimental.optimizedFonts path; required by the Babel-safe constraint above.
  • Evaluation: MIT-licensed, actively maintained (5.x, regular releases), no critical CVEs, ships its own types/exports.
  • Bundle impact: zero when the flag is off — NormalModuleReplacementPlugin keeps it out of the module graph.
  • Bucket: dependencies (must be resolvable when stores opt in).
  • Pinning: declared locally in packages/core/package.json, caret-pinned to ^5.2.6.

Requesting maintainer approval per the Agent Autonomy Boundaries (new runtime dependency in a published package).

How to enable in a store

  1. In discovery.config.js:

    experimental: {
      optimizedFonts: true,
    }
  2. Remove any <link rel="stylesheet" href="https://fonts.googleapis.com/..."> from customizations/src/fonts/WebFonts.tsx to avoid duplicate font loads.

If the store uses a font other than Inter, do not enable this flag — keep the existing WebFonts.tsx configuration.

Test plan

  • With optimizedFonts: false (default): build is byte-identical to before, no .woff2 in the bundle.
  • With optimizedFonts: true: .woff2 files ship, no request to fonts.googleapis.com, Inter renders correctly across weights 400/500/600/700/900.
  • Babel store (custom .babelrc.js) builds cleanly with the flag on.
  • SWC store builds cleanly with the flag on.
  • pnpm test --filter=@faststore/core passes (new tests under test/fonts/).
  • Lighthouse mobile FCP improves on a real store with the flag enabled.

Made with Cursor

Summary by CodeRabbit

  • New Features

    • Added an opt-in experimental optimizedFonts flag to self-host the Inter font (defaults to false), enabling optional local font delivery to reduce external requests.
  • Documentation

    • Updated changelog with guidance for enabling the font optimization and related setup notes.
  • Chores

    • Added runtime dependency to support self-hosted Inter fonts.
  • Tests

    • Added unit tests to verify the default flag value and font-module behavior.

…onts

Adds a new opt-in config flag `experimental.optimizedFonts` (default
false) that, when enabled, self-hosts the Inter font via the
@fontsource/inter package. This eliminates the render-blocking request
to fonts.googleapis.com and improves FCP by ~660ms on Lighthouse mobile.

Implementation:
- Add @fontsource/inter ^5.2.6 as a runtime dependency of
  @faststore/core. Justification recorded in CHANGELOG per AGENTS.md
  Dependency Discipline.
- src/fonts/inter.ts is an empty stub. When optimizedFonts is true,
  next.config.js uses webpack's NormalModuleReplacementPlugin to
  rewrite imports of `src/fonts/inter` to fonts/inter.ts (located
  outside src/, so Babel never scans it). fonts/inter.ts side-effect
  imports the @fontsource/inter CSS files (400/500/600/700/900),
  which ship the self-hosted Inter .woff2 assets.
- _app.tsx imports `src/fonts/inter` because Next.js requires global
  CSS imports to live in _app.tsx.
- When the flag is false (default), webpack never bundles the package
  and the store's behavior is unchanged — zero cost when off.

Why plain CSS imports instead of next/font/google:
The first attempt used next/font/google, which fails at build time
for stores shipping a custom .babelrc.js (Next.js disables SWC, and
next/font requires SWC). @fontsource/inter is compiler-agnostic
plain CSS + woff2, so it works with both Babel and SWC.

Tests:
- test/fonts/inter.test.ts and test/fonts/optimizedFonts.test.ts
  validate the wiring.

Note: this PR is stacked on top of fix/vte-18-partytown-migration
(#3344) so the pnpm-lock.yaml diff is minimal (just the @fontsource/inter
entries). It will rebase onto dev once #3344 lands.

Co-authored-by: Cursor <[email protected]>
@renatomaurovtex
renatomaurovtex requested a review from a team as a code owner May 26, 2026 17:35
@renatomaurovtex
renatomaurovtex requested review from emersonlaurentino and hellofanny and removed request for a team May 26, 2026 17:35
@coderabbitai

coderabbitai Bot commented May 26, 2026 •

Copy link
Copy Markdown
Contributor

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: 5e32971f-8fac-47f9-a598-b902ef1ef803

📥 Commits

Reviewing files that changed from the base of the PR and between 8082ba7 and cbfa650.

⛔ Files ignored due to path filters (1)
  • pnpm-workspace.yaml is excluded by none and included by none
📒 Files selected for processing (2)
  • packages/core/package.json
  • packages/core/src/fonts/inter.ts
💤 Files with no reviewable changes (1)
  • packages/core/src/fonts/inter.ts
✅ Files skipped from review due to trivial changes (1)
  • packages/core/package.json

Walkthrough

This PR adds an opt-in experimental.optimizedFonts flag to enable self-hosting Inter via @fontsource/inter. Webpack conditionally replaces the stub src/fonts/inter with a real fonts/inter.ts implementation at build time; _app.tsx imports the font module and tests cover the stub/real behavior and default flag value.

Changes

Experimental optimized self-hosted Inter font

Layer / File(s) Summary
Configuration flag and dependencies
packages/core/discovery.config.default.js, packages/core/package.json, packages/core/CHANGELOG.md
Adds experimental.optimizedFonts (default false), documents the feature in the changelog, and adds @fontsource/inter to dependencies.
Font modules and webpack replacement
packages/core/fonts/inter.ts, packages/core/src/fonts/inter.ts, packages/core/next.config.js
Introduces a real font module that imports Inter weights, a Babel-safe stub, and a conditional NormalModuleReplacementPlugin to remap src/fonts/inter to the real module when the flag is enabled.
Application-level import
packages/core/src/pages/_app.tsx
Adds a side-effect import of src/fonts/inter so the font module (stub or real) is included in the application bundle.
Tests and mocks
packages/core/test/fonts/inter.test.ts, packages/core/test/fonts/optimizedFonts.test.ts
Mocks @fontsource/inter/* CSS imports, asserts the stub exports nothing, and verifies experimental.optimizedFonts defaults to false.

Sequence Diagram

sequenceDiagram
  participant NextBuild as Next.js build
  participant Webpack as Webpack
  participant NMRP as NormalModuleReplacementPlugin
  participant Stub as src/fonts/inter (stub)
  participant Real as packages/core/fonts/inter.ts (real)
  participant FontSource as `@fontsource/inter`

  NextBuild->>Webpack: initialize with storeConfig
  Webpack->>NMRP: register plugin when experimental.optimizedFonts = true
  NMRP->>Stub: intercept import `src/fonts/inter` -> replace with Real
  Real->>FontSource: import weight CSS (400,500,600,700,900)
  NextBuild->>Stub: if flag false, include Stub (no asset imports)
  NextBuild->>Real: if flag true, include Real (bundles font CSS assets)
Loading

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Suggested reviewers

  • ommeirelles
  • lariciamota

Poem

🌱 A flag to host Inter close and neat,
Build-time swap makes font weights meet.
Stub stays light when experiments sleep,
Enable to pull CSS from fontsource deep.
Small tests verify the default keep.

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately and concisely describes the main feature: an opt-in flag for self-hosting the Inter font via experimental.optimizedFonts config.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch feat/core-optimized-fonts-inter

Comment @coderabbitai help to get the list of available commands and usage tips.

@codesandbox-ci

Copy link
Copy Markdown

This pull request is automatically built and testable in CodeSandbox.

To see build info of the built libraries, click here or the icon next to each commit SHA.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@packages/core/package.json`:
- Line 48: Replace the hardcoded dependency entry "`@fontsource/inter`": "^5.2.6"
in packages/core/package.json with the workspace catalog reference
"`@fontsource/inter`": "catalog:" and then add or update the catalog section in
pnpm-workspace.yaml to include "`@fontsource/inter`": "<version-or-range>" so the
shared version is governed from pnpm-workspace.yaml; update the catalog mapping
rather than keeping the version inside the package.json.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: 01f29c11-cbbf-4e8c-b4a5-218124cb1e5c

📥 Commits

Reviewing files that changed from the base of the PR and between 953efdb and 8082ba7.

⛔ Files ignored due to path filters (1)
  • pnpm-lock.yaml is excluded by !**/pnpm-lock.yaml and included by none
📒 Files selected for processing (9)
  • packages/core/CHANGELOG.md
  • packages/core/discovery.config.default.js
  • packages/core/fonts/inter.ts
  • packages/core/next.config.js
  • packages/core/package.json
  • packages/core/src/fonts/inter.ts
  • packages/core/src/pages/_app.tsx
  • packages/core/test/fonts/inter.test.ts
  • packages/core/test/fonts/optimizedFonts.test.ts

Comment thread packages/core/package.json Resolved
@sonar-workflows

Copy link
Copy Markdown

Failed Quality Gate failed

  • 1 New Issues (is greater than 0)

Project ID: vtex_faststore_f0a862d5-9557-49f9-8d09-de40caa76622

View in SonarQube

Move @fontsource/inter to workspace catalog versioning and remove the empty export from the font stub to satisfy the Sonar rule while preserving the no-op module behavior.

Co-authored-by: Cursor <[email protected]>
@renatomaurovtex
renatomaurovtex merged commit d264f27 into fix/vte-18-partytown-migration Jun 1, 2026
4 of 7 checks passed
@renatomaurovtex
renatomaurovtex deleted the feat/core-optimized-fonts-inter branch June 1, 2026 19:28
lemagnetic pushed a commit that referenced this pull request Jun 16, 2026
…onts (#3351)

Adds a new opt-in config flag `experimental.optimizedFonts` (default
`false`). When enabled, FastStore self-hosts the Inter font via
`@fontsource/inter`, eliminating the render-blocking request to
`fonts.googleapis.com` and improving FCP by ~660ms on Lighthouse mobile.

This is the fourth slice of #3345.

> **Stacked PR.** Base branch is `fix/vte-18-partytown-migration`
(#3344), so the `pnpm-lock.yaml` diff here only shows the
`@fontsource/inter` entries instead of the full lockfile reformat that
the partytown PR already includes. After #3344 lands, this PR will be
rebased onto `dev` automatically and the diff against `dev` will remain
minimal.

- `discovery.config.default.js` exposes `experimental.optimizedFonts:
false`.
- `packages/core/src/fonts/inter.ts` is an empty stub. When the flag is
`true`, `next.config.js` uses webpack's `NormalModuleReplacementPlugin`
to rewrite imports of `src/fonts/inter` to
`packages/core/fonts/inter.ts` (located **outside** `src/`, so Babel
never scans it).
- `packages/core/fonts/inter.ts` side-effect imports the
`@fontsource/inter` CSS files (400/500/600/700/900), which ship the
self-hosted Inter `.woff2` assets.
- `_app.tsx` imports `src/fonts/inter` because Next.js requires global
CSS imports to live in `_app.tsx`.
- When the flag is `false` (default), webpack never bundles the package
— **zero cost when off**.

The first attempt used `next/font/google`, which fails at build time for
stores shipping a custom `.babelrc.js` (Next.js disables SWC, and
`next/font` requires SWC). `@fontsource/inter` is compiler-agnostic
plain CSS + woff2, so it works with both Babel and SWC.

Discipline](https://github.com/vtex/faststore/blob/dev/AGENTS.md#dependency-discipline))

Recorded in detail in `packages/core/CHANGELOG.md`:

- **Need:** powers the opt-in `experimental.optimizedFonts` path;
required by the Babel-safe constraint above.
- **Evaluation:** MIT-licensed, actively maintained (5.x, regular
releases), no critical CVEs, ships its own types/exports.
- **Bundle impact:** zero when the flag is off —
`NormalModuleReplacementPlugin` keeps it out of the module graph.
- **Bucket:** `dependencies` (must be resolvable when stores opt in).
- **Pinning:** declared locally in `packages/core/package.json`,
caret-pinned to `^5.2.6`.

Requesting maintainer approval per the Agent Autonomy Boundaries (new
runtime dependency in a published package).

1. In `discovery.config.js`:

   ```js
   experimental: {
     optimizedFonts: true,
   }
   ```

2. Remove any `<link rel="stylesheet"
href="https://fonts.googleapis.com/...">` from
`customizations/src/fonts/WebFonts.tsx` to avoid duplicate font loads.

If the store uses a font other than Inter, **do not enable this flag** —
keep the existing `WebFonts.tsx` configuration.

- [ ] With `optimizedFonts: false` (default): build is byte-identical to
before, no `.woff2` in the bundle.
- [ ] With `optimizedFonts: true`: `.woff2` files ship, no request to
`fonts.googleapis.com`, Inter renders correctly across weights
400/500/600/700/900.
- [ ] Babel store (custom `.babelrc.js`) builds cleanly with the flag
on.
- [ ] SWC store builds cleanly with the flag on.
- [ ] `pnpm test --filter=@faststore/core` passes (new tests under
`test/fonts/`).
- [ ] Lighthouse mobile FCP improves on a real store with the flag
enabled.

Made with [Cursor](https://cursor.com)

<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->

* **New Features**
* Added an opt-in experimental `optimizedFonts` flag to self-host the
Inter font (defaults to false), enabling optional local font delivery to
reduce external requests.

* **Documentation**
* Updated changelog with guidance for enabling the font optimization and
related setup notes.

* **Chores**
  * Added runtime dependency to support self-hosted Inter fonts.

* **Tests**
* Added unit tests to verify the default flag value and font-module
behavior.
<!-- end of auto-generated comment: release notes by coderabbit.ai -->

---------

Co-authored-by: Cursor <[email protected]>
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