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
@@ -102,6 +105,39 @@ Congratulations! You’ve successfully deployed a Next.js web application to Git
102
105
103
106
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/).
104
107
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:
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.
0 commit comments