Skip to content

Commit 3ae56a4

Browse files
committed
Add github pages article
1 parent 15905e4 commit 3ae56a4

File tree

7 files changed

+243
-13
lines changed

7 files changed

+243
-13
lines changed

app.config.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,7 @@ const babelPluginLabels = [
1515
];
1616

1717
export default defineConfig({
18-
ssr: true,
1918
devOverlay: false,
20-
2119
server: {
2220
baseURL: process.env.BASE_PATH,
2321
static: true,

src/BlogIndex.tsx

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,14 @@
11
import { Separator } from "@kobalte/core";
2-
import { css } from "@linaria/core";
3-
import cn from "classnames";
4-
import { Component, JSX } from "solid-js";
2+
import { Component } from "solid-js";
53
import { For } from "solid-js/web";
64
import { config } from "~/config";
75
import { getArticles } from "~/ssg/getArticles";
86

97
const articles = getArticles();
108

11-
interface Props {
12-
children?: JSX.Element;
13-
}
14-
15-
const BlogIndex: Component<Props> = (props) => {
9+
const BlogIndex: Component = () => {
1610
return (
17-
<main class={cn(_BlogIndex)}>
11+
<main>
1812
<h1 class="mb-4 bg-colors-primary-500 pl-8 pr-8 text-center text-big text-colors-text-900a">
1913
Blog of a programming rabbit
2014
</h1>
@@ -49,6 +43,4 @@ const BlogIndex: Component<Props> = (props) => {
4943
);
5044
};
5145

52-
const _BlogIndex = css``;
53-
5446
export default BlogIndex;
Lines changed: 236 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,236 @@
1+
---
2+
title: Deploying a Solid Start app to Github pages
3+
---
4+
5+
import DialogImage from "~/DialogImage";
6+
import p1 from "./settings.jpg?lazy";
7+
import t1 from "./settings.jpg?lazy&size=700x";
8+
import p2 from "./workflow.jpg?lazy";
9+
import t2 from "./workflow.jpg?lazy&size=700x";
10+
import p3 from "./site.jpg?lazy";
11+
import t3 from "./site.jpg?lazy&size=500x";
12+
13+
14+
Solid Start is rapidly progressing toward the initial 1.0 stable release, so let's look
15+
at how we can build a pre-rendered page and deploy it to Github pages.
16+
17+
[Github pages](https://pages.github.com) is a great (and free) place to host static sites
18+
such as blog and portfolio websites.
19+
While there's plenty of documentation how to build and deploy a React app there, consider
20+
using a superior framework like Solid.js, built with
21+
[Solid Start](https://start.solidjs.com/) - the official meta-framework
22+
for building and deploying apps quickly.
23+
24+
# Creating a Github repository
25+
26+
The first step is creating a new Github repository, for this example I'll be calling
27+
mine `my-blog`.
28+
Next we need to configure Github pages, navigate to the `Settings > Pages` tab
29+
and set the `Source` to `Github Actions` so that our page gets build and deployed
30+
automatically through the CI.
31+
32+
<Img
33+
src={p1}
34+
thumbnail={t1}
35+
caption="We need to set the source to Github actions"
36+
/>
37+
38+
Optionally it's also possible to configure the app to run with a custom domain name.
39+
There's plenty of documentation on how to do so, but I'll be sticking with the defaults
40+
for this article. Regardless, the rest of the configuration won't change much.
41+
42+
43+
# Creating a Solid Start app
44+
45+
Follow the [offictial documentation](start.solidjs.com) to setup a new app, it's as simple
46+
as running a single command and selecting the presets you want.
47+
I'll be using the `basic` template, which comes with a simple landing page, a router
48+
and some simple client components to make sure things are working.
49+
50+
51+
```terminal
52+
[shiro@shiro-pc:~]# pnpm create solid
53+
Create-Solid v0.5.8
54+
55+
◇ Project Name
56+
│ my-blog
57+
58+
◇ Is this a Solid-Start project?
59+
│ Yes
60+
61+
◇ Which template would you like to use?
62+
│ basic
63+
64+
◇ Use Typescript?
65+
│ Yes
66+
67+
◇ Project successfully created! 🎉
68+
69+
◇ To get started, run: ─╮
70+
│ │
71+
│ cd my-blog │
72+
│ pnpm install │
73+
│ pnpm dev │
74+
│ │
75+
├────────────────────────╯
76+
[shiro@shiro-pc:~]# cd my-blog
77+
[shiro@shiro-pc:~/my-blog]# pnpm install
78+
Done in 23.3s
79+
```
80+
81+
Since we don't really have a server for our backend (Github pages just serves
82+
static assets), we need to update the configuration, so that the output will be a
83+
pre-rendered static site with client side interactivity.
84+
If you need to display dynamic content that isn't known at compile-time, you
85+
might want to consider adding an external API server and loading the dynamic
86+
data from the client after page load.
87+
88+
```tsx title="app.config.ts" diff
89+
import { defineConfig } from "@solidjs/start/config";
90+
91+
export default defineConfig({
92+
+ server: {
93+
+ baseURL: process.env.BASE_PATH,
94+
+ static: true,
95+
+ prerender: {
96+
+ failOnError: true,
97+
+ routes: ["/"],
98+
+ crawlLinks: true,
99+
+ },
100+
+ },
101+
});
102+
```
103+
It's also necessary to let the router know where the URL root of the site is, i.e. in
104+
the case of Github pages, it'll be the respository name, which we can inject as an
105+
environmental variable at build-time.
106+
107+
```tsx title="src/app.tsx" diff
108+
export default function App() {
109+
return (
110+
<Router
111+
+ base={import.meta.env.SERVER_BASE_URL}
112+
root={(props) => (...)}
113+
>
114+
<FileRoutes />
115+
</Router>
116+
);
117+
}
118+
```
119+
120+
With this, our router should now behave as expected.
121+
Let's add the Github Actions configuration so that our site will be built whenever
122+
a new commit is made to the `master` branch.
123+
You can customize the workflow in many ways, as described in the
124+
[docs](https://docs.github.com/en/actions/using-workflows/workflow-syntax-for-github-actions).
125+
126+
```yaml title=".github/workflows/CI.yml"
127+
name: Build package
128+
129+
on:
130+
push:
131+
branches: [ master ]
132+
tags: [ '*' ]
133+
pull_request:
134+
workflow_dispatch:
135+
136+
permissions:
137+
contents: read
138+
pages: write
139+
id-token: write
140+
141+
jobs:
142+
build:
143+
name: Build
144+
runs-on: ubuntu-latest
145+
steps:
146+
- uses: actions/checkout@v3
147+
148+
- uses: pnpm/action-setup@v2
149+
with:
150+
version: latest
151+
152+
- uses: actions/setup-node@v4
153+
with:
154+
node-version: '20.12.0'
155+
cache: 'pnpm'
156+
157+
- name: install
158+
run: pnpm install --frozen-lockfile
159+
160+
- name: build
161+
run: pnpm build
162+
env:
163+
BASE_PATH: '/my-blog'
164+
165+
- name: upload artifact
166+
uses: actions/[email protected]
167+
with:
168+
path: '.output/public'
169+
170+
deploy:
171+
name: Deploy
172+
needs: build
173+
runs-on: ubuntu-latest
174+
environment:
175+
name: github-pages
176+
url: ${{ steps.deployment.outputs.page_url }}
177+
steps:
178+
- name: Deploy to GitHub Pages
179+
id: deployment
180+
uses: actions/[email protected]
181+
```
182+
183+
While the configuration might look complicated, it's easy to look at individual steps
184+
and understand what they do. It's mostly about setting up the environment, install dependencies
185+
and finally build and deploy our app.
186+
Since the code is already hosted on Github, there's no need to pass in any secrets for deployment
187+
authorization, pretty convenient indeed.
188+
189+
The last step is to initialize the git repository in our codebase and push it to Github.
190+
191+
```terminal
192+
[shiro@shiro-pc:~/my-blog]# git init
193+
Initialized empty Git repository in ~/my-blog/.git/
194+
[shiro@shiro-pc:~/my-blog]# git remote add origin [email protected]:username/my-blog.git
195+
[shiro@shiro-pc:~/my-blog]# git add .
196+
[shiro@shiro-pc:~/my-blog]# git commit -m "Initial commit"
197+
[shiro@shiro-pc:~/my-blog]# git push
198+
To github.com:username/my-blog.git
199+
* [new branch] master -> master
200+
```
201+
202+
Once the code is committed, you can visit the `Actions` tab in the Github repository
203+
UI and ensure the new workflow is being run.
204+
205+
# Test it out
206+
207+
If everything went as expected, you should now see the new workflow run being successful
208+
after a couple of seconds. This means the static site is now deployed at
209+
`https://username.github.io/my-blog`
210+
211+
<Img
212+
src={p2}
213+
thumbnail={t2}
214+
caption="The workflow finished successfully"
215+
/>
216+
217+
Let's try opening the page and ensuring everything works as it should.
218+
219+
<Img
220+
src={p3}
221+
thumbnail={t3}
222+
caption="Our website hosted on Github pages"
223+
/>
224+
225+
With this you should have a basic Solid Start app that auto-builds and deploys
226+
itself on new commits. If you're interested in a more complex site that uses
227+
the same base configuration we just built, check out the repository for my
228+
blog site.
229+
230+
<Embed
231+
url="https://github.com/shiro/blog"
232+
description="My personal blog website"
233+
/>
234+
235+
With services like Github actions making building and deploying sites free and
236+
easy, there's no limit to what we can build, let your imagination run wild!
Loading
Loading
Loading

src/style/global.style.tsx

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,10 @@ export const globals = css`
206206
margin-bottom: 32px;
207207
background-color: ${color("colors/primary-50")};
208208
padding: 8px;
209+
& > code {
210+
display: flex;
211+
flex-direction: column;
212+
}
209213
.line {
210214
${subText};
211215
width: 100%;

0 commit comments

Comments
 (0)