Repository navigation
feat(core): opt-in self-hosted Inter font via experimental.optimizedFonts - #3351
renatomaurovtex merged 2 commits into
Conversation
…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]>
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Path: .coderabbit.yaml Review profile: CHILL Plan: Pro Run ID: ⛔ Files ignored due to path filters (1)
📒 Files selected for processing (2)
💤 Files with no reviewable changes (1)
✅ Files skipped from review due to trivial changes (1)
WalkthroughThis PR adds an opt-in ChangesExperimental optimized self-hosted Inter font
Sequence DiagramsequenceDiagram
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)
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~20 minutes Suggested reviewers
Poem
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches📝 Generate docstrings
🧪 Generate unit tests (beta)
Comment |
|
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. |
There was a problem hiding this comment.
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
⛔ Files ignored due to path filters (1)
pnpm-lock.yamlis excluded by!**/pnpm-lock.yamland included by none
📒 Files selected for processing (9)
packages/core/CHANGELOG.mdpackages/core/discovery.config.default.jspackages/core/fonts/inter.tspackages/core/next.config.jspackages/core/package.jsonpackages/core/src/fonts/inter.tspackages/core/src/pages/_app.tsxpackages/core/test/fonts/inter.test.tspackages/core/test/fonts/optimizedFonts.test.ts
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]>
d264f27
into
fix/vte-18-partytown-migration
…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]>

Summary
Adds a new opt-in config flag
experimental.optimizedFonts(defaultfalse). When enabled, FastStore self-hosts the Inter font via@fontsource/inter, eliminating the render-blocking request tofonts.googleapis.comand improving FCP by ~660ms on Lighthouse mobile.This is the fourth slice of #3345.
How it works
discovery.config.default.jsexposesexperimental.optimizedFonts: false.packages/core/src/fonts/inter.tsis an empty stub. When the flag istrue,next.config.jsuses webpack'sNormalModuleReplacementPluginto rewrite imports ofsrc/fonts/intertopackages/core/fonts/inter.ts(located outsidesrc/, so Babel never scans it).packages/core/fonts/inter.tsside-effect imports the@fontsource/interCSS files (400/500/600/700/900), which ship the self-hosted Inter.woff2assets._app.tsximportssrc/fonts/interbecause Next.js requires global CSS imports to live in_app.tsx.false(default), webpack never bundles the package — zero cost when off.Why plain CSS imports instead of
next/font/googleThe first attempt used
next/font/google, which fails at build time for stores shipping a custom.babelrc.js(Next.js disables SWC, andnext/fontrequires SWC).@fontsource/interis 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:experimental.optimizedFontspath; required by the Babel-safe constraint above.NormalModuleReplacementPluginkeeps it out of the module graph.dependencies(must be resolvable when stores opt 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
In
discovery.config.js:Remove any
<link rel="stylesheet" href="https://fonts.googleapis.com/...">fromcustomizations/src/fonts/WebFonts.tsxto avoid duplicate font loads.If the store uses a font other than Inter, do not enable this flag — keep the existing
WebFonts.tsxconfiguration.Test plan
optimizedFonts: false(default): build is byte-identical to before, no.woff2in the bundle.optimizedFonts: true:.woff2files ship, no request tofonts.googleapis.com, Inter renders correctly across weights 400/500/600/700/900..babelrc.js) builds cleanly with the flag on.pnpm test --filter=@faststore/corepasses (new tests undertest/fonts/).Made with Cursor
Summary by CodeRabbit
New Features
optimizedFontsflag to self-host the Inter font (defaults to false), enabling optional local font delivery to reduce external requests.Documentation
Chores
Tests