Skip to content

Commit 336ec6e

Browse files
committed
Add cp command to build step to copy extra dirs to standalone
1 parent 436cb0c commit 336ec6e

File tree

3 files changed

+57
-51
lines changed

3 files changed

+57
-51
lines changed

.github/workflows/nextjs.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ jobs:
7575
- name: Install dependencies
7676
run: ${{ steps.detect-package-manager.outputs.manager }} ${{ steps.detect-package-manager.outputs.command }}
7777
- name: Build with Next.js
78-
run: ${{ steps.detect-package-manager.outputs.runner }} next build
78+
run: ${{ steps.detect-package-manager.outputs.runner }} next build && cp -r ./public ./.next/standalone/ && cp -r ./.next/static ./.next/standalone/.next/
7979
env:
8080
REACT_APP_ALCHEMY_API_KEY: ${{ secrets.REACT_APP_ALCHEMY_API_KEY }}
8181
REACT_APP_ETHERSCAN_API_KEY: ${{ secrets.REACT_APP_ETHERSCAN_API_KEY }}

LEARNING.md

Lines changed: 24 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -3,26 +3,27 @@
33
> This site was initially deployed to Vercel, but my free tier was canceled because of too much traffic. So now I'm documenting how to **deploy to Github Pages with Github Actions**.
44
55
### 1. Configure the Next.js Build Process
6-
By default, Next.js uses Node.js to run the application, which is incompatible with GitHub Pages. We need to enable static page generation in Next.js in order to deploy to Github Pages.
6+
By default, Next.js uses Node.js to run the application, which is incompatible with GitHub Pages. We need to enable static or standalone page generation in Next.js in order to deploy to Github Pages.
77

