Unverified Commit 7fd0f775 authored by Hannes Bornö's avatar Hannes Bornö Committed by GitHub

Reuse file when shared by several google font variants (#42480)

If a Google font is available as a variable font - the different variants may reuse the same file when you ask for specific variants. Make sure to reuse the same file and don't make additional requests.

## Bug

- [ ] Related issues linked using `fixes #number`
- [ ] Integration tests added
- [ ] Errors have a helpful link attached, see `contributing.md`

## Feature

- [ ] Implements an existing feature request or RFC. Make sure the feature request has been accepted for implementation before opening a PR.
- [ ] Related issues linked using `fixes #number`
- [ ] Integration tests added
- [ ] Documentation added
- [ ] Telemetry added. In case of a feature if it's used or not.
- [ ] Errors have a helpful link attached, see `contributing.md`

## Documentation / Examples

- [ ] Make sure the linting passes by running `pnpm build && pnpm lint`
- [ ] The "examples guidelines" are followed from [our contributing doc](https://github.com/vercel/next.js/blob/canary/contributing/examples/adding-examples.md)
parent 086d4340
...@@ -79,7 +79,12 @@ const downloadGoogleFonts: FontLoader = async ({ ...@@ -79,7 +79,12 @@ const downloadGoogleFonts: FontLoader = async ({
currentSubset = newSubset currentSubset = newSubset
} else { } else {
const googleFontFileUrl = /src: url\((.+?)\)/.exec(line)?.[1] const googleFontFileUrl = /src: url\((.+?)\)/.exec(line)?.[1]
if (googleFontFileUrl) { if (
googleFontFileUrl &&
!fontFiles.some(
(foundFile) => foundFile.googleFontFileUrl === googleFontFileUrl
)
) {
fontFiles.push({ fontFiles.push({
googleFontFileUrl, googleFontFileUrl,
preloadFontFile: preloadFontFile:
...@@ -119,7 +124,7 @@ const downloadGoogleFonts: FontLoader = async ({ ...@@ -119,7 +124,7 @@ const downloadGoogleFonts: FontLoader = async ({
// Replace @font-face sources with self-hosted files // Replace @font-face sources with self-hosted files
let updatedCssResponse = fontFaceDeclarations let updatedCssResponse = fontFaceDeclarations
for (const { googleFontFileUrl, selfHostedFileUrl } of downloadedFiles) { for (const { googleFontFileUrl, selfHostedFileUrl } of downloadedFiles) {
updatedCssResponse = updatedCssResponse.replace( updatedCssResponse = updatedCssResponse.replaceAll(
googleFontFileUrl, googleFontFileUrl,
selfHostedFileUrl selfHostedFileUrl
) )
......
...@@ -12,6 +12,8 @@ const frauncesMultiple = Fraunces({ ...@@ -12,6 +12,8 @@ const frauncesMultiple = Fraunces({
axes: ['SOFT', 'WONK', 'opsz'], axes: ['SOFT', 'WONK', 'opsz'],
}) })
const frauncesMultipleWeights = Fraunces({ weight: ['100', '400', '900'] })
export default function WithFonts() { export default function WithFonts() {
return ( return (
<> <>
...@@ -27,6 +29,12 @@ export default function WithFonts() { ...@@ -27,6 +29,12 @@ export default function WithFonts() {
<div id="multiple-fraunces" className={frauncesMultiple.className}> <div id="multiple-fraunces" className={frauncesMultiple.className}>
{JSON.stringify(frauncesMultiple)} {JSON.stringify(frauncesMultiple)}
</div> </div>
<div
id="multiple-weights-fraunces"
className={frauncesMultipleWeights.className}
>
{JSON.stringify(frauncesMultipleWeights)}
</div>
</> </>
) )
} }
...@@ -853,4 +853,87 @@ module.exports = { ...@@ -853,4 +853,87 @@ module.exports = {
unicode-range: U+0000-00FF, U+0131, U+0152-0153, U+02BB-02BC, U+02C6, U+02DA, U+02DC, U+2000-206F, U+2074, U+20AC, U+2122, U+2191, U+2193, U+2212, U+2215, U+FEFF, U+FFFD; unicode-range: U+0000-00FF, U+0131, U+0152-0153, U+02BB-02BC, U+02C6, U+02DA, U+02DC, U+2000-206F, U+2074, U+20AC, U+2122, U+2191, U+2193, U+2212, U+2215, U+FEFF, U+FFFD;
} }
`, `,
'https://fonts.googleapis.com/css2?family=Fraunces:wght@100;400;900&display=optional': `
/* vietnamese */
@font-face {
font-family: 'Fraunces';
font-style: normal;
font-weight: 100;
font-display: optional;
src: url(https://fonts.gstatic.com/s/fraunces/v24/6NUu8FyLNQOQZAnv9bYEvDiIdE9Ea92uemAk_WBq8U_9v0c2Wa0K7iN7hzFUPJH58nib14c0qv8oRcTnaIM.woff2) format('woff2');
unicode-range: U+0102-0103, U+0110-0111, U+0128-0129, U+0168-0169, U+01A0-01A1, U+01AF-01B0, U+1EA0-1EF9, U+20AB;
}
/* latin-ext */
@font-face {
font-family: 'Fraunces';
font-style: normal;
font-weight: 100;
font-display: optional;
src: url(https://fonts.gstatic.com/s/fraunces/v24/6NUu8FyLNQOQZAnv9bYEvDiIdE9Ea92uemAk_WBq8U_9v0c2Wa0K7iN7hzFUPJH58nib14c1qv8oRcTnaIM.woff2) format('woff2');
unicode-range: U+0100-024F, U+0259, U+1E00-1EFF, U+2020, U+20A0-20AB, U+20AD-20CF, U+2113, U+2C60-2C7F, U+A720-A7FF;
}
/* latin */
@font-face {
font-family: 'Fraunces';
font-style: normal;
font-weight: 100;
font-display: optional;
src: url(https://fonts.gstatic.com/s/fraunces/v24/6NUu8FyLNQOQZAnv9bYEvDiIdE9Ea92uemAk_WBq8U_9v0c2Wa0K7iN7hzFUPJH58nib14c7qv8oRcTn.woff2) format('woff2');
unicode-range: U+0000-00FF, U+0131, U+0152-0153, U+02BB-02BC, U+02C6, U+02DA, U+02DC, U+2000-206F, U+2074, U+20AC, U+2122, U+2191, U+2193, U+2212, U+2215, U+FEFF, U+FFFD;
}
/* vietnamese */
@font-face {
font-family: 'Fraunces';
font-style: normal;
font-weight: 400;
font-display: optional;
src: url(https://fonts.gstatic.com/s/fraunces/v24/6NUu8FyLNQOQZAnv9bYEvDiIdE9Ea92uemAk_WBq8U_9v0c2Wa0K7iN7hzFUPJH58nib14c0qv8oRcTnaIM.woff2) format('woff2');
unicode-range: U+0102-0103, U+0110-0111, U+0128-0129, U+0168-0169, U+01A0-01A1, U+01AF-01B0, U+1EA0-1EF9, U+20AB;
}
/* latin-ext */
@font-face {
font-family: 'Fraunces';
font-style: normal;
font-weight: 400;
font-display: optional;
src: url(https://fonts.gstatic.com/s/fraunces/v24/6NUu8FyLNQOQZAnv9bYEvDiIdE9Ea92uemAk_WBq8U_9v0c2Wa0K7iN7hzFUPJH58nib14c1qv8oRcTnaIM.woff2) format('woff2');
unicode-range: U+0100-024F, U+0259, U+1E00-1EFF, U+2020, U+20A0-20AB, U+20AD-20CF, U+2113, U+2C60-2C7F, U+A720-A7FF;
}
/* latin */
@font-face {
font-family: 'Fraunces';
font-style: normal;
font-weight: 400;
font-display: optional;
src: url(https://fonts.gstatic.com/s/fraunces/v24/6NUu8FyLNQOQZAnv9bYEvDiIdE9Ea92uemAk_WBq8U_9v0c2Wa0K7iN7hzFUPJH58nib14c7qv8oRcTn.woff2) format('woff2');
unicode-range: U+0000-00FF, U+0131, U+0152-0153, U+02BB-02BC, U+02C6, U+02DA, U+02DC, U+2000-206F, U+2074, U+20AC, U+2122, U+2191, U+2193, U+2212, U+2215, U+FEFF, U+FFFD;
}
/* vietnamese */
@font-face {
font-family: 'Fraunces';
font-style: normal;
font-weight: 900;
font-display: optional;
src: url(https://fonts.gstatic.com/s/fraunces/v24/6NUu8FyLNQOQZAnv9bYEvDiIdE9Ea92uemAk_WBq8U_9v0c2Wa0K7iN7hzFUPJH58nib14c0qv8oRcTnaIM.woff2) format('woff2');
unicode-range: U+0102-0103, U+0110-0111, U+0128-0129, U+0168-0169, U+01A0-01A1, U+01AF-01B0, U+1EA0-1EF9, U+20AB;
}
/* latin-ext */
@font-face {
font-family: 'Fraunces';
font-style: normal;
font-weight: 900;
font-display: optional;
src: url(https://fonts.gstatic.com/s/fraunces/v24/6NUu8FyLNQOQZAnv9bYEvDiIdE9Ea92uemAk_WBq8U_9v0c2Wa0K7iN7hzFUPJH58nib14c1qv8oRcTnaIM.woff2) format('woff2');
unicode-range: U+0100-024F, U+0259, U+1E00-1EFF, U+2020, U+20A0-20AB, U+20AD-20CF, U+2113, U+2C60-2C7F, U+A720-A7FF;
}
/* latin */
@font-face {
font-family: 'Fraunces';
font-style: normal;
font-weight: 900;
font-display: optional;
src: url(https://fonts.gstatic.com/s/fraunces/v24/6NUu8FyLNQOQZAnv9bYEvDiIdE9Ea92uemAk_WBq8U_9v0c2Wa0K7iN7hzFUPJH58nib14c7qv8oRcTn.woff2) format('woff2');
unicode-range: U+0000-00FF, U+0131, U+0152-0153, U+02BB-02BC, U+02C6, U+02DA, U+02DC, U+2000-206F, U+2074, U+20AC, U+2122, U+2191, U+2193, U+2212, U+2215, U+FEFF, U+FFFD;
}
`,
} }
...@@ -357,7 +357,7 @@ describe('@next/font/google', () => { ...@@ -357,7 +357,7 @@ describe('@next/font/google', () => {
expect($('link[rel="preconnect"]').length).toBe(0) expect($('link[rel="preconnect"]').length).toBe(0)
// Preload // Preload
expect($('link[as="font"]').length).toBe(7) expect($('link[as="font"]').length).toBe(8)
expect( expect(
Array.from($('link[as="font"]')) Array.from($('link[as="font"]'))
...@@ -369,6 +369,7 @@ describe('@next/font/google', () => { ...@@ -369,6 +369,7 @@ describe('@next/font/google', () => {
'/_next/static/media/560a6db6ac485cb1.p.woff2', '/_next/static/media/560a6db6ac485cb1.p.woff2',
'/_next/static/media/686d1702f12625fe.p.woff2', '/_next/static/media/686d1702f12625fe.p.woff2',
'/_next/static/media/86d92167ff02c708.p.woff2', '/_next/static/media/86d92167ff02c708.p.woff2',
'/_next/static/media/9ac01b894b856187.p.woff2',
'/_next/static/media/c9baea324111137d.p.woff2', '/_next/static/media/c9baea324111137d.p.woff2',
'/_next/static/media/fb68b4558e2a718e.p.woff2', '/_next/static/media/fb68b4558e2a718e.p.woff2',
]) ])
......
...@@ -334,4 +334,56 @@ describe('@next/font/google loader', () => { ...@@ -334,4 +334,56 @@ describe('@next/font/google loader', () => {
) )
}) })
}) })
it('should not send duplicate requests when several font variants use the same font file', async () => {
fetch
.mockResolvedValue({
ok: true,
arrayBuffer: () => '',
})
.mockResolvedValueOnce({
ok: true,
text: async () => `
/* latin */
@font-face {
font-family: 'Fraunces';
font-style: normal;
font-weight: 100;
font-display: swap;
src: url(https://fonts.gstatic.com/s/fraunces/v24/6NUu8FyLNQOQZAnv9bYEvDiIdE9Ea92uemAk_WBq8U_9v0c2Wa0K7iN7hzFUPJH58nib14c7qv8oRcTn.woff2) format('woff2');
unicode-range: U+0000-00FF, U+0131, U+0152-0153, U+02BB-02BC, U+02C6, U+02DA, U+02DC, U+2000-206F, U+2074, U+20AC, U+2122, U+2191, U+2193, U+2212, U+2215, U+FEFF, U+FFFD;
}
/* latin */
@font-face {
font-family: 'Fraunces';
font-style: normal;
font-weight: 300;
font-display: swap;
src: url(https://fonts.gstatic.com/s/fraunces/v24/6NUu8FyLNQOQZAnv9bYEvDiIdE9Ea92uemAk_WBq8U_9v0c2Wa0K7iN7hzFUPJH58nib14c7qv8oRcTn.woff2) format('woff2');
unicode-range: U+0000-00FF, U+0131, U+0152-0153, U+02BB-02BC, U+02C6, U+02DA, U+02DC, U+2000-206F, U+2074, U+20AC, U+2122, U+2191, U+2193, U+2212, U+2215, U+FEFF, U+FFFD;
}
/* latin */
@font-face {
font-family: 'Fraunces';
font-style: normal;
font-weight: 900;
font-display: swap;
src: url(https://fonts.gstatic.com/s/fraunces/v24/6NUu8FyLNQOQZAnv9bYEvDiIdE9Ea92uemAk_WBq8U_9v0c2Wa0K7iN7hzFUPJH58nib14c7qv8oRcTn.woff2) format('woff2');
unicode-range: U+0000-00FF, U+0131, U+0152-0153, U+02BB-02BC, U+02C6, U+02DA, U+02DC, U+2000-206F, U+2074, U+20AC, U+2122, U+2191, U+2193, U+2212, U+2215, U+FEFF, U+FFFD;
}
`,
})
const { css } = await loader({
functionName: 'Fraunces',
data: [{ weight: ['100', '300', '900'] }],
config: { subsets: [] },
emitFontFile: jest.fn(),
resolve: jest.fn(),
fs: {} as any,
isServer: true,
variableName: 'myFont',
})
expect(fetch).toHaveBeenCalledTimes(2)
expect(css).not.toInclude('https://fonts.gstatic.com/s/fraunces/v24/')
})
}) })
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment