You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: LEARNING.md
+24-21Lines changed: 24 additions & 21 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -3,26 +3,27 @@
3
3
> 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**.
4
4
5
5
### 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.
// 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
+
constisProd=process.env.NODE_ENV==='production';
11
+
12
+
/**@type{import('next').NextConfig}*/
13
+
constnextConfig= {
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
+
exportdefaultnextConfig;
26
27
```
27
28
28
29
> [!NOTE]
@@ -38,26 +39,28 @@ export default (phase) => {
38
39
- Locate the `Source` dropdown, which is likely set to `Deploy from a branch`.
39
40
- Click `Deploy from a branch` and switch it to `Github Actions`.
40
41
- 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:
42
43
```yml
43
44
- name: Build with Next.js
44
45
run: ${{ steps.detect-package-manager.outputs.runner }} next build
45
46
```
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:
47
48
```yml
48
49
- name: Build with Next.js
49
-
run: ${{ steps.detect-package-manager.outputs.runner }} next build
**Without the `cp` command**, the Github Pages URL will not find the requested files, and **your page will 404!**
54
56
- In the next section of that same file, update the `path` where the binaries are located; change `path: ./out` to `path: ./.next/standalone`:
55
57
```yml
56
58
- name: Upload artifact
57
59
uses: actions/upload-pages-artifact@v3
58
60
with:
59
61
path: ./.next/standalone
60
62
```
63
+
Failing to do so will result in build errors because tar cannot find the proper directory to archive.
61
64
- 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:
62
65
```yml
63
66
- name: Setup Pages
@@ -70,7 +73,7 @@ export default (phase) => {
70
73
# You may remove this line if you want to manage the configuration yourself.
71
74
# static_site_generator: next
72
75
```
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!
74
77
- 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.
0 commit comments