From 288abc4da1a3880c05bfb9dea056d5b8dc0340fd Mon Sep 17 00:00:00 2001 From: Adam Brauer <400763+ambrauer@users.noreply.github.com> Date: Mon, 30 May 2022 02:13:51 -0500 Subject: [PATCH] [NextJs] Fix "debug module not found" error in redirects middleware (#1045) * Remove import of next.config.js in redirects middleware (fixes #535263) and use static values for now. next.config.js is not intended to be imported in this way. We should have access to locales directly in middleware in the future: https://github.com/vercel/next.js/issues/37253 * Add type assist jsdoc for NextConfig (cherry picked from commit 2e280767f38a98507c0972462dca95366219fe94) --- .../src/lib/next-config/plugins/disconnected.js | 3 +++ .../src/lib/next-config/plugins/styleguide.js | 3 +++ .../nextjs-sxa/src/lib/middleware/plugins/redirects.ts | 5 +++-- .../nextjs-sxa/src/lib/next-config/plugins/robots.js | 3 +++ .../nextjs-sxa/src/lib/next-config/plugins/sitemap.js | 3 +++ .../create-sitecore-jss/src/templates/nextjs/next.config.js | 3 +++ .../src/templates/nextjs/src/lib/next-config/plugins/edge.js | 5 ++++- .../templates/nextjs/src/lib/next-config/plugins/graphql.js | 3 +++ .../templates/nextjs/src/lib/next-config/plugins/monorepo.js | 3 +++ 9 files changed, 28 insertions(+), 3 deletions(-) diff --git a/packages/create-sitecore-jss/src/templates/nextjs-styleguide/src/lib/next-config/plugins/disconnected.js b/packages/create-sitecore-jss/src/templates/nextjs-styleguide/src/lib/next-config/plugins/disconnected.js index 57db70d284..e271979ff0 100644 --- a/packages/create-sitecore-jss/src/templates/nextjs-styleguide/src/lib/next-config/plugins/disconnected.js +++ b/packages/create-sitecore-jss/src/templates/nextjs-styleguide/src/lib/next-config/plugins/disconnected.js @@ -3,6 +3,9 @@ const { constants } = require('@sitecore-jss/sitecore-jss-nextjs'); const disconnectedServerUrl = `http://localhost:${process.env.PROXY_PORT || 3042}/`; const isDisconnected = process.env.JSS_MODE === constants.JSS_MODE.DISCONNECTED; +/** + * @param {import('next').NextConfig} nextConfig + */ const disconnectedPlugin = (nextConfig = {}) => { if (!isDisconnected) { return nextConfig; diff --git a/packages/create-sitecore-jss/src/templates/nextjs-styleguide/src/lib/next-config/plugins/styleguide.js b/packages/create-sitecore-jss/src/templates/nextjs-styleguide/src/lib/next-config/plugins/styleguide.js index 61b5989c51..4fa624a9ff 100644 --- a/packages/create-sitecore-jss/src/templates/nextjs-styleguide/src/lib/next-config/plugins/styleguide.js +++ b/packages/create-sitecore-jss/src/templates/nextjs-styleguide/src/lib/next-config/plugins/styleguide.js @@ -1,3 +1,6 @@ +/** + * @param {import('next').NextConfig} nextConfig + */ const styleguidePlugin = (nextConfig = {}) => { return Object.assign({}, nextConfig, { i18n: { diff --git a/packages/create-sitecore-jss/src/templates/nextjs-sxa/src/lib/middleware/plugins/redirects.ts b/packages/create-sitecore-jss/src/templates/nextjs-sxa/src/lib/middleware/plugins/redirects.ts index 583c632090..b85be4746b 100644 --- a/packages/create-sitecore-jss/src/templates/nextjs-sxa/src/lib/middleware/plugins/redirects.ts +++ b/packages/create-sitecore-jss/src/templates/nextjs-sxa/src/lib/middleware/plugins/redirects.ts @@ -2,7 +2,6 @@ import { NextRequest, NextResponse } from 'next/server'; import { RedirectsMiddleware } from '@sitecore-jss/sitecore-jss-nextjs/edge'; import config from 'temp/config'; import { MiddlewarePlugin } from '..'; -import nextConfig from '../../../../next.config'; class RedirectsPlugin implements MiddlewarePlugin { private redirectsMiddleware: RedirectsMiddleware; @@ -13,7 +12,9 @@ class RedirectsPlugin implements MiddlewarePlugin { endpoint: config.graphQLEndpoint, apiKey: config.sitecoreApiKey, siteName: config.jssAppName, - locales: nextConfig().i18n.locales, + // These are all the locales you support in your application. + // These should match those in your next.config.js (i18n.locales). + locales: ['en'], }); } diff --git a/packages/create-sitecore-jss/src/templates/nextjs-sxa/src/lib/next-config/plugins/robots.js b/packages/create-sitecore-jss/src/templates/nextjs-sxa/src/lib/next-config/plugins/robots.js index e71f335807..b4d9d678c3 100644 --- a/packages/create-sitecore-jss/src/templates/nextjs-sxa/src/lib/next-config/plugins/robots.js +++ b/packages/create-sitecore-jss/src/templates/nextjs-sxa/src/lib/next-config/plugins/robots.js @@ -1,3 +1,6 @@ +/** + * @param {import('next').NextConfig} nextConfig + */ const robotsPlugin = (nextConfig = {}) => { return Object.assign({}, nextConfig, { async rewrites() { diff --git a/packages/create-sitecore-jss/src/templates/nextjs-sxa/src/lib/next-config/plugins/sitemap.js b/packages/create-sitecore-jss/src/templates/nextjs-sxa/src/lib/next-config/plugins/sitemap.js index f8b8a61a18..0f07905abd 100644 --- a/packages/create-sitecore-jss/src/templates/nextjs-sxa/src/lib/next-config/plugins/sitemap.js +++ b/packages/create-sitecore-jss/src/templates/nextjs-sxa/src/lib/next-config/plugins/sitemap.js @@ -1,3 +1,6 @@ +/** + * @param {import('next').NextConfig} nextConfig + */ const sitemapPlugin = (nextConfig = {}) => { return Object.assign({}, nextConfig, { async rewrites() { diff --git a/packages/create-sitecore-jss/src/templates/nextjs/next.config.js b/packages/create-sitecore-jss/src/templates/nextjs/next.config.js index eaec6d312e..334deb0287 100644 --- a/packages/create-sitecore-jss/src/templates/nextjs/next.config.js +++ b/packages/create-sitecore-jss/src/templates/nextjs/next.config.js @@ -5,6 +5,9 @@ const plugins = require('./src/temp/next-config-plugins') || {}; const publicUrl = getPublicUrl(); +/** + * @type {import('next').NextConfig} + */ const nextConfig = { // Set assetPrefix to our public URL assetPrefix: publicUrl, diff --git a/packages/create-sitecore-jss/src/templates/nextjs/src/lib/next-config/plugins/edge.js b/packages/create-sitecore-jss/src/templates/nextjs/src/lib/next-config/plugins/edge.js index 931742e93a..e059292ec7 100644 --- a/packages/create-sitecore-jss/src/templates/nextjs/src/lib/next-config/plugins/edge.js +++ b/packages/create-sitecore-jss/src/templates/nextjs/src/lib/next-config/plugins/edge.js @@ -1,3 +1,6 @@ +/** + * @param {import('next').NextConfig} nextConfig + */ const edgePlugin = (nextConfig = {}) => { return Object.assign({}, nextConfig, { webpack: (config, options) => { @@ -6,7 +9,7 @@ const edgePlugin = (nextConfig = {}) => { // Point the Edge compiler in the right direction for 3rd-party module browser bundles. // debug - config.resolve.alias.debug = require.resolve('debug/src/browser'); + config.resolve.alias['debug'] = require.resolve('debug/src/browser'); // graphql-request config.resolve.alias['cross-fetch'] = require.resolve('cross-fetch/dist/browser-ponyfill'); diff --git a/packages/create-sitecore-jss/src/templates/nextjs/src/lib/next-config/plugins/graphql.js b/packages/create-sitecore-jss/src/templates/nextjs/src/lib/next-config/plugins/graphql.js index 5f2e44d777..c38787a85b 100644 --- a/packages/create-sitecore-jss/src/templates/nextjs/src/lib/next-config/plugins/graphql.js +++ b/packages/create-sitecore-jss/src/templates/nextjs/src/lib/next-config/plugins/graphql.js @@ -1,3 +1,6 @@ +/** + * @param {import('next').NextConfig} nextConfig + */ const graphqlPlugin = (nextConfig = {}) => { return Object.assign({}, nextConfig, { webpack: (config, options) => { diff --git a/packages/create-sitecore-jss/src/templates/nextjs/src/lib/next-config/plugins/monorepo.js b/packages/create-sitecore-jss/src/templates/nextjs/src/lib/next-config/plugins/monorepo.js index 61683243d7..5c818ded7a 100644 --- a/packages/create-sitecore-jss/src/templates/nextjs/src/lib/next-config/plugins/monorepo.js +++ b/packages/create-sitecore-jss/src/templates/nextjs/src/lib/next-config/plugins/monorepo.js @@ -5,6 +5,9 @@ const path = require('path'); const CWD = process.cwd(); +/** + * @param {import('next').NextConfig} nextConfig + */ const monorepoPlugin = (nextConfig = {}) => { return withTM(Object.assign({}, nextConfig, { webpack: (config, options) => {