Unverified Commit c4fdfcb7 authored by Lee Robinson's avatar Lee Robinson Committed by GitHub

Update GitHub Pages deployment example. (#37282)

Verified it's working: https://leerob.github.io/gh-pages-test/
parent 23013674
const env = require('./env-config')
module.exports = {
presets: ['next/babel'],
plugins: [['transform-define', env]],
}
# gh-pages Hello World example
This example shows the most basic idea behind Next. We have 2 pages: `pages/index.js` and `pages/about.js`. The former responds to `/` requests and the latter to `/about`. Using `next/link` you can add hyperlinks between them with universal routing capabilities.
## How to use
Execute [`create-next-app`](https://github.com/vercel/next.js/tree/canary/packages/create-next-app) with [npm](https://docs.npmjs.com/cli/init), [Yarn](https://yarnpkg.com/lang/en/docs/cli/create/), or [pnpm](https://pnpm.io) to bootstrap the example:
```bash
npx create-next-app --example gh-pages gh-pages-app
# or
yarn create next-app --example gh-pages gh-pages-app
# or
pnpm create next-app --example gh-pages gh-pages-app
```
### Deploy it to github
Edit `env-config.js` and replace `'Next-gh-page-example'` by your project name.
Edit `next.config.js` and replace `'Next-gh-page-example'` by your project name.
1. Create repository.
2. Link it to your github account.
3. Publish your default branch, e.g. `main`.
```bash
npm run deploy
```
Test it:
Replace `<github-user-name>` and `<github-project-name>`
```bash
https://<github-user-name>.github.io/<github-project-name>/
```
Example:
```bash
https://github.com/thierryc/Next-gh-page-example/
https://thierryc.github.io/Next-gh-page-example/
```
const prod = process.env.NODE_ENV === 'production'
module.exports = {
'process.env.BACKEND_URL': prod ? '/Next-gh-page-example' : '',
}
// This file is not going through babel transformation.
// So, we write it in vanilla JS
// (But you could use ES2015 features supported by your Node.js version)
const debug = process.env.NODE_ENV !== 'production'
module.exports = {
assetPrefix: !debug ? '/Next-gh-page-example/' : '',
}
{
"private": true,
"scripts": {
"dev": "next",
"build": "next build",
"start": "next start",
"export": "next export",
"deploy": "rimraf node_modules/.cache && next build && next export && nodetouch out/.nojekyll && git add out/ && git commit -m \"Deploy Next.js to gh-pages\" && git subtree push --prefix out origin gh-pages"
},
"dependencies": {
"next": "latest",
"react": "^17.0.2",
"react-dom": "^17.0.2"
},
"devDependencies": {
"babel-plugin-transform-define": "^1.3.0",
"rimraf": "^3.0.2",
"touch": "^3.1.0"
}
}
# Deploying to GitHub Pages
This example supports deloying a static Next.js application (using `next export`) to GitHub Pages.
## How to use
Execute [`create-next-app`](https://github.com/vercel/next.js/tree/canary/packages/create-next-app) with [npm](https://docs.npmjs.com/cli/init), [Yarn](https://yarnpkg.com/lang/en/docs/cli/create/), or [pnpm](https://pnpm.io) to bootstrap the example:
```bash
npx create-next-app --example github-pages nextjs-github-pages
# or
yarn create next-app --example github-pages nextjs-github-pages
# or
pnpm create next-app --example github-pages nextjs-github-pages
```
### Deploy to GitHub Pages
1. Create a new public GitHub repository.
1. Edit `next.config.js` to match your GitHub repository name.
1. Push the starter code to the `main` branch.
1. Run the `deploy` script (e.g. `npm run deploy`) to create the `gh-pages` branch.
1. On GitHub, go to **Settings** > **Pages** > **Source**, and choose `gh-pages` as the branch with the `/root` folder. Hit **Save**.
1. Make a change.
1. Run the `deploy` script again to push the changes to GitHub Pages.
Congratulations! You should have a URL like:
```bash
https://<github-user-name>.github.io/<github-project-name>/
```
module.exports = {
basePath: '/gh-pages-test',
}
{
"private": true,
"scripts": {
"dev": "next",
"build": "next build && next export",
"deploy": "next build && touch out/.nojekyll && git add out/ && git commit -m \"Deploy\" && git subtree push --prefix out origin gh-pages"
},
"dependencies": {
"next": "latest",
"react": "latest",
"react-dom": "latest"
}
}
import Link from 'next/link' import Link from 'next/link'
export default function About() { export default function About() {
return ( return (
<div> <div>
<div>About us</div> <div>About</div>
<div> <div>
Back to{' '} Back to <Link href="/">Home</Link>
<Link href="/" as={process.env.BACKEND_URL + '/'}>
<a>Home</a>
</Link>
</div> </div>
</div> </div>
) )
......
import Link from 'next/link' import Link from 'next/link'
export default function Home() { export default function Home() {
return ( return (
<div> <div>
Hello World.{' '} Hello World. <Link href="/about">About</Link>
<Link href="/about" as={process.env.BACKEND_URL + '/about'}>
<a>About</a>
</Link>
</div> </div>
) )
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment