| 415 | } |
| 416 | |
| 417 | function 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 | |