Skip to content

Commit af3c506

Browse files
authored
Fix replaceAll usage in font loader (vercel#42550)
This removes usage of `replaceAll` as it wasn't introduced [until Node.js v15](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replaceAll#browser_compatibility) although we support v14 still. x-ref: https://dev.azure.com/nextjs/next.js/_build/results?buildId=43457&view=logs&jobId=8af7cf9c-43a1-584d-6f5c-57bad8880974 ## 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)
1 parent 98106ba commit af3c506

File tree

1 file changed

+14
-2
lines changed

1 file changed

+14
-2
lines changed

packages/font/src/google/loader.ts

+14-2
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,18 @@ import {
1717
const cssCache = new Map<string, Promise<string>>()
1818
const fontCache = new Map<string, any>()
1919

20+
// regexp is based on https://github.com/sindresorhus/escape-string-regexp
21+
const reHasRegExp = /[|\\{}()[\]^$+*?.-]/
22+
const reReplaceRegExp = /[|\\{}()[\]^$+*?.-]/g
23+
24+
function escapeStringRegexp(str: string) {
25+
// see also: https://github.com/lodash/lodash/blob/2da024c3b4f9947a48517639de7560457cd4ec6c/escapeRegExp.js#L23
26+
if (reHasRegExp.test(str)) {
27+
return str.replace(reReplaceRegExp, '\\$&')
28+
}
29+
return str
30+
}
31+
2032
const downloadGoogleFonts: FontLoader = async ({
2133
functionName,
2234
data,
@@ -124,8 +136,8 @@ const downloadGoogleFonts: FontLoader = async ({
124136
// Replace @font-face sources with self-hosted files
125137
let updatedCssResponse = fontFaceDeclarations
126138
for (const { googleFontFileUrl, selfHostedFileUrl } of downloadedFiles) {
127-
updatedCssResponse = updatedCssResponse.replaceAll(
128-
googleFontFileUrl,
139+
updatedCssResponse = updatedCssResponse.replace(
140+
new RegExp(escapeStringRegexp(googleFontFileUrl), 'g'),
129141
selfHostedFileUrl
130142
)
131143
}

0 commit comments

Comments
 (0)