Skip to content

feat: add support for use with next export #1012

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 4 commits into from
Dec 16, 2021
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
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,15 @@ If you are using Nx, then you will need to point `publish` to the folder inside
The Essential Next.js plugin now fully supports ISR on Netlify. For more details see
[the ISR docs](https://github.com/netlify/netlify-plugin-nextjs/blob/main/docs/isr.md).

## Use with `next export`

If you are using `next export` to generate a static site, you do not need most of the functionality of this plugin and
you can remove it. Alternatively you can
[set the environment variable](https://docs.netlify.com/configure-builds/environment-variables/)
`NETLIFY_NEXT_PLUGIN_SKIP` to `true` and the plugin will handle caching but won't generate any functions for SSR
support. See [`demos/next-export`](https://github.com/netlify/netlify-plugin-nextjs/tree/main/demos/next-export) for an
example.

## Feedback

If you think you have found a bug in the plugin,
Expand Down
4 changes: 4 additions & 0 deletions demos/next-export/.eslintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"extends": "next",
"root": true
}
34 changes: 34 additions & 0 deletions demos/next-export/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# See https://help.github.com/articles/ignoring-files/ for more about ignoring files.

# dependencies
/node_modules
/.pnp
.pnp.js

# testing
/coverage

# next.js
/.next/
/out/

# production
/build

# misc
.DS_Store
*.pem

# debug
npm-debug.log*
yarn-debug.log*
yarn-error.log*

# local env files
.env.local
.env.development.local
.env.test.local
.env.production.local

# vercel
.vercel
2 changes: 2 additions & 0 deletions demos/next-export/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
This is an example of a site that uses `next export`. It only uses the plugin for cache handling and sets
`NETLIFY_NEXT_PLUGIN_SKIP` to disable everything else.
1 change: 1 addition & 0 deletions demos/next-export/local-plugin/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = require('../../../lib')
1 change: 1 addition & 0 deletions demos/next-export/local-plugin/manifest.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
name: '@netlify/plugin-nextjs-local'
5 changes: 5 additions & 0 deletions demos/next-export/local-plugin/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 11 additions & 0 deletions demos/next-export/local-plugin/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"name": "local-plugin",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"preinstall": "cd ../../.. && npm i"
},
"author": "",
"license": "ISC"
}
16 changes: 16 additions & 0 deletions demos/next-export/netlify.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
[build]
command = "next build && next export"
publish = "out"
ignore = "git diff --quiet $CACHED_COMMIT_REF $COMMIT_REF ../../"

[build.environment]
NETLIFY_NEXT_PLUGIN_SKIP = "true"

[dev]
framework = "#static"

[[plugins]]
package = "./local-plugin"

[[plugins]]
package = "@netlify/plugin-local-install-core"
5 changes: 5 additions & 0 deletions demos/next-export/next.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module.exports = {
// Configurable site features we support:
// distDir: 'build',
generateBuildId: () => 'build-id',
}
7 changes: 7 additions & 0 deletions demos/next-export/pages/_app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import '../styles/globals.css'

function MyApp({ Component, pageProps }) {
return <Component {...pageProps} />
}

export default MyApp
22 changes: 22 additions & 0 deletions demos/next-export/pages/another.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import Head from 'next/head'
import { useRouter } from 'next/router'
import styles from '../styles/Home.module.css'

export default function Home() {
const { locale } = useRouter()
return (
<div className={styles.container}>
<Head>
<title>Create Next App</title>
<meta name="description" content="Generated by create next app" />
<link rel="icon" href="/favicon.ico" />
</Head>

<main className={styles.main}>
<h1 className={styles.title}>Another page</h1>
</main>


</div>
)
}
45 changes: 45 additions & 0 deletions demos/next-export/pages/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import Head from 'next/head'
import { useRouter } from 'next/router'
import styles from '../styles/Home.module.css'

export default function Home() {
const { locale } = useRouter()
return (
<div className={styles.container}>
<Head>
<title>Create Next App</title>
<meta name="description" content="Generated by create next app" />
<link rel="icon" href="/favicon.ico" />
</Head>

<main className={styles.main}>
<h1 className={styles.title}>
Welcome to <a href="https://nextjs.org">Next.js!</a>
</h1>

<p>The current locale is {locale}</p>

<p className={styles.description}>
Get started by editing <code className={styles.code}>pages/index.js</code>
</p>

<div className={styles.grid}>
<a href="https://nextjs.org/docs" className={styles.card}>
<h2>Documentation &rarr;</h2>
<p>Find in-depth information about Next.js features and API.</p>
</a>

<a href="https://nextjs.org/learn" className={styles.card}>
<h2>Learn &rarr;</h2>
<p>Learn about Next.js in an interactive course with quizzes!</p>
</a>

<a href="https://github.com/vercel/next.js/tree/master/examples" className={styles.card}>
<h2>Examples &rarr;</h2>
<p>Discover and deploy boilerplate example Next.js projects.</p>
</a>
</div>
</main>
</div>
)
}
Binary file added demos/next-export/public/favicon.ico
Binary file not shown.
121 changes: 121 additions & 0 deletions demos/next-export/styles/Home.module.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
.container {
min-height: 100vh;
padding: 0 0.5rem;
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
height: 100vh;
}

