From 5fc2e3d35dbfbca9d1ad3fe64760b6d0e3400237 Mon Sep 17 00:00:00 2001 From: Artem Alexeyenko Date: Thu, 7 Jul 2022 04:28:51 -0400 Subject: [PATCH] [Nextjs] Update nextjs to 12.2 (#1093) * Nextjs version update plus extra upgrade steps for middleware * Moved middleware.ts to recommended location (adjacent to pages) * Updated next-env.d.ts * Comment updates (use generic "Sitecore editor", next/image now supported, next doc links changed) * Removed .babelrc (as of 12.2, swc appears to work in Windows-based rendering hosts in containers - or at least acts no differently than with it) * Add the _next... URL exclusion rule for personalize middleware Co-authored-by: Adam Brauer <400763+ambrauer@users.noreply.github.com> --- .../src/templates/nextjs/.babelrc | 9 - .../src/templates/nextjs/.env | 4 +- .../src/templates/nextjs/.eslintrc | 2 +- .../src/templates/nextjs/next-env.d.ts | 1 - .../src/templates/nextjs/package.json | 2 +- .../scripts/templates/component-factory.ts | 2 +- .../nextjs/sitecore/config/{{appName}}.config | 2 +- .../src/templates/nextjs/src/Layout.tsx | 4 +- .../src/templates/nextjs/src/Navigation.tsx | 4 +- .../src/templates/nextjs/src/assets/app.css | 2 +- .../{pages/_middleware.ts => middleware.ts} | 0 .../nextjs/src/pages/[[...path]].tsx | 4 +- .../src/pages/api/editing/data/[key].ts | 6 +- .../nextjs/src/pages/api/editing/render.ts | 8 +- packages/sitecore-jss-nextjs/package.json | 4 +- .../src/edge/personalize-middleware.ts | 14 +- yarn.lock | 157 +++++++++++------- 17 files changed, 127 insertions(+), 98 deletions(-) delete mode 100644 packages/create-sitecore-jss/src/templates/nextjs/.babelrc rename packages/create-sitecore-jss/src/templates/nextjs/src/{pages/_middleware.ts => middleware.ts} (100%) diff --git a/packages/create-sitecore-jss/src/templates/nextjs/.babelrc b/packages/create-sitecore-jss/src/templates/nextjs/.babelrc deleted file mode 100644 index edb97c6178..0000000000 --- a/packages/create-sitecore-jss/src/templates/nextjs/.babelrc +++ /dev/null @@ -1,9 +0,0 @@ -// This file exists in order to disable Next.js SWC compilation due to -// current instability. Specifically, when compiling on Windows-based -// rendering hosts using containers you may see the following error: -// https://nextjs.org/docs/messages/failed-loading-swc -// -// You may wish to remove this file when deploying to Vercel. -{ - "presets": ["next/babel"] -} diff --git a/packages/create-sitecore-jss/src/templates/nextjs/.env b/packages/create-sitecore-jss/src/templates/nextjs/.env index 6b0f61d620..34bc459af6 100644 --- a/packages/create-sitecore-jss/src/templates/nextjs/.env +++ b/packages/create-sitecore-jss/src/templates/nextjs/.env @@ -4,14 +4,14 @@ # https://nextjs.org/docs/basic-features/environment-variables # The public URL to use for absolute URLs, which are required when -# the Next.js app is run within the Sitecore Experience Editor. +# the Next.js app is run within Sitecore editors. # This should match the `serverSideRenderingEngineApplicationUrl` # in your Sitecore configuration (see \sitecore\config\<%- appName %>.config). # Be sure to update these values accordingly as your public endpoint changes. # See https://jss.sitecore.com/docs/fundamentals/services/view-engine PUBLIC_URL=http://localhost:3000 -# To secure the Experience Editor endpoint exposed by your Next.js app +# To secure the Sitecore editor endpoint exposed by your Next.js app # (`/api/editing/render` by default), a secret token is used. This (client-side) # value must match your server-side value (see \sitecore\config\<%- appName %>.config). # We recommend an alphanumeric value of at least 16 characters. diff --git a/packages/create-sitecore-jss/src/templates/nextjs/.eslintrc b/packages/create-sitecore-jss/src/templates/nextjs/.eslintrc index e145248adc..915ce8ba28 100644 --- a/packages/create-sitecore-jss/src/templates/nextjs/.eslintrc +++ b/packages/create-sitecore-jss/src/templates/nextjs/.eslintrc @@ -15,7 +15,7 @@ ], "ignorePatterns": [".generated/**/*", "**/*.d.ts", "**/*.js"], "rules": { - "@next/next/no-img-element": "off", // Don't force next/image (not currently compatible with Sitecore editing) + "@next/next/no-img-element": "off", // Don't force next/image "jsx-a11y/alt-text": ["warn", { "elements": ["img"] }], // Don't force alt for (sourced from Sitecore media) "no-unused-vars": "off", "@typescript-eslint/no-unused-vars": "error", diff --git a/packages/create-sitecore-jss/src/templates/nextjs/next-env.d.ts b/packages/create-sitecore-jss/src/templates/nextjs/next-env.d.ts index 9bc3dd46b9..4f11a03dc6 100644 --- a/packages/create-sitecore-jss/src/templates/nextjs/next-env.d.ts +++ b/packages/create-sitecore-jss/src/templates/nextjs/next-env.d.ts @@ -1,5 +1,4 @@ /// -/// /// // NOTE: This file should not be edited diff --git a/packages/create-sitecore-jss/src/templates/nextjs/package.json b/packages/create-sitecore-jss/src/templates/nextjs/package.json index 93f5d92b72..d972002360 100644 --- a/packages/create-sitecore-jss/src/templates/nextjs/package.json +++ b/packages/create-sitecore-jss/src/templates/nextjs/package.json @@ -32,7 +32,7 @@ "@sitecore-jss/sitecore-jss-nextjs": "^21.0.0-canary", "graphql": "~15.8.0", "graphql-tag": "^2.11.0", - "next": "^12.1.6", + "next": "^12.2.0", "next-localization": "^0.12.0", "react": "^18.1.0", "react-dom": "^18.1.0" diff --git a/packages/create-sitecore-jss/src/templates/nextjs/scripts/templates/component-factory.ts b/packages/create-sitecore-jss/src/templates/nextjs/scripts/templates/component-factory.ts index d1140a6c2b..75dca71ce5 100644 --- a/packages/create-sitecore-jss/src/templates/nextjs/scripts/templates/component-factory.ts +++ b/packages/create-sitecore-jss/src/templates/nextjs/scripts/templates/component-factory.ts @@ -90,7 +90,7 @@ ${componentFiles // See https://nextjs.org/docs/advanced-features/dynamic-import // At the end you will have single preloaded script for each lazy loading module. // Editing mode doesn't work well with dynamic components in nextjs: dynamic components are not displayed without refresh after a rendering is added. -// This happens beacuse Experience Editor simply inserts updated HTML generated on server side. This conflicts with nextjs dynamic logic as no HTML gets rendered for dynamic component +// This happens beacuse Sitecore editors simply insert updated HTML generated on server side. This conflicts with nextjs dynamic logic as no HTML gets rendered for dynamic component // So we use require() to obtain dynamic components in editing mode while preserving dynamic logic for non-editing scenarios // As we need to be able to seamlessly work with dynamic components in both editing and normal modes, different componentFactory functions will be passed to app 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 e943130b83..096a13cb38 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 @@ -33,7 +33,7 @@