rsnext/packages
Indraan S. Toor dda62b693e
Fix weight values above 900 not working with Google Fonts (#54339)
## What?
This PR fixes issue #54304. As discussed in the issue it was suspected that the values of weights were not being sorted correctly. The weights were being sorted in lexicographical order. 

In order for Google fonts to work, Google Fonts need the values of weights present in the url in sorted order.

Due to this the url which was being generated for Google Fonts which had a value of "1000" in weight was giving an error and the font was not working.

## Why I Think This Solution Works?

Let's assume that we have an object named axes with the property named named "wght" which will have the value of weights as following:

```
const axes = {
  wght: ["100", "200", "300", "400", "500", "600", "700", "800", "900", '1000']
}
```

Let's create another variable named "display" with the value set as "swap" and create another variable named "fontFamily" with the value as "DM Sans" (this is the font which was being used in the issue).

```
const fontFamily = "DM Sans";
const display = "swap";
```

If we call the function using them
```
const FONT_URL = getGoogleFontsUrl(fontFamily, axes, display);
console.log("The font url is: ", FONT_URL)

Output Will Be:
The font url is: https://fonts.googleapis.com/css2?family=DM+Sans:wght@100;1000;200;300;400;500;600;700;800;900&display=swap
```
It will give the incorrect value which was shown in the issue. 

If you see at line number 56 there was no comparison function passed in the sort function which is what was suspected earlier and it was sorting the values lexicographically.

Create another variable inside the function above line 54 where we are updating the value of "url" variable.

```
const variantValues = variants.map((variant) => variant.map(([, val]) => val).join(','))
console.log("Variant Values: ", variantValues)

Output Will Be:
Variant Values: ["100", "200", "300", "400", "500", "600", "700", "800", "900", "1000"]
```

Now call the function again with the same values you will see the values in the correct order but as soon as you call "sort" function it will give you incorrect sorted values which is what is happening in the "url" variable.

```
const variantValues = variants.map((variant) => variant.map(([, val]) => val).join(',')).sort()
console.log("Variant Values: ", variantValues)

Output Will Be:
["100", "1000", "200", "300", "400", "500", "600", "700", "800", "900"] 

(As you can see the values are sorted incorrectly)
```
There was one case when I implemented this solution. When "ital" is also passed then the values are in "ital,wght" format and because of it the sorting was giving incorrect result for this case.

Let's update the "axes" variable which we made previously to contain these values:

```
const axes = {
  wght: ["500","300", "400"],
  ital: ["0","1"]
}
```
Now when we run the function it gives us the result as:
`
https://fonts.googleapis.com/css2?family=DM+Sans:ital,wght@0,500;0,300;0,400;1,500;1,300;1,400&display=swap`

Where as the expected result was: 

`https://fonts.googleapis.com/css2?family=DM+Sans:ital,wght@0,300;0,400;0,500;1,300;1,400;1,500&display=swap`

So, I rewrote the "sort" function to also handle this case.

## What Did I Do To Fix It?

I passed the comparison function inside the "sort" function which was being to ensure that the elements are compared as integers, and thus the sorting will be done numerically.

Which gave me the correct output as url:

`https://fonts.googleapis.com/css2?family=DM+Sans:wght@100;200;300;400;500;600;700;800;900;1000&display=swap`

With values being sorted correctly and if you visit the link you will see the response instead of the error.

Any thought's or suggestions would be greatly appreciated.
2023-08-28 12:02:40 +00:00
..
create-next-app v13.4.20-canary.9 2023-08-25 21:26:50 +00:00
eslint-config-next v13.4.20-canary.9 2023-08-25 21:26:50 +00:00
eslint-plugin-next v13.4.20-canary.9 2023-08-25 21:26:50 +00:00
font Fix weight values above 900 not working with Google Fonts (#54339) 2023-08-28 12:02:40 +00:00
next Implement hot reloader interface for Turbopack (#54632) 2023-08-28 13:34:38 +02:00
next-bundle-analyzer v13.4.20-canary.9 2023-08-25 21:26:50 +00:00
next-codemod v13.4.20-canary.9 2023-08-25 21:26:50 +00:00
next-env v13.4.20-canary.9 2023-08-25 21:26:50 +00:00
next-mdx v13.4.20-canary.9 2023-08-25 21:26:50 +00:00
next-plugin-storybook v13.4.20-canary.9 2023-08-25 21:26:50 +00:00
next-polyfill-module v13.4.20-canary.9 2023-08-25 21:26:50 +00:00
next-polyfill-nomodule v13.4.20-canary.9 2023-08-25 21:26:50 +00:00
next-swc Turbopack: add middleware support for next.rs api dev mode (#54555) 2023-08-27 12:10:15 +02:00
react-dev-overlay v13.4.20-canary.9 2023-08-25 21:26:50 +00:00
react-refresh-utils v13.4.20-canary.9 2023-08-25 21:26:50 +00:00
third-parties v13.4.20-canary.9 2023-08-25 21:26:50 +00:00