From 698fe18f17817a97f504809860423104baac888d Mon Sep 17 00:00:00 2001 From: Erica Pisani Date: Mon, 15 Aug 2022 12:46:57 -0400 Subject: [PATCH 1/3] feat: skip run if @netlify/plugin-nextjs installed disable in order to avoid conflicts should the old package be installed in the project. --- packages/runtime/src/helpers/utils.ts | 11 +++++++++++ packages/runtime/src/index.ts | 16 ++++++++++++++-- test/index.js | 3 +-- 3 files changed, 26 insertions(+), 4 deletions(-) diff --git a/packages/runtime/src/helpers/utils.ts b/packages/runtime/src/helpers/utils.ts index 4d83cf45bd..ad328e9b88 100644 --- a/packages/runtime/src/helpers/utils.ts +++ b/packages/runtime/src/helpers/utils.ts @@ -188,6 +188,17 @@ export const isNextAuthInstalled = (): boolean => { } } +export const isOldPluginInstalled = (): boolean => { + try { + // eslint-disable-next-line import/no-unassigned-import, import/no-extraneous-dependencies, n/no-extraneous-require + require('@netlify/plugin-nextjs') + return true + } catch { + // Ignore the MODULE_NOT_FOUND error + return false + } +} + export const getCustomImageResponseHeaders = (headers: Header[]): Record | null => { const customImageResponseHeaders = headers.find((header) => header.for?.startsWith('/_next/image/')) diff --git a/packages/runtime/src/index.ts b/packages/runtime/src/index.ts index ad12b0753a..dbd5991081 100644 --- a/packages/runtime/src/index.ts +++ b/packages/runtime/src/index.ts @@ -19,7 +19,7 @@ import { updateConfig, writeEdgeFunctions, loadMiddlewareManifest } from './help import { moveStaticPages, movePublicFiles, patchNextFiles, unpatchNextFiles } from './helpers/files' import { generateFunctions, setupImageFunction, generatePagesResolver } from './helpers/functions' import { generateRedirects, generateStaticRedirects } from './helpers/redirects' -import { shouldSkip, isNextAuthInstalled, getCustomImageResponseHeaders } from './helpers/utils' +import { shouldSkip, isNextAuthInstalled, isOldPluginInstalled, getCustomImageResponseHeaders } from './helpers/utils' import { verifyNetlifyBuildVersion, checkNextSiteHasBuilt, @@ -39,6 +39,10 @@ const plugin: NetlifyPlugin = { cache, }, }) { + if (isOldPluginInstalled()) { + return + } + const { publish } = netlifyConfig.build if (shouldSkip()) { await restoreCache({ cache, publish }) @@ -65,7 +69,7 @@ const plugin: NetlifyPlugin = { build: { failBuild }, }, }) { - if (shouldSkip()) { + if (isOldPluginInstalled() || shouldSkip()) { return } const { publish } = netlifyConfig.build @@ -178,6 +182,14 @@ const plugin: NetlifyPlugin = { }) { await saveCache({ cache, publish }) + if (isOldPluginInstalled()) { + status.show({ + summary: + 'Please remove @netlify/plugin-nextjs from your site. It is no longer required and will prevent you using new features.', + }) + return + } + if (shouldSkip()) { status.show({ title: 'Essential Next.js plugin did not run', diff --git a/test/index.js b/test/index.js index c150b8b66b..400c7cef69 100644 --- a/test/index.js +++ b/test/index.js @@ -2,6 +2,7 @@ jest.mock('../packages/runtime/src/helpers/utils', () => { return { ...jest.requireActual('../packages/runtime/src/helpers/utils'), isNextAuthInstalled: jest.fn(), + isOldPluginInstalled: jest.fn(), } }) @@ -33,8 +34,6 @@ const { } = require('../packages/runtime/src/helpers/config') const { dirname } = require('path') const { getProblematicUserRewrites } = require('../packages/runtime/src/helpers/verification') -const { onPostBuild } = require('../packages/runtime/lib') -const { basePath } = require('../demos/next-i18next/next.config') const chance = new Chance() const FIXTURES_DIR = `${__dirname}/fixtures` From 65700ab946e98dbdb8f28ee6ee228beec447f199 Mon Sep 17 00:00:00 2001 From: Erica Pisani Date: Mon, 15 Aug 2022 13:07:58 -0400 Subject: [PATCH 2/3] test: add a couple of tests --- test/index.js | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/test/index.js b/test/index.js index 400c7cef69..92814db02b 100644 --- a/test/index.js +++ b/test/index.js @@ -559,6 +559,43 @@ describe('onBuild()', () => { }) describe('onPostBuild', () => { + const { isOldPluginInstalled } = require('../packages/runtime/src/helpers/utils') + + test('show warning message to remove old plugin', async () => { + isOldPluginInstalled.mockImplementation(() => { + return true + }) + const mockStatusFunc = jest.fn() + + await plugin.onPostBuild({ + ...defaultArgs, + utils: { ...utils, status: { show: mockStatusFunc } } + }) + + expect(mockStatusFunc).toHaveBeenCalledWith({ "summary": "Please remove @netlify/plugin-nextjs from your site. It is no longer required and will prevent you using new features." }) + + }) + + test('does not show warning message to remove old plugin', async () => { + isOldPluginInstalled.mockImplementation(() => { + return false + }) + const mockStatusFunc = jest.fn() + await moveNextDist() + + await plugin.onPostBuild({ + ...defaultArgs, + utils: { + ...utils, + status: { show: mockStatusFunc }, + functions: { list: jest.fn().mockResolvedValue([]) } + } + }) + + expect(mockStatusFunc).not.toHaveBeenCalledWith({ "summary": "Please remove @netlify/plugin-nextjs from your site. It is no longer required and will prevent you using new features." }) + + }) + test('saves cache with right paths', async () => { await moveNextDist() From 2c0fdd93a1ffcbeb897b1e3ffb39786d45144188 Mon Sep 17 00:00:00 2001 From: Erica Pisani Date: Tue, 16 Aug 2022 09:18:37 -0400 Subject: [PATCH 3/3] feat: add shortlink Shortlink will eventually link to related documentation --- packages/runtime/src/index.ts | 2 +- test/index.js | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/runtime/src/index.ts b/packages/runtime/src/index.ts index bfd63b6271..01911a1c5a 100644 --- a/packages/runtime/src/index.ts +++ b/packages/runtime/src/index.ts @@ -185,7 +185,7 @@ const plugin: NetlifyPlugin = { if (isOldPluginInstalled()) { status.show({ summary: - 'Please remove @netlify/plugin-nextjs from your site. It is no longer required and will prevent you using new features.', + 'Please remove @netlify/plugin-nextjs from your site. It is no longer required and will prevent you using new features. Learn more: https://ntl.fyi/3w85e2E', }) return } diff --git a/test/index.js b/test/index.js index 92814db02b..dc3f8c41c2 100644 --- a/test/index.js +++ b/test/index.js @@ -572,7 +572,7 @@ describe('onPostBuild', () => { utils: { ...utils, status: { show: mockStatusFunc } } }) - expect(mockStatusFunc).toHaveBeenCalledWith({ "summary": "Please remove @netlify/plugin-nextjs from your site. It is no longer required and will prevent you using new features." }) + expect(mockStatusFunc).toHaveBeenCalledWith({ "summary": "Please remove @netlify/plugin-nextjs from your site. It is no longer required and will prevent you using new features. Learn more: https://ntl.fyi/3w85e2E" }) })