({
config,
src,
unoptimized,
width,
quality,
sizes,
loader,
}: GenImgAttrsData)
| 222 | } |
| 223 | |
| 224 | function generateImgAttrs({ |
| 225 | config, |
| 226 | src, |
| 227 | unoptimized, |
| 228 | width, |
| 229 | quality, |
| 230 | sizes, |
| 231 | loader, |
| 232 | }: GenImgAttrsData): GenImgAttrsResult { |
| 233 | if (unoptimized) { |
| 234 | if (src.startsWith('/') && !src.startsWith('//')) { |
| 235 | let deploymentId = getDeploymentId() |
| 236 | if (deploymentId) { |
| 237 | // We unfortunately can't easily use `new URL()` here, because it normalizes the URL which causes |
| 238 | // double-encoding with the `encodeURIComponent(src)` below |
| 239 | const qIndex = src.indexOf('?') |
| 240 | if (qIndex !== -1) { |
| 241 | const params = new URLSearchParams(src.slice(qIndex + 1)) |
| 242 | const srcDpl = params.get('dpl') |
| 243 | if (!srcDpl) { |
| 244 | // src is missing the dpl parameter, but we have a deploymentId, so add it to the src URL |
| 245 | params.append('dpl', deploymentId) |
| 246 | src = src.slice(0, qIndex) + '?' + params.toString() |
| 247 | } |
| 248 | } else { |
| 249 | // src is missing the dpl parameter, but we have a deploymentId, so add it to the src URL |
| 250 | src = src + `?dpl=${deploymentId}` |
| 251 | } |
| 252 | } |
| 253 | } |
| 254 | return { src, srcSet: undefined, sizes: undefined } |
| 255 | } |
| 256 | |
| 257 | const { widths, kind } = getWidths(config, width, sizes) |
| 258 | const last = widths.length - 1 |
| 259 | |
| 260 | return { |
| 261 | sizes: !sizes && kind === 'w' ? '100vw' : sizes, |
| 262 | srcSet: widths |
| 263 | .map( |
| 264 | (w, i) => |
| 265 | `${loader({ config, src, quality, width: w })} ${ |
| 266 | kind === 'w' ? w : i + 1 |
| 267 | }${kind}` |
| 268 | ) |
| 269 | .join(', '), |
| 270 | |
| 271 | // It's intended to keep `src` the last attribute because React updates |
| 272 | // attributes in order. If we keep `src` the first one, Safari will |
| 273 | // immediately start to fetch `src`, before `sizes` and `srcSet` are even |
| 274 | // updated by React. That causes multiple unnecessary requests if `srcSet` |
| 275 | // and `sizes` are defined. |
| 276 | // This bug cannot be reproduced in Chrome or Firefox. |
| 277 | src: loader({ config, src, quality, width: widths[last] }), |
| 278 | } |
| 279 | } |
| 280 | |
| 281 | /** |
no test coverage detected