MCPcopy
hub / github.com/vercel/next.js / generateImgAttrs

Function generateImgAttrs

packages/next/src/client/legacy/image.tsx:417–473  ·  view source on GitHub ↗
({
  config,
  src,
  unoptimized,
  layout,
  width,
  quality,
  sizes,
  loader,
}: GenImgAttrsData)

Source from the content-addressed store, hash-verified

415}
416
417function generateImgAttrs({
418 config,
419 src,
420 unoptimized,
421 layout,
422 width,
423 quality,
424 sizes,
425 loader,
426}: GenImgAttrsData): GenImgAttrsResult {
427 if (unoptimized) {
428 if (src.startsWith('/') && !src.startsWith('//')) {
429 let deploymentId = getDeploymentId()
430 if (deploymentId) {
431 // We unfortunately can't easily use `new URL()` here, because it normalizes the URL which causes
432 // double-encoding with the `encodeURIComponent(src)` below
433 const qIndex = src.indexOf('?')
434 if (qIndex !== -1) {
435 const params = new URLSearchParams(src.slice(qIndex + 1))
436 const srcDpl = params.get('dpl')
437 if (!srcDpl) {
438 // src is missing the dpl parameter, but we have a deploymentId, so add it to the src URL
439 params.append('dpl', deploymentId)
440 src = src.slice(0, qIndex) + '?' + params.toString()
441 }
442 } else {
443 // src is missing the dpl parameter, but we have a deploymentId, so add it to the src URL
444 src = src + `?dpl=${deploymentId}`
445 }
446 }
447 }
448 return { src, srcSet: undefined, sizes: undefined }
449 }
450
451 const { widths, kind } = getWidths(config, width, layout, sizes)
452 const last = widths.length - 1
453
454 return {
455 sizes: !sizes && kind === 'w' ? '100vw' : sizes,
456 srcSet: widths
457 .map(
458 (w, i) =>
459 `${loader({ config, src, quality, width: w })} ${
460 kind === 'w' ? w : i + 1
461 }${kind}`
462 )
463 .join(', '),
464
465 // It's intended to keep `src` the last attribute because React updates
466 // attributes in order. If we keep `src` the first one, Safari will
467 // immediately start to fetch `src`, before `sizes` and `srcSet` are even
468 // updated by React. That causes multiple unnecessary requests if `srcSet`
469 // and `sizes` are defined.
470 // This bug cannot be reproduced in Chrome or Firefox.
471 src: loader({ config, src, quality, width: widths[last] }),
472 }
473}
474

Callers 2

ImageElementFunction · 0.70
ImageFunction · 0.70

Calls 11

getDeploymentIdFunction · 0.90
startsWithMethod · 0.80
getWidthsFunction · 0.70
getMethod · 0.65
loaderFunction · 0.50
indexOfMethod · 0.45
sliceMethod · 0.45
appendMethod · 0.45
toStringMethod · 0.45
joinMethod · 0.45
mapMethod · 0.45

Tested by

no test coverage detected