From 2c759c103197a24e4f449e70fbe0eba8d418b39c Mon Sep 17 00:00:00 2001 From: Addy Pathania <89087450+sc-addypathania@users.noreply.github.com> Date: Thu, 14 Apr 2022 16:32:03 -0400 Subject: [PATCH] [Nextjs] New NextImage component (#978) * Implement Nextjs Image nextjs package and styleguide * WIP: Implement nextjs image in nextjs package and styleguide * Refactored image loader * Refactor way in which EE markup is applied in react image component to be reused in next image component. * WIP: Start refactoring test for nextjs image also refactor nextjs-styleguide template to use new image component * added tests and completed todos * removed unwanted code and added line to end of file * addressed review comments * fixed unit tests * removed dublicate code * removed extra comments * updated yarn.lock * updated yarn.lock, removed dubplicate interface * removed changes in yarn.lock * updated yarn.lock * updated yarn.lock * fixed linting error * updated deviceSizes comment in next.config * improved comments, added utility to get hostname * removed path and loader from config * added relative path to loader function * added comment for path variable * updated error message, removed extra interface * fixed undefined path variable issue * added test cases for the loader function * renamed next-setup file * updated next.config comments, refactored redundant code * added the fix from vercel regarding access to config in loader function * removed blurDataUrl, made loader function more customizable, fixed tests * Delete yarn.lock * added yarn.lock * updated yarn.lock * changed next version in template * removed redundant comments and files , changed error message * added test case for user custom loader function * added another test case fro custom loader * added test for absolute url, mock loader function * added test for user sent custom loader * removed params object, added afterEach * updated the afterEach call * refactored mock loader tests * created two describes for next image tests * updated tests description * updated the order of tests and describes in next image tests * Simplified loader which does not depend on 'config' (not to be used according to Vercel: https://github.com/vercel/next.js/issues/35115). Added whitelisting config for default next/image sizes. * updated image url, fixed classname bug for EE in next/image * added unit test * remove console.log * resolved merge conflicts in yarn.lock file * refactored util fucntions, removed redundant code * updated yarn.lock, refactored utils * removed url-parse Co-authored-by: CobyPear Co-authored-by: Adam Brauer <400763+ambrauer@users.noreply.github.com> --- .../fields/Styleguide-FieldUsage-Image.tsx | 32 ++- .../src/lib/component-props/styleguide.ts | 2 +- .../nextjs/sitecore/config/{{appName}}.config | 24 +- .../nextjs/src/pages/[[...path]].tsx | 4 +- packages/sitecore-jss-nextjs/package.json | 1 + .../src/components/NextImage.test.tsx | 269 ++++++++++++++++++ .../src/components/NextImage.tsx | 115 ++++++++ packages/sitecore-jss-nextjs/src/index.ts | 1 + .../src/testData/ee-data.ts | 3 + .../src/components/Image.tsx | 71 ++--- packages/sitecore-jss-react/src/index.ts | 2 +- packages/sitecore-jss-react/src/utils.ts | 19 ++ .../sitecore-jss/src/media/media-api.test.ts | 24 +- packages/sitecore-jss/src/media/media-api.ts | 32 ++- yarn.lock | 1 + 15 files changed, 535 insertions(+), 65 deletions(-) create mode 100644 packages/sitecore-jss-nextjs/src/components/NextImage.test.tsx create mode 100644 packages/sitecore-jss-nextjs/src/components/NextImage.tsx diff --git a/packages/create-sitecore-jss/src/templates/nextjs-styleguide/src/components/fields/Styleguide-FieldUsage-Image.tsx b/packages/create-sitecore-jss/src/templates/nextjs-styleguide/src/components/fields/Styleguide-FieldUsage-Image.tsx index d80bb9c618..3c199decf2 100644 --- a/packages/create-sitecore-jss/src/templates/nextjs-styleguide/src/components/fields/Styleguide-FieldUsage-Image.tsx +++ b/packages/create-sitecore-jss/src/templates/nextjs-styleguide/src/components/fields/Styleguide-FieldUsage-Image.tsx @@ -1,4 +1,4 @@ -import { Image, ImageField, withDatasourceCheck } from '@sitecore-jss/sitecore-jss-nextjs'; +import { NextImage, ImageField, withDatasourceCheck } from '@sitecore-jss/sitecore-jss-nextjs'; import StyleguideSpecimen from 'components/styleguide/Styleguide-Specimen'; import { ComponentProps } from 'lib/component-props'; import { StyleguideSpecimenFields } from 'lib/component-props/styleguide'; @@ -17,21 +17,29 @@ type StyleguideFieldUsageImageProps = ComponentProps & */ const StyleguideFieldUsageImage = (props: StyleguideFieldUsageImageProps): JSX.Element => ( + {/* + You can use any of the props available in the next/image. + E.g. we have used 'priority' to demonstrate how an image could be considered high priority and preload. + Lazy loading is automatically disabled for images using priority. + See here for all the features provided by next/image: https://nextjs.org/docs/api-reference/next/image + */}

Plain image

- + {/* Advanced image usage example editable: controls whether image can be edited in Sitecore Experience Editor - imageParams: parameters that are passed to Sitecore to perform server-side resizing of the image. + unoptimized: disables next/image source optimization in favor of imageParams + imageParams: parameters that are passed to Sitecore to perform server-side resizing of the image Sample rescales image to max 100x50 dimensions on the server, respecting aspect ratio IMPORTANT: imageParams must be whitelisted for resizing to occur. See /sitecore/config/*.config (search for 'allowedMediaParams') any other attributes: pass through to img tag */}

Advanced image (not editable)

- {/* - Srcset adaptive image usage example - Adaptive srcsets are supported using Sitecore server-side resizing. - The `srcSet` can use Sitecore image resizing parameters (i.e. w, h, mw, mh). - Sample create a srcset using two sizes (server resizing), 300 and 100px max widths, respecting aspect ratio. - IMPORTANT: srcSet params must be whitelisted for adaptive resizing to occur. See /sitecore/config/*.config (search for 'allowedMediaParams') + next/image generates responsive srcSet automatically based on layout. See https://nextjs.org/docs/api-reference/next/image#layout. + IMPORTANT: The generated sizes should match your Sitecore server-side allowlist. See /sitecore/config/*.config (search for 'allowedMediaParams') */}

Srcset responsive image

-
); diff --git a/packages/create-sitecore-jss/src/templates/nextjs-styleguide/src/lib/component-props/styleguide.ts b/packages/create-sitecore-jss/src/templates/nextjs-styleguide/src/lib/component-props/styleguide.ts index a0a059bec8..446b54e9bc 100644 --- a/packages/create-sitecore-jss/src/templates/nextjs-styleguide/src/lib/component-props/styleguide.ts +++ b/packages/create-sitecore-jss/src/templates/nextjs-styleguide/src/lib/component-props/styleguide.ts @@ -3,7 +3,7 @@ import { Field } from '@sitecore-jss/sitecore-jss-nextjs'; /** * Shared styleguide specimen fields */ - export type StyleguideSpecimenFields = { +export type StyleguideSpecimenFields = { fields: { description: Field; heading: Field; diff --git a/packages/create-sitecore-jss/src/templates/nextjs/sitecore/config/{{appName}}.config b/packages/create-sitecore-jss/src/templates/nextjs/sitecore/config/{{appName}}.config index b0049e0241..e943130b83 100644 --- a/packages/create-sitecore-jss/src/templates/nextjs/sitecore/config/{{appName}}.config +++ b/packages/create-sitecore-jss/src/templates/nextjs/sitecore/config/{{appName}}.config @@ -100,7 +100,7 @@