Skip to content

Commit 0cad1e6

Browse files
committed
Updated readme and action
1 parent 60b8914 commit 0cad1e6

File tree

2 files changed

+40
-4
lines changed

2 files changed

+40
-4
lines changed

.github/workflows/node.js.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ name: GitHub Pages deploy
55

66
on:
77
push:
8-
branches: [image-fix]
8+
branches: [main]
99

1010
jobs:
1111
build:

README.md

+39-3
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,10 @@ Follow the official Next.js [getting started guide](https://nextjs.org/docs/gett
1919
const nextConfig = {
2020
output: "export",
2121
images: {
22-
loader: 'akamai',
23-
path: '',
22+
loader: "akamai",
23+
path: "",
2424
},
25-
assetPrefix: './',
25+
assetPrefix: "./",
2626
};
2727

2828
export default nextConfig;
@@ -79,6 +79,9 @@ jobs:
7979
- name: Installing my packages
8080
run: npm ci
8181

82+
- name: Extract repository name
83+
run: echo "BASE_PATH=/$(echo $GITHUB_REPOSITORY | cut -d '/' -f 2)" >> $GITHUB_ENV
84+
8285
- name: Build my App
8386
run: npm run build && touch ./out/.nojekyll
8487

@@ -102,6 +105,39 @@ Congratulations! You’ve successfully deployed a Next.js web application to Git
102105
103106
If you want to see this repository’s deployment in action, you can visit the website [here](https://davealdon.github.io/Next.js-and-GitHub-Pages-Example/).
104107
108+
#### Step 4: Images
109+
110+
If you're using images out of the public folder, and serving them with Nextjs's `<Image/>` component, they won't work out of the box with static site generation. This is because there's no automatic handling of path prefixes for images.
111+
112+
Nextjs gives a way to automatically prepend various assets with your Github's repository name, such as for `.css` files, but not for images served out of the Nextjs image component. To work around this, the path needs to be prefixed manually. I prefer a simple method:
113+
114+
1. Take a look inside the `utils` folder for the `prefix.ts` file:
115+
116+
```ts
117+
const prefix = process.env.BASE_PATH || "";
118+
export { prefix };
119+
```
120+
121+
This simply looks for an environment variable path prefix, and returns it. For any image assets, we can simple reference it like this:
122+
123+
```tsx
124+
<Image
125+
src={`${prefix}/vercel.svg`}
126+
alt="Vercel Logo"
127+
width={72}
128+
height={16}
129+
/>
130+
```
131+
132+
If you use my Github action script, the assignment of this environment variable is actually handled automatically:
133+
134+
```yml
135+
- name: Extract repository name
136+
run: echo "BASE_PATH=/$(echo $GITHUB_REPOSITORY | cut -d '/' -f 2)" >> $GITHUB_ENV
137+
```
138+
139+
It pulls the repository's name out and applies it to the build, without the need for making your own `.env` file. This way everything should work automatically if you use the prefix utility.
140+
105141
### Troubleshooting
106142

107143
#### Fork problems

0 commit comments

Comments
 (0)