Skip to content

Commit 0b4cf8c

Browse files
authored
feat: skip run if @netlify/plugin-nextjs installed (#1536)
* feat: skip run if @netlify/plugin-nextjs installed disable in order to avoid conflicts should the old package be installed in the project. * test: add a couple of tests * feat: add shortlink Shortlink will eventually link to related documentation
1 parent 5173a9a commit 0b4cf8c

File tree

3 files changed

+63
-4
lines changed

3 files changed

+63
-4
lines changed

packages/runtime/src/helpers/utils.ts

+11
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,17 @@ export const isNextAuthInstalled = (): boolean => {
188188
}
189189
}
190190

191+
export const isOldPluginInstalled = (): boolean => {
192+
try {
193+
// eslint-disable-next-line import/no-unassigned-import, import/no-extraneous-dependencies, n/no-extraneous-require
194+
require('@netlify/plugin-nextjs')
195+
return true
196+
} catch {
197+
// Ignore the MODULE_NOT_FOUND error
198+
return false
199+
}
200+
}
201+
191202
export const getCustomImageResponseHeaders = (headers: Header[]): Record<string, string> | null => {
192203
const customImageResponseHeaders = headers.find((header) => header.for?.startsWith('/_next/image/'))
193204

packages/runtime/src/index.ts

+14-2
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import { updateConfig, writeEdgeFunctions, loadMiddlewareManifest } from './help
1919
import { moveStaticPages, movePublicFiles, patchNextFiles } from './helpers/files'
2020
import { generateFunctions, setupImageFunction, generatePagesResolver } from './helpers/functions'
2121
import { generateRedirects, generateStaticRedirects } from './helpers/redirects'
22-
import { shouldSkip, isNextAuthInstalled, getCustomImageResponseHeaders } from './helpers/utils'
22+
import { shouldSkip, isNextAuthInstalled, isOldPluginInstalled, getCustomImageResponseHeaders } from './helpers/utils'
2323
import {
2424
verifyNetlifyBuildVersion,
2525
checkNextSiteHasBuilt,
@@ -39,6 +39,10 @@ const plugin: NetlifyPlugin = {
3939
cache,
4040
},
4141
}) {
42+
if (isOldPluginInstalled()) {
43+
return
44+
}
45+
4246
const { publish } = netlifyConfig.build
4347
if (shouldSkip()) {
4448
await restoreCache({ cache, publish })
@@ -65,7 +69,7 @@ const plugin: NetlifyPlugin = {
6569
build: { failBuild },
6670
},
6771
}) {
68-
if (shouldSkip()) {
72+
if (isOldPluginInstalled() || shouldSkip()) {
6973
return
7074
}
7175
const { publish } = netlifyConfig.build
@@ -178,6 +182,14 @@ const plugin: NetlifyPlugin = {
178182
}) {
179183
await saveCache({ cache, publish })
180184

185+
if (isOldPluginInstalled()) {
186+
status.show({
187+
summary:
188+
'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',
189+
})
190+
return
191+
}
192+
181193
if (shouldSkip()) {
182194
status.show({
183195
title: 'Essential Next.js plugin did not run',

test/index.js

+38-2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ jest.mock('../packages/runtime/src/helpers/utils', () => {
22
return {
33
...jest.requireActual('../packages/runtime/src/helpers/utils'),
44
isNextAuthInstalled: jest.fn(),
5+
isOldPluginInstalled: jest.fn(),
56
}
67
})
78

@@ -33,8 +34,6 @@ const {
3334
} = require('../packages/runtime/src/helpers/config')
3435
const { dirname } = require('path')
3536
const { getProblematicUserRewrites } = require('../packages/runtime/src/helpers/verification')
36-
const { onPostBuild } = require('../packages/runtime/lib')
37-
const { basePath } = require('../demos/next-i18next/next.config')
3837

3938
const chance = new Chance()
4039
const FIXTURES_DIR = `${__dirname}/fixtures`
@@ -560,6 +559,43 @@ describe('onBuild()', () => {
560559
})
561560

562561
describe('onPostBuild', () => {
562+
const { isOldPluginInstalled } = require('../packages/runtime/src/helpers/utils')
563+
564+
test('show warning message to remove old plugin', async () => {
565+
isOldPluginInstalled.mockImplementation(() => {
566+
return true
567+
})
568+
const mockStatusFunc = jest.fn()
569+
570+
await plugin.onPostBuild({
571+
...defaultArgs,
572+
utils: { ...utils, status: { show: mockStatusFunc } }
573+
})
574+
575+
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" })
576+
577+
})
578+
579+
test('does not show warning message to remove old plugin', async () => {
580+
isOldPluginInstalled.mockImplementation(() => {
581+
return false
582+
})
583+
const mockStatusFunc = jest.fn()
584+
await moveNextDist()
585+
586+
await plugin.onPostBuild({
587+
...defaultArgs,
588+
utils: {
589+
...utils,
590+
status: { show: mockStatusFunc },
591+
functions: { list: jest.fn().mockResolvedValue([]) }
592+
}
593+
})
594+
595+
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." })
596+
597+
})
598+
563599
test('saves cache with right paths', async () => {
564600
await moveNextDist()
565601

0 commit comments

Comments
 (0)