Skip to content

Commit e3e7cd1

Browse files
authored
feat: add NEXT_PLUGIN_FORCE_RUN to skip auto-detection (#547)
* feat: add NEXT_PLUGIN_FORCE_RUN to skip auto-detection * chore: add tests * chore: add plugin removal info
1 parent 4a69dcc commit e3e7cd1

File tree

4 files changed

+65
-14
lines changed

4 files changed

+65
-14
lines changed

README.md

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -48,15 +48,9 @@ If your Next.js project was already deployed to Netlify pre-3.0.0, use the Netli
4848
package = "@netlify/plugin-nextjs"
4949
```
5050

51-
Note: the plugin does not run for statically exported Next.js sites (aka sites that use `next export`). To use the plugin, you should use the `[build]` config in the .toml snippet above. Be sure to exclude `next export` from your build script.
52-
53-
You can also add context-specific properties and environment variables to your `netlify.toml`. Read more about [deploy contexts](https://docs.netlify.com/configure-builds/file-based-configuration/#deploy-contexts) in our docs. For example:
54-
55-
```toml
56-
[context.production.environment]
57-
NEXT_SERVERLESS = "true"
58-
NODE_ENV = "production"
59-
```
51+
Note: the plugin does not run for statically exported Next.js sites (aka sites that use `next export`). To use the plugin, you should use the `[build]` config in the .toml snippet above. Be sure to exclude `next export` from your build script.
52+
The plugin will attempt to detect if the site uses static export or Storybook, and will not run for either. If you want to disable the auto-detection, you can set the `NEXT_PLUGIN_FORCE_RUN` environment variable to `true` or `false`.
53+
Setting it to `true` or `1` will mean the plugin always runs, and setting it to `false` or `0` will mean it never runs. If unset, auto-detection will be used. This variable should be set in the Netlify UI or in the `netlify.toml` file.
6054

6155
2\. From your project's base directory, use `npm`, `yarn`, or any other Node.js package manager to add this plugin to `dependencies` in `package.json`.
6256

helpers/doesNotNeedPlugin.js

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,26 @@ const isStaticExportProject = require('./isStaticExportProject')
66
const usesBuildCommand = require('./usesBuildCommand')
77

88
const doesNotNeedPlugin = ({ netlifyConfig, packageJson }) => {
9+
// The env var skips the auto-detection
10+
const envVar = process.env.NEXT_PLUGIN_FORCE_RUN
11+
if (envVar === 'false' || envVar === '0' || envVar === false) {
12+
console.log(
13+
yellowBright`The env var NEXT_PLUGIN_FORCE_RUN was set to ${JSON.stringify(
14+
envVar,
15+
)}, so auto-detection is disabled and the plugin will not run`,
16+
)
17+
return true
18+
}
19+
if (envVar === 'true' || envVar === '1' || envVar === true) {
20+
console.log(
21+
yellowBright`The env var NEXT_PLUGIN_FORCE_RUN was set to ${JSON.stringify(
22+
envVar,
23+
)}, so auto-detection is disabled and the plugin will run regardless`,
24+
)
25+
return false
26+
}
27+
// Otherwise use auto-detection
28+
929
const { build } = netlifyConfig
1030
const { scripts = {} } = packageJson
1131

@@ -18,7 +38,7 @@ const doesNotNeedPlugin = ({ netlifyConfig, packageJson }) => {
1838

1939
if (usesBuildCommand({ build, scripts, command: 'build-storybook' })) {
2040
console.log(
21-
yellowBright`Site seems to be building a Storybook rather than the Next.js site, so the Essential Next.js plugin will not run.`,
41+
yellowBright`Site seems to be building a Storybook rather than the Next.js site, so the Essential Next.js plugin will not run. If this is incorrect, set NEXT_PLUGIN_FORCE_RUN to true`,
2242
)
2343
return true
2444
}

helpers/usesBuildCommand.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,12 @@ const usesBuildCommand = ({ build, scripts, command }) => {
2424
return raw.some((script) => script.includes(command))
2525
} catch (error) {
2626
console.error(
27-
'There was an error parsing your build command:',
28-
error.message,
29-
`\n\nThe build command is "${build.command}" and the available npm scripts are: ${Object.keys(scripts)
27+
`Static export detection disabled because we could not parse your build command: ${error.message}
28+
The build command is "${build.command}" and the available npm scripts are: ${Object.keys(scripts)
3029
.map((script) => `"${script}"`)
31-
.join(', ')}`,
30+
.join(', ')}
31+
32+
If the site does use static export then you can set the env var NEXT_PLUGIN_FORCE_RUN to "false" or uninstall the plugin. See https://ntl.fyi/remove-plugin for instructions.`,
3233
)
3334
}
3435
}

test/index.js

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,42 @@ describe('preBuild()', () => {
143143
expect(process.env.NEXT_PRIVATE_TARGET).toBe('serverless')
144144
})
145145

146+
test('run plugin if `NEXT_PLUGIN_FORCE_RUN` is set to true, even if next export is in script', async () => {
147+
process.env.NEXT_PLUGIN_FORCE_RUN = 'true'
148+
await plugin.onPreBuild({
149+
netlifyConfig,
150+
packageJson: { ...DUMMY_PACKAGE_JSON, scripts: { build: 'next export' } },
151+
utils,
152+
constants: {},
153+
})
154+
expect(process.env.NEXT_PRIVATE_TARGET).toBe('serverless')
155+
process.env.NEXT_PLUGIN_FORCE_RUN = undefined
156+
})
157+
158+
test('run plugin if `NEXT_PLUGIN_FORCE_RUN` is set to true, even if build-storybook is in script', async () => {
159+
process.env.NEXT_PLUGIN_FORCE_RUN = 'true'
160+
await plugin.onPreBuild({
161+
netlifyConfig,
162+
packageJson: { ...DUMMY_PACKAGE_JSON, scripts: { build: 'build-storybook' } },
163+
utils,
164+
constants: {},
165+
})
166+
expect(process.env.NEXT_PRIVATE_TARGET).toBe('serverless')
167+
process.env.NEXT_PLUGIN_FORCE_RUN = undefined
168+
})
169+
170+
test('not run plugin if `NEXT_PLUGIN_FORCE_RUN` is set to false', async () => {
171+
process.env.NEXT_PLUGIN_FORCE_RUN = 'false'
172+
await plugin.onPreBuild({
173+
netlifyConfig,
174+
packageJson: DUMMY_PACKAGE_JSON,
175+
utils,
176+
constants: {},
177+
})
178+
expect(process.env.NEXT_PRIVATE_TARGET).toBeUndefined()
179+
process.env.NEXT_PLUGIN_FORCE_RUN = undefined
180+
})
181+
146182
test('do nothing if app has static html export in toml/ntl config', async () => {
147183
await plugin.onPreBuild({
148184
netlifyConfig: { build: { command: 'next build && next export' } },

0 commit comments

Comments
 (0)