Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix unexpected object mutation while resolving Open Graph
Modifying an object passed from external sources can unintentionally lead to unpredictable behavior. By changing the implementation from in-place object modification to returning a new object, we can make the method more predictable. fixes #49501
- Loading branch information
Showing
2 changed files
with
33 additions
and
9 deletions.
There are no files selected for viewing
23 changes: 23 additions & 0 deletions
23
packages/next/src/lib/metadata/resolvers/resolve-opengraph.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import { resolveImages } from './resolve-opengraph' | ||
|
||
describe('resolveImages', () => { | ||
const image1 = 'https://www.example.com/image1.jpg' | ||
const image2 = 'https://www.example.com/image2.jpg' | ||
|
||
it(`should resolve images`, () => { | ||
const images = [image1, { url: image2, alt: 'Image2' }] | ||
|
||
expect(resolveImages(images, null)).toEqual([ | ||
{ url: new URL(image1) }, | ||
{ url: new URL(image2), alt: 'Image2' }, | ||
]) | ||
}) | ||
|
||
it('should not mutating passed images', () => { | ||
const images = [image1, { url: image2, alt: 'Image2' }] | ||
|
||
resolveImages(images, null) | ||
|
||
expect(images).toEqual([image1, { url: image2, alt: 'Image2' }]) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters