Skip to content

Commit

Permalink
Fix Image compatibility issue when using sizes (#24569)
Browse files Browse the repository at this point in the history
When using `sizes`, [`matchAll`](https://caniuse.com/mdn-javascript_builtins_string_matchall) isn't supported by older browsers like IE and Safari 12. This PR changes it to `exec`.

There're already tests of `sizes` with multiple `vw` values covered.

Fixes #23677.

## Bug

- [x] Related issues linked using `fixes #number`
- [ ] Integration tests added

## Feature

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

## Documentation / Examples

- [ ] Make sure the linting passes
  • Loading branch information
shuding authored Apr 29, 2021
1 parent a35dedb commit 89d2c4e
Showing 1 changed file with 5 additions and 3 deletions.
8 changes: 5 additions & 3 deletions packages/next/client/image.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -94,9 +94,11 @@ function getWidths(
): { widths: number[]; kind: 'w' | 'x' } {
if (sizes && (layout === 'fill' || layout === 'responsive')) {
// Find all the "vw" percent sizes used in the sizes prop
const percentSizes = [...sizes.matchAll(/(^|\s)(1?\d?\d)vw/g)].map((m) =>
parseInt(m[2])
)
const viewportWidthRe = /(^|\s)(1?\d?\d)vw/g
const percentSizes = []
for (let match; (match = viewportWidthRe.exec(sizes)); match) {
percentSizes.push(parseInt(match[2]))
}
if (percentSizes.length) {
const smallestRatio = Math.min(...percentSizes) * 0.01
return {
Expand Down

0 comments on commit 89d2c4e

Please sign in to comment.