Skip to content

feat: skip run if @netlify/plugin-nextjs installed #1536

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Aug 16, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions packages/runtime/src/helpers/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, string> | null => {
const customImageResponseHeaders = headers.find((header) => header.for?.startsWith('/_next/image/'))

Expand Down
16 changes: 14 additions & 2 deletions packages/runtime/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import { updateConfig, writeEdgeFunctions, loadMiddlewareManifest } from './help
import { moveStaticPages, movePublicFiles, patchNextFiles } 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,
Expand All @@ -39,6 +39,10 @@ const plugin: NetlifyPlugin = {
cache,
},
}) {
if (isOldPluginInstalled()) {
return
}

const { publish } = netlifyConfig.build
if (shouldSkip()) {
await restoreCache({ cache, publish })
Expand All @@ -65,7 +69,7 @@ const plugin: NetlifyPlugin = {
build: { failBuild },
},
}) {
if (shouldSkip()) {
if (isOldPluginInstalled() || shouldSkip()) {
return
}
const { publish } = netlifyConfig.build
Expand Down Expand Up @@ -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. Learn more: https://ntl.fyi/3w85e2E',
})
return
}

if (shouldSkip()) {
status.show({
title: 'Essential Next.js plugin did not run',
Expand Down
40 changes: 38 additions & 2 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
}
})

Expand Down Expand Up @@ -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`
Expand Down Expand Up @@ -560,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. Learn more: https://ntl.fyi/3w85e2E" })

})

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()

Expand Down