Skip to content

Commit 8b6fd48

Browse files
authored
Merge pull request #5 from DaveAldon/image-fix
Fix Static Images and Update Action Plugins
2 parents b909b5f + 0cad1e6 commit 8b6fd48

File tree

6 files changed

+74
-2305
lines changed

6 files changed

+74
-2305
lines changed

.github/workflows/node.js.yml

+8-5
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ jobs:
1313

1414
steps:
1515
- name: Checkout 🛎️
16-
uses: actions/checkout@v2.3.1
16+
uses: actions/checkout@v4.1.3
1717
- name: Use Node.js 18.x
1818
uses: actions/setup-node@v1
1919
with:
@@ -22,12 +22,15 @@ jobs:
2222
- name: Installing my packages
2323
run: npm ci
2424

25+
- name: Extract repository name
26+
run: echo "BASE_PATH=/$(echo $GITHUB_REPOSITORY | cut -d '/' -f 2)" >> $GITHUB_ENV
27+
2528
- name: Build my App
2629
run: npm run build && touch ./out/.nojekyll
2730

2831
- name: Deploy 🚀
29-
uses: JamesIves/github-pages-deploy-action@v4.4.1
32+
uses: JamesIves/github-pages-deploy-action@v4.6.0
3033
with:
31-
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
32-
BRANCH: public # The branch the action should deploy to.
33-
FOLDER: out # The folder the action should deploy.
34+
token: ${{ secrets.GITHUB_TOKEN }}
35+
branch: public # The branch the action should deploy to.
36+
folder: out # The folder the action should deploy to.

README.md

+45-9
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;
@@ -70,24 +70,27 @@ jobs:
7070

7171
steps:
7272
- name: Checkout 🛎️
73-
uses: actions/checkout@v2.3.1
73+
uses: actions/checkout@v4.1.3
7474
- name: Use Node.js 18.x
7575
uses: actions/setup-node@v1
7676
with:
77-
node-version: '18.x'
77+
node-version: "18.x"
7878

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

8588
- name: Deploy 🚀
86-
uses: JamesIves/github-pages-deploy-action@v4.4.1
89+
uses: JamesIves/github-pages-deploy-action@v4.6.0
8790
with:
88-
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
89-
BRANCH: public # The branch the action will deploy to
90-
FOLDER: out # The folder the action will deploy to
91+
token: ${{ secrets.GITHUB_TOKEN }}
92+
branch: public # The branch the action should deploy to.
93+
folder: out # The folder the action should deploy to.
9194
```
9295
9396
Once you commit these files, the actions tab for your repository will show your action running. Actions are triggered automatically after any commits by default.
@@ -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

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
"scripts": {
66
"dev": "next dev",
77
"build": "next build",
8-
"start": "next start",
8+
"start": "npx serve@latest out",
99
"lint": "next lint"
1010
},
1111
"dependencies": {

pages/index.tsx

+17-11
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
1-
import type { NextPage } from 'next'
2-
import Head from 'next/head'
3-
import Image from 'next/image'
4-
import styles from '../styles/Home.module.css'
1+
import type { NextPage } from "next";
2+
import Head from "next/head";
3+
import Image from "next/image";
4+
import styles from "../styles/Home.module.css";
5+
import { prefix } from "../utils/prefix";
56

67
const Home: NextPage = () => {
78
return (
89
<div className={styles.container}>
910
<Head>
1011
<title>Create Next App</title>
1112
<meta name="description" content="Generated by create next app" />
12-
<link rel="icon" href="/favicon.ico" />
13+
<link rel="icon" href={`${prefix}/favicon.ico`} />
1314
</Head>
1415

1516
<main className={styles.main}>
@@ -18,7 +19,7 @@ const Home: NextPage = () => {
1819
</h1>
1920

2021
<p className={styles.description}>
21-
Get started by editing{' '}
22+
Get started by editing{" "}
2223
<code className={styles.code}>pages/index.tsx</code>
2324
</p>
2425

@@ -59,14 +60,19 @@ const Home: NextPage = () => {
5960
target="_blank"
6061
rel="noopener noreferrer"
6162
>
62-
Powered by{' '}
63+
Powered by{" "}
6364
<span className={styles.logo}>
64-
<Image src="/vercel.svg" alt="Vercel Logo" width={72} height={16} />
65+
<Image
66+
src={`${prefix}/vercel.svg`}
67+
alt="Vercel Logo"
68+
width={72}
69+
height={16}
70+
/>
6571
</span>
6672
</a>
6773
</footer>
6874
</div>
69-
)
70-
}
75+
);
76+
};
7177

72-
export default Home
78+
export default Home;

utils/prefix.ts

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
const prefix = process.env.BASE_PATH || "";
2+
3+
export { prefix };

0 commit comments

Comments
 (0)