Skip to content

Commit 76b8039

Browse files
authored
Turbopack: Fix sizes=any with icon.svg metadata file (#78663)
## What? The check for the svg extension that exists in the webpack loader was missing: https://github.com/vercel/next.js/blob/757542a400945c7709f1338e5619b171304b8d48/packages/next/src/build/webpack/loaders/next-metadata-image-loader.ts#L150 Implemented that. Fixes PACK-4475 Fixes #78520 <!-- Thanks for opening a PR! Your contribution is much appreciated. To make sure your PR is handled as smoothly as possible we request that you follow the checklist sections below. Choose the right checklist for the change(s) that you're making: ## For Contributors ### Improving Documentation - Run `pnpm prettier-fix` to fix formatting issues before opening the PR. - Read the Docs Contribution Guide to ensure your contribution follows the docs guidelines: https://nextjs.org/docs/community/contribution-guide ### Adding or Updating Examples - The "examples guidelines" are followed from our contributing doc https://github.com/vercel/next.js/blob/canary/contributing/examples/adding-examples.md - Make sure the linting passes by running `pnpm build && pnpm lint`. See https://github.com/vercel/next.js/blob/canary/contributing/repository/linting.md ### Fixing a bug - Related issues linked using `fixes #number` - Tests added. See: https://github.com/vercel/next.js/blob/canary/contributing/core/testing.md#writing-tests-for-nextjs - Errors have a helpful link attached, see https://github.com/vercel/next.js/blob/canary/contributing.md ### Adding a feature - Implements an existing feature request or RFC. Make sure the feature request has been accepted for implementation before opening a PR. (A discussion must be opened, see https://github.com/vercel/next.js/discussions/new?category=ideas) - Related issues/discussions are linked using `fixes #number` - e2e tests added (https://github.com/vercel/next.js/blob/canary/contributing/core/testing.md#writing-tests-for-nextjs) - Documentation added - Telemetry added. In case of a feature if it's used or not. - Errors have a helpful link attached, see https://github.com/vercel/next.js/blob/canary/contributing.md ## For Maintainers - Minimal description (aim for explaining to someone not on the team to understand the PR) - When linking to a Slack thread, you might want to share details of the conclusion - Link both the Linear (Fixes NEXT-xxx) and the GitHub issues - Add review comments if necessary to explain to the reviewer the logic behind a change ### What? ### Why? ### How? Closes NEXT- Fixes # -->
1 parent b826934 commit 76b8039

File tree

8 files changed

+56
-6
lines changed

8 files changed

+56
-6
lines changed

crates/next-core/src/app_page_loader_tree.rs

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -268,10 +268,16 @@ impl AppPageLoaderTreeBuilder {
268268
writeln!(self.loader_tree_code, "{s} width: {identifier}.width,")?;
269269
writeln!(self.loader_tree_code, "{s} height: {identifier}.height,")?;
270270
} else {
271-
writeln!(
272-
self.loader_tree_code,
273-
"{s} sizes: `${{{identifier}.width}}x${{{identifier}.height}}`,"
274-
)?;
271+
let ext = &*path.extension().await?;
272+
// For SVGs, skip sizes and use "any" to let it scale automatically based on viewport,
273+
// For the images doesn't provide the size properly, use "any" as well.
274+
// If the size is presented, use the actual size for the image.
275+
let sizes = if ext == "svg" {
276+
"any".to_string()
277+
} else {
278+
format!("${{{identifier}.width}}x${{{identifier}.height}}")
279+
};
280+
writeln!(self.loader_tree_code, "{s} sizes: `{sizes}`,")?;
275281
}
276282

277283
let content_type = get_content_type(path).await?;

crates/next-core/src/next_app/metadata/image.rs

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,22 @@ pub async fn dynamic_image_metadata_source(
5353

5454
let use_numeric_sizes = ty == "twitter" || ty == "openGraph";
5555
let sizes = if use_numeric_sizes {
56-
"data.width = size.width; data.height = size.height;"
56+
"data.width = size.width; data.height = size.height;".to_string()
5757
} else {
58-
"data.sizes = size.width + \"x\" + size.height;"
58+
// Note: This case seemingly can never happen because this code runs for dynamic metadata
59+
// which has e.g. a `.js` or `.ts` extension not `.svg`. Branching code is still here to
60+
// match the static implementation
61+
//
62+
// For SVGs, skip sizes and use "any" to let it scale automatically based on viewport,
63+
// For the images doesn't provide the size properly, use "any" as well.
64+
// If the size is presented, use the actual size for the image.
65+
let sizes = if ext == "svg" {
66+
"any"
67+
} else {
68+
"${size.width}x${size.height}"
69+
};
70+
71+
format!("data.sizes = `{sizes}`;")
5972
};
6073

6174
let source = Vc::upcast(FileSource::new(path));
25.3 KB
Binary file not shown.
Lines changed: 1 addition & 0 deletions
Loading
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import { ReactNode } from 'react'
2+
export default function Root({ children }: { children: ReactNode }) {
3+
return (
4+
<html>
5+
<body>{children}</body>
6+
</html>
7+
)
8+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export default function Page() {
2+
return <p>hello world</p>
3+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import { nextTestSetup } from 'e2e-utils'
2+
3+
describe('metadata-svg-icon', () => {
4+
const { next } = nextTestSetup({
5+
files: __dirname,
6+
})
7+
8+
it('should have sizes=any for .svg icon', async () => {
9+
const $ = await next.render$('/')
10+
const icon = $('link[rel="icon"][type="image/svg+xml"]')
11+
expect(icon.attr('sizes')).toBe('any')
12+
})
13+
})
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
/**
2+
* @type {import('next').NextConfig}
3+
*/
4+
const nextConfig = {}
5+
6+
module.exports = nextConfig

0 commit comments

Comments
 (0)