.main {
padding: 5rem 0;
flex: 1;
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
}

.footer {
width: 100%;
height: 100px;
border-top: 1px solid #eaeaea;
display: flex;
justify-content: center;
align-items: center;
}

.footer a {
display: flex;
justify-content: center;
align-items: center;
flex-grow: 1;
}

.title a {
color: #0070f3;
text-decoration: none;
}

.title a:hover,
.title a:focus,
.title a:active {
text-decoration: underline;
}

.title {
margin: 0;
line-height: 1.15;
font-size: 4rem;
}

.title,
.description {
text-align: center;
}

.description {
line-height: 1.5;
font-size: 1.5rem;
}

.code {
background: #fafafa;
border-radius: 5px;
padding: 0.75rem;
font-size: 1.1rem;
font-family: Menlo, Monaco, Lucida Console, Liberation Mono, DejaVu Sans Mono,
Bitstream Vera Sans Mono, Courier New, monospace;
}

.grid {
display: flex;
align-items: center;
justify-content: center;
flex-wrap: wrap;
max-width: 800px;
margin-top: 3rem;
}

.card {
margin: 1rem;
padding: 1.5rem;
text-align: left;
color: inherit;
text-decoration: none;
border: 1px solid #eaeaea;
border-radius: 10px;
transition: color 0.15s ease, border-color 0.15s ease;
width: 45%;
}

.card:hover,
.card:focus,
.card:active {
color: #0070f3;
border-color: #0070f3;
}

.card h2 {
margin: 0 0 1rem 0;
font-size: 1.5rem;
}

.card p {
margin: 0;
font-size: 1.25rem;
line-height: 1.5;
}

.logo {
height: 1em;
margin-left: 0.5rem;
}

@media (max-width: 600px) {
.grid {
width: 100%;
flex-direction: column;
}
}
16 changes: 16 additions & 0 deletions demos/next-export/styles/globals.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
html,
body {
padding: 0;
margin: 0;
font-family: -apple-system, BlinkMacSystemFont, Segoe UI, Roboto, Oxygen,
Ubuntu, Cantarell, Fira Sans, Droid Sans, Helvetica Neue, sans-serif;
}

a {
color: inherit;
text-decoration: none;
}

* {
box-sizing: border-box;
}
6 changes: 6 additions & 0 deletions src/helpers/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,9 @@ export const netlifyRoutesForNextRoute = (nextRoute: string): Array<string> => {
.replace(DYNAMIC_PARAMETER_REGEX, '/:$1'),
)
}

export const shouldSkip = (): boolean =>
process.env.NEXT_PLUGIN_FORCE_RUN === 'false' ||
process.env.NEXT_PLUGIN_FORCE_RUN === '0' ||
process.env.NETLIFY_NEXT_PLUGIN_SKIP === 'true' ||
process.env.NETLIFY_NEXT_PLUGIN_SKIP === '1'
5 changes: 2 additions & 3 deletions src/helpers/verification.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,14 +73,13 @@ export const checkNextSiteHasBuilt = ({
publish,
)}" does not contain a Next.js production build. Perhaps the build command was not run, or you specified the wrong publish directory.
${outWarning}
If you are using "next export" then the Essential Next.js plugin should be removed. See https://ntl.fyi/remove-plugin for details.
If you are using "next export" then you should set the environment variable NETLIFY_NEXT_PLUGIN_SKIP to "true".
`)
}
if (existsSync(path.join(publish, 'export-detail.json'))) {
failBuild(outdent`
Detected that "next export" was run, but site is incorrectly publishing the ".next" directory.
This plugin is not needed for "next export" so should be removed, and publish directory set to "out".
See https://ntl.fyi/remove-plugin for more details on how to remove this plugin.
The publish directory should be set to "out", and you should set the environment variable NETLIFY_NEXT_PLUGIN_SKIP to "true".
`)
}
}
Expand Down
Loading