88
Update `next.config.mjs` with the following:
99
```mjs
10-
import { PHASE_PRODUCTION_BUILD, PHASE_PRODUCTION_SERVER } from 'next/constants.js';
11-
12-
export default (phase) => {
13-
const isProd = phase === PHASE_PRODUCTION_BUILD || phase === PHASE_PRODUCTION_SERVER;
14-
15-
/** @type {import('next').NextConfig} */
16-
const nextConfig = {
17-
// Enable standalone export for Github Pages:
18-
output: 'standalone',
19-
// Map all static assets to the project URL davidde.github.io/ethblox,
20-
// instead of the base davidde.github.io domain, but only for production:
21-
basePath: isProd ? '/ethblox' : undefined,
22-
};
23-
24-
return nextConfig;
25-
}
10+
const isProd = process.env.NODE_ENV === 'production';
11+
12+
/** @type {import('next').NextConfig} */
13+
const nextConfig = {
14+
// Enable standalone export for Github Pages:
15+
output: 'standalone',
16+
// Map all static assets to the project URL davidde.github.io/ethblox,
17+
// instead of the base davidde.github.io domain, but only for production:
18+
basePath: isProd ? '/ethblox' : undefined,
19+
// Note that all `npm run build` commands will get classified as 'production',
20+
// so they will get the '/ethblox' basePath even when run locally.
21+
// This means that when running the build with `node .next/standalone/server.js` locally,
22+
// the base URL is `http://localhost:3000/ethblox/`, and the default
23+
// `http://localhost:3000/` will 404.
24+
};
25+
26+
export default nextConfig;
2627
```
2728

2829
> [!NOTE]
@@ -38,26 +39,28 @@ export default (phase) => {
3839
- Locate the `Source` dropdown, which is likely set to `Deploy from a branch`.
3940
- Click `Deploy from a branch` and switch it to `Github Actions`.
4041
- Click `Configure` in the Github Actions field, which will take you to a `/.github/workflows/nextjs.yml` action configuration file.
41-
- In this file, we need to also add the API keys to the build step. Find the following text:
42+
- In this file, we need to add the API keys to the build step, as well as copy some extra folders to the build output because of the `output: 'standalone'`. Find the following text:
4243
```yml
4344
- name: Build with Next.js
4445
run: ${{ steps.detect-package-manager.outputs.runner }} next build
4546
```
46-
and add an `env` section to it:
47+
Now, add ` && cp -r ./public ./.next/standalone/ && cp -r ./.next/static ./.next/standalone/.next/` to the build command, and add the following `env` section:
4748
```yml
4849
- name: Build with Next.js
49-
run: ${{ steps.detect-package-manager.outputs.runner }} next build
50+
run: ${{ steps.detect-package-manager.outputs.runner }} next build && cp -r ./public ./.next/standalone/ && cp -r ./.next/static ./.next/standalone/.next/
5051
env:
5152
REACT_APP_ALCHEMY_API_KEY: ${{ secrets.REACT_APP_ALCHEMY_API_KEY }}
5253
REACT_APP_ETHERSCAN_API_KEY: ${{ secrets.REACT_APP_ETHERSCAN_API_KEY }}
5354
```
55+
**Without the `cp` command**, the Github Pages URL will not find the requested files, and **your page will 404!**
5456
- In the next section of that same file, update the `path` where the binaries are located; change `path: ./out` to `path: ./.next/standalone`:
5557
```yml
5658
- name: Upload artifact
5759
uses: actions/upload-pages-artifact@v3
5860
with:
5961
path: ./.next/standalone
6062
```
63+
Failing to do so will result in build errors because tar cannot find the proper directory to archive.
6164
- Still in that file, also comment out the line that says `static_site_generator: next` under `- name: Setup Pages`. Additonally, you'll also need to comment out the `with:` header, because an empty field is invalid:
6265
```yml
6366
- name: Setup Pages
@@ -70,7 +73,7 @@ export default (phase) => {
7073
# You may remove this line if you want to manage the configuration yourself.
7174
# static_site_generator: next
7275
```
73-
If you do not comment these out, the build process will ignore the local `next.config.mjs` file, which is necessary for the proper configuration!
76+
If you do not comment these out, the build process will ignore the local `next.config.mjs` file, and you will get build errors!
7477
- Finally, click `Commit changes...` to commit it to the main branch. After committing, GitHub will automatically initiate the deployment to GitHub Pages. You can inspect this process in your project's `Actions` tab, which you can find in the middle of the `Code` and `Settings` tabs.
7578

7679
## Security

next.config.mjs

Lines changed: 32 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,37 @@
1-
import { PHASE_PRODUCTION_BUILD, PHASE_PRODUCTION_SERVER } from 'next/constants.js';
21

3-
export default (phase) => {
4-
const isProd = phase === PHASE_PRODUCTION_BUILD || phase === PHASE_PRODUCTION_SERVER;
2+
const isProd = process.env.NODE_ENV === 'production';
53

6-
/** @type {import('next').NextConfig} */
7-
const nextConfig = {
8-
// experimental: {
9-
/* Fix: "Failed to generate cache key for"
10-
// https://github.com/connectrpc/connect-es/issues/1326
11-
// https://nextjs.org/docs/app/api-reference/config/next-config-js/serverComponentsHmrCache
12-
*/
13-
// serverComponentsHmrCache: false, // defaults to true
14-
// },
4+
/** @type {import('next').NextConfig} */
5+
const nextConfig = {
6+
// experimental: {
7+
/* Fix: "Failed to generate cache key for"
8+
// https://github.com/connectrpc/connect-es/issues/1326
9+
// https://nextjs.org/docs/app/api-reference/config/next-config-js/serverComponentsHmrCache
10+
*/
11+
// serverComponentsHmrCache: false, // defaults to true
12+
// },
1513

16-
// Enable standalone export for Github Pages:
17-
output: 'standalone',
18-
// Map all static assets to the project URL davidde.github.io/ethblox,
19-
// instead of the base davidde.github.io domain, but only for production:
20-
basePath: isProd ? '/ethblox' : undefined,
14+
// Enable standalone export for Github Pages:
15+
output: 'standalone',
16+
// Map all static assets to the project URL davidde.github.io/ethblox,
17+
// instead of the base davidde.github.io domain, but only for production:
18+
basePath: isProd ? '/ethblox' : undefined,
19+
// Note that all `npm run build` commands will get classified as 'production',
20+
// so they will get the '/ethblox' basePath even when run locally.
21+
// This means that when running the build with `node .next/standalone/server.js` locally,
22+
// the base URL is `http://localhost:3000/ethblox/`, and the default
23+
// `http://localhost:3000/` will 404.
2124

22-
async redirects() {
23-
return [
24-
{
25-
source: '/',
26-
destination: '/mainnet',
27-
permanent: true,
28-
},
29-
]
30-
},
31-
};
25+
async redirects() {
26+
return [
27+
{
28+
source: '/',
29+
destination: '/mainnet',
30+
permanent: true,
31+
},
32+
]
33+
},
34+
};
35+
36+
export default nextConfig;
3237

33-
return nextConfig;
34-
}

0 commit comments

Comments
 (